Sebastien Diot
Sebastien Diot

Reputation: 7359

64 unsigned integers in Lua 5.3/5.4 do not behave like in "Programming in Lua"

I'm reading "Programming in Lua, Forth Edition", and I'm stuck on exercise 13.1, because Lua does not seem to behave like in the book. The section 13.2, about Unsigned Integers, states:

> x = 13835058055282163712   -- 3ul << 62ul
> x                          --> -4611686018427387904

But if I do that in Lua 5.4, Windows x64, or Lua 5.3, WSL x64, they both behave like this:

> x = 13835058055282163712
> x
1.3835058055282e+19
> math.type(x)
float

So, I have 2 questions:

  1. Why does "the book" about learning Lua 5.3, shows examples not matching actual Lua implementations? Is that due to some later update to the language?
  2. Is there a work-around? How to I write large "unsigned integers" in decimal, without them being turned into floats?

Upvotes: 3

Views: 167

Answers (1)

ESkri
ESkri

Reputation: 1898

At the time the book was written (it corresponds to Lua version 5.3.0) Lua had wrong behavior of reading long integer literals: it wraparounded them.
Later, in Lua 5.3.3 this behavior has been corrected to a more user-friendly.
As for now, when a decimal integer literal in beyond int64 range, Lua reads it as float number to preserve its numeric value. Hexadecimal integer literals are wraparounded.

Upvotes: 3

Related Questions