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

------------------------------------------------------------------------------
WatchGuard Dimension instantly turns raw network data into actionable 
security intelligence. It gives you real-time visual feedback on key
security issues and trends.  Skip the complicated setup - simply import
a virtual appliance and go from zero to informed in seconds.
http://pubads.g.doubleclick.net/gampad/clk?id=123612991&iu=/4140/ostg.clktrk
_______________________________________________
Gambas-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gambas-user

Reply via email to