lua-users home
lua-l archive

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


> Sometimes operations on such "integers" give wrong results:
> 
> > a = 2^53-1
> > =("%.f"):format(a)
> 9007199254740991
> > =(-a)%(a-1)
> -1
> > =math.floor((-a)/(a-1))
> -1

On my machine I've got correct results. (In the last line, the result of
the division is not an integer, so it is reasonable that it can have
precision errors.)

$ lua5.1
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> a = 2^53-1
> =("%.f"):format(a)
9007199254740991
> =(-a)%(a-1)
9.007199254741e+15
> = ("%.f"):format((-a)%(a-1))
9007199254740989
> = math.floor((-a)/(a-1))
-2

(The actual value of ((-a)/(a-1)) is slightly less than -1,
something like (-1 - 2.2204460492503e-16).)

The results are the same with Lua 5.1, 5.2, 5.3, and Luajit 2.0.2.

-- Roberto