Lua Integers vs Floats
Since Lua 5.3, numbers come in two subtypes: integers (64-bit, exact) and floats (64-bit IEEE doubles, approximate). This lesson explains which operations produce which subtype, how to convert between them, and the edge cases that bite.
print(math.type(3), math.type(3.0))
print(3 == 3.0) -- equal as numbers
print(3 .. "", 3.0 .. "") -- but they print differentlyinteger float
true
3 3.0
Which operations give which subtype
| Operation | Integer operands | Any float operand |
|---|---|---|
+ - * // % |
integer | float |
/ |
float | float |
^ |
float | float |
math.floor, math.ceil |
integer | integer if it fits |
bitwise & | ~ << >> |
integer | converted to integer (must have an exact value) |
print(6 + 2, 6 * 2, 6 // 2, 6 % 4)
print(6 / 2, 6 ^ 2)
print(6 + 2.0, 6 // 2.0)
print(math.floor(3.7), math.ceil(3.2))8 12 3 2
3.0 36.0
8.0 3.0
3 4
Converting
-- float -> integer
print(math.tointeger(8.0)) -- exact value: converts
print(math.tointeger(8.5)) -- has a fraction: nil
print(math.floor(8.5), 8.5 // 1)
-- integer -> float
print(8 + 0.0, 8 * 1.0)
-- string -> number keeps the subtype the text suggests
print(math.type(tonumber("8")), math.type(tonumber("8.0")))8
nil
8 8.0
8.0 8.0
integer float
math.floor returns an integer, while x // 1 keeps a float a float. A common trick to "drop the .0" before printing is math.tointeger(x) or x.
Integer limits and wrap-around
Integers have a fixed range. math.maxinteger is 2⁶³ − 1. Going past it wraps around silently instead of growing or raising an error:
print(math.maxinteger)
print(math.mininteger)
print(math.maxinteger + 1 == math.mininteger)
print(math.maxinteger * 2)9223372036854775807
-9223372036854775808
true
-2
Floats have a much bigger range but lose precision beyond 2⁵³: above that, not every whole number can be stored.
local big = 2 ^ 53
print(string.format("%.0f", big))
print(big + 1 == big) -- the +1 is lost
print(math.tointeger(big) + 1) -- exact as an integer9007199254740992
true
9007199254740993
Comparing integers and floats
Comparisons between the two subtypes compare the mathematical values exactly, so there are no surprises from conversion:
print(1 == 1.0, 1 < 1.5, -0.0 == 0)
print(math.ult(1, -1)) -- compare as unsigned integerstrue true true
true
math.ult treats integers as unsigned, so -1 becomes the largest possible value.
Table keys
Floats with an exact integer value are normalised to integers when used as table keys, so t[1] and t[1.0] are the same slot:
local t = {}
t[1.0] = "one"
print(t[1])
print(math.type(next(t)))one
integer
Rules of thumb
- Use integers for counting, indexing and money in cents.
- Use floats for measurements, physics, percentages.
- Use
//when you want integer division and/when you want an exact quotient. - When printing a value that might be
3.0, decide which form you want and format it with%d(requires an integer value) or%.2f.
Test yourself
What is the subtype of 10 // 3?
- integer
- float
- it depends on the platform
- string
What does math.tointeger(2.5) return?
23nil- an error
What is math.maxinteger + 1?
- An overflow error
math.hugemath.mininteger(it wraps around)- A float