FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Strange problem with numbers
Posts: 18
Joined: Fri Oct 14, 2005 05:15 PM
Strange problem with numbers
Posted: Wed Feb 10, 2016 12:16 PM

Hello,

i have a strange Problem with numbers.

for n:=1.8 to 10.8 step .5
? str(n,22,18)
next

1.800000000000000000
2.300000000000000000
2.800000000000000000
3.300000000000000000
3.800000000000000000
4.300000000000000000
4.800000000000000000
5.300000000000000000
5.800000000000000000
6.300000000000000000
6.800000000000000000
7.300000000000000000
7.800000000000000000
8.300000000000001000
8.800000000000001000
9.300000000000001000
9.800000000000001000
10.300000000000000000
10.800000000000000000

Tested with xHarbour 1.2.3, BCC7 and xHarbour 1.2.0., BCC5
Same Result

Have anyone a solution?

Regards
Thomas Grueninger

Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: Strange problem with numbers
Posted: Wed Feb 10, 2016 01:57 PM
maybe.

Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()

   LOCAL N := 0

   for n:=1.8 to 10.8 step .5

      ? str(n,22,18)

      N = N + .1

   next

Return Nil

/*
1.800000000000000000
2.300000000000000000
2.800000000000000000
3.300000000000000000
3.800000000000000000
4.300000000000000000
4.800000000000000000
5.300000000000000000
5.800000000000000000
6.300000000000000000
6.800000000000000000
7.300000000000000000
7.800000000000000000
8.300000000000001000
8.800000000000001000
9.300000000000001000
9.800000000000001000
10.300000000000000000
10.800000000000000000
*/


João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 100
Joined: Fri Dec 12, 2008 04:39 PM
Re: Strange problem with numbers
Posted: Wed Feb 10, 2016 02:02 PM

Good Morning.

Full is a bug this xHarbour because this returning garbage in decimals.

It is good to inform xHarbour developers.

bye.

Contagem/Brazil

FWH/xharbour 15.12/PELLES C, MED, DBF
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Strange problem with numbers
Posted: Wed Feb 10, 2016 02:17 PM

No, it's the normal behaviour of the floating-point numbers.

EMG

Posts: 100
Joined: Fri Dec 12, 2008 04:39 PM
Re: Strange problem with numbers
Posted: Wed Feb 10, 2016 02:34 PM

Enrico understood, then the solution would be to truncate the decimals?

I did not understand what the problem is that this variation is causing him.

Thanks

Contagem/Brazil

FWH/xharbour 15.12/PELLES C, MED, DBF
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Strange problem with numbers
Posted: Wed Feb 10, 2016 02:39 PM

Better to round it to the desired number of decimals using ROUND() function.

EMG

Posts: 18
Joined: Fri Oct 14, 2005 05:15 PM
Re: Strange problem with numbers
Posted: Thu Feb 11, 2016 05:41 AM

It works simple. You never come to 8.8

local n:=8.8
MsgInfo(str(n,20,18))


Information

8.800000000000001000

OK

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Strange problem with numbers
Posted: Thu Feb 11, 2016 09:03 AM

Clipper, Harbour, xHarbour floating-point system supports up to 15 significant digits.

EMG

Continue the discussion