lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Tom Reahard wrote:
I am experiencing some strange behavior with numbers where the results of a multiplication should equal a given number but Lua does not recognize the equality.

it appears to be a floating-point issue :

Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> print ( 20425 * .053, 1082.525, 1082.525-(20425*0.053) )
1082.525        1082.525        2.2737367544323e-013

> print ( 20.425 * 53, 1082.525,1082.525-(20.425*53) )
1082.525        1082.525        0

note how the integer multiply works ok!

this is _not_ a lua issue - try the following in c++ :

 double d1 = 20425.0;
 double d2 = 0.053;
 double d3 = 1082.525;

 double d4 = d1 * d2;
 double d5 = d3 - d4;

d5 == 2.27x10-13 also !

it will have something to do with the binary representation of one of the doubles, i guess...

Adrian