# Lua multiplication problem Classic List Threaded 5 messages Open this post in threaded view
|

## Lua multiplication problem

 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. ``` Here are some examples: ```if ( 20425 * .053 ) == 1082.525 then print ("equal") else print ("not equal") end if rawequal( 20425 * .053, 1082.525 ) then print ("equal") else print ("not equal")end ``` ```Both result in "not equal". Lua interprets the multiplication as less than the correct number. ``` ```if ( 20425 * .053 ) < 1082.525 then print ("less than") else print ("not less than") end ```less than ```I am using Lua 5.02 on Windows from the LuaBinaries (Release 2). I quickly downloaded and tested Lua 4.0 and it has the same issue. ``` Thanks for any help on this. Tom ```
Open this post in threaded view
|

## Re: Lua multiplication problem

 ```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 ```
Open this post in threaded view
|

## Re: Lua multiplication problem

 In reply to this post by Tom Reahard ```> if ( 20425 * .053 ) == 1082.525 then print ("equal") else print ("not > equal") end Welcome to the floating-point world: 0.053 dos not have an exact representation. This has nothing to do with Lua. See http://lua-users.org/wiki/FloatingPoint http://docs.python.org/tut/node16.html --lhf ```
Open this post in threaded view
|

## Re: Lua multiplication problem

 ```> > if ( 20425 * .053 ) == 1082.525 then print ("equal") else print ("not > > equal") end > > Welcome to the floating-point world: 0.053 dos not have an exact > representation. This has nothing to do with Lua. See > http://lua-users.org/wiki/FloatingPoint > http://docs.python.org/tut/node16.html Welcome, indeed. See also http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm for a quick (but not highly recommended) workaround: > tol = 2^-52 -- double machine precision > function eq(x,y) return x == y or >> math.abs(x-y) < tol * math.max(math.abs(x), math.abs(y)) end > = eq(20425 * .053, 1082.525) true Cheers, luis. -- A mathematician is a device for turning coffee into theorems. -- P. Erdos -- Luis Carvalho Applied Math PhD Student - Brown University PGP Key: E820854A <[hidden email]> ```
 Thanks for the explanation. I can work around this. Regards, Tom Luiz Henrique de Figueiredo wrote: ```if ( 20425 * .053 ) == 1082.525 then print ("equal") else print ("not equal") end ``` ``` Welcome to the floating-point world: 0.053 dos not have an exact representation. This has nothing to do with Lua. See http://lua-users.org/wiki/FloatingPoint http://docs.python.org/tut/node16.html --lhf ``` ```-- Tom Reahard Symmetry Software 14350 N. 87th Street, #170 Scottsdale, AZ 85260 Ph: 480-596-1500 x120 Web: www.symmetry.com ```