Numbers
Lua 5.3 has two kinds of numbers, whole-number integers and decimal floats, and mixes them for you.
In this page:
Numbers
Lua 5.3 has integer and float subtypes of the number type. Integers are 64-bit by default, and a value with a decimal point or exponent is a float. Operations mix them automatically, and math.type tells you which subtype a value has.
Note:
Use math.type(x) to check for "integer" or "float", since type(x) says only "number".
Example: Numbers
print(10, 10.0, 1e2)
print(math.type(10), math.type(10.0), math.type("10"))
print(3 == 3.0)
print(math.maxinteger)
print(math.maxinteger + 1 == math.mininteger)
-- Output:
-- 10 10.0 100.0
-- integer float nil
-- true
-- 9223372036854775807
-- true
Login to try C/C++/Java/PHP code in the editor
Common Mistakes
- Assuming all numbers are floats as in Lua 5.2
- Expecting 3 and 3.0 to print the same
- Expecting integer overflow to raise an error
Chapter Summary
- There are integer and float subtypes
- A decimal point makes a float
- math.type reveals the subtype
- Integers wrap around on overflow
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: