[Gambas-user] Possible bug in round() function

Benoît Minisini gambas at ...1...
Thu Jan 30 19:48:43 CET 2014


Le 30/01/2014 11:33, Ricardo Díaz Martín a écrit :
> Hi guys,
>
> Try to do this in the gambas console:
>
> ? round (283.5 * 0.21, -2)
> 59,53
>
> ? 283.5 * 0.21
> 59,535
>
> ? round(59.535, -2)
> 59,54
>
> Why round (283.5 * 0.21, -2) doesn't works as expected? (or as I
> expected...)
>
> I got the same wrong behavior in my program using something like fAux =
> round(fNumber1, fNumber2, -2)
>
> Regards,
> Ricardo Díaz
>

Round() is defined that way:

Round(x, y) = Int(x / (10 ^ y) + 0.5) * (10 ^ y)

283.5 * 0.21 is not equal to 59.535 in binary:

?283.5 * 0.21 - 59.535
-7,105427357601E-15

59.535 cannot be encoded in binary (like 1/3 in decimal):

59.535 = 59 + 0.100010001111010111000010100011110101110000101000...

The '11110101110000101000' pattern is repeated ad vitam aeternam.

So 283.5 * 0.21 is lower than 59.535, and so Round returns 59.53.

As Tobi said, floating points are disillusioning!

-- 
Benoît Minisini




More information about the User mailing list