Strange problem with numbers

Strange problem with numbers

Postby Thomas Grueninger » 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
User avatar
Thomas Grueninger
 
Posts: 18
Joined: Fri Oct 14, 2005 5:15 pm
Location: Germany

Re: Strange problem with numbers

Postby karinha » Wed Feb 10, 2016 1:57 pm

maybe.

Code: Select all  Expand view

#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
User avatar
karinha
 
Posts: 7804
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: Strange problem with numbers

Postby tiaofw » Wed Feb 10, 2016 2: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
tiaofw
 
Posts: 99
Joined: Fri Dec 12, 2008 4:39 pm
Location: Brasil

Re: Strange problem with numbers

Postby Enrico Maria Giordano » Wed Feb 10, 2016 2:17 pm

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

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Strange problem with numbers

Postby tiaofw » Wed Feb 10, 2016 2: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
tiaofw
 
Posts: 99
Joined: Fri Dec 12, 2008 4:39 pm
Location: Brasil

Re: Strange problem with numbers

Postby Enrico Maria Giordano » Wed Feb 10, 2016 2:39 pm

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

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Strange problem with numbers

Postby Thomas Grueninger » Thu Feb 11, 2016 5:41 am

It works simple. You never come to 8.8

local n:=8.8
MsgInfo(str(n,20,18))
---------------------------
Information
---------------------------
8.800000000000001000
---------------------------
OK
---------------------------
User avatar
Thomas Grueninger
 
Posts: 18
Joined: Fri Oct 14, 2005 5:15 pm
Location: Germany

Re: Strange problem with numbers

Postby Enrico Maria Giordano » Thu Feb 11, 2016 9:03 am

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

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 66 guests