Page 1 of 1

Strange problem with numbers

Posted: Wed Feb 10, 2016 12:16 pm
by Thomas Grueninger
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

Re: Strange problem with numbers

Posted: Wed Feb 10, 2016 1:57 pm
by karinha
maybe.

Code: Select all

#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
*/
 

Re: Strange problem with numbers

Posted: Wed Feb 10, 2016 2:02 pm
by tiaofw
Good Morning.

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

It is good to inform xHarbour developers.

bye.

Re: Strange problem with numbers

Posted: Wed Feb 10, 2016 2:17 pm
by Enrico Maria Giordano
No, it's the normal behaviour of the floating-point numbers.

EMG

Re: Strange problem with numbers

Posted: Wed Feb 10, 2016 2:34 pm
by tiaofw
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

Re: Strange problem with numbers

Posted: Wed Feb 10, 2016 2:39 pm
by Enrico Maria Giordano
Better to round it to the desired number of decimals using ROUND() function.

EMG

Re: Strange problem with numbers

Posted: Thu Feb 11, 2016 5:41 am
by Thomas Grueninger
It works simple. You never come to 8.8

local n:=8.8
MsgInfo(str(n,20,18))
---------------------------
Information
---------------------------
8.800000000000001000
---------------------------
OK
---------------------------

Re: Strange problem with numbers

Posted: Thu Feb 11, 2016 9:03 am
by Enrico Maria Giordano
Clipper, Harbour, xHarbour floating-point system supports up to 15 significant digits.

EMG