KeatsKelleher
KeatsKelleher

Reputation: 10191

Why does lua 5.1 interpret certain integers incorrectly?

Lua 5.1 appears interpret many perfectly valid 64b integers as

1,805,996,217,335,808,768 

while the largest signed valid 64b integer is

9,223,372,036,854,775,807

Here is an example of the unfortunate output:

Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> return 1805996217335808768 == 1805996217335808804
true
> return 1805996217335808768 == 1805996217335808805
true
> return 1805996217335808768 == 1805996217335808806
true
> return 1805996217335808768 == 1805996217335808769
true
> return 1805996217335808768 == 1805996217335808767
true

One would expect to see a false everywhere there is a true here.

Edit (flagged as duplicate):

This is not a duplicate of this question because you cannot compile Lua 5.1 to handle 64b integers.

Upvotes: 4

Views: 805

Answers (1)

Darius
Darius

Reputation: 1180

Lua 5.1 uses double type with precision of 53 bits.

Only first 53 bits of number will be stored in double fraction part. Max effective integer is 9007199254740991. If number is greater as 9007199254740991, last bits until 53 bits will be zeroed.

54 bits number:

18014398509481983 = 111111111111111111111111111111111111111111111111111111b

internally will be stored as

18014398509481982 = 111111111111111111111111111111111111111111111111111110b

as only first 53 bits are used, so both are equal.

Upvotes: 1

Related Questions