[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua multiplication problem
- From: Luis Carvalho <carvalho@...>
- Date: Fri, 25 Nov 2005 10:44:38 -0500
> > 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 <carvalho@dam.brown.edu>