Numbers and Arithmetic

4.1 Integers and Floats (5.3+)

Lua 5.1/LuaJIT: all numbers are doubles. There's no integer subtype, no //, and no bitwise operators (LuaJIT has the bit library).

print(3, 3.0, 3 == 3.0)            --> 3  3.0  true
print(1 / 2)                       --> 0.5   (/ always produces a float)
print(7 // 2, 7.0 // 2)            --> 3  3.0 (floor division)
print(-7 // 2)                     --> -4    (floors toward negative infinity)
print(7 % 3, -7 % 3, 7 % -3)       --> 1  2  -2 (result has the sign of the divisor)
print(2 ^ 10)                      --> 1024.0 (^ always produces a float)
print(math.maxinteger, math.mininteger)
print(math.maxinteger + 1 == math.mininteger)   --> true (integer overflow wraps around)
print(0x7fffffffffffffff, 0xA, 1e3, 0x1p4)     --> hex, scientific, hex float (16.0)
print(1e308 * 10, -1e308 * 10, 0/0 ~= 0/0)     --> inf  -inf  true (NaN is never equal to itself)
print(3 | 0, 3.0 | 0)              --> 3  3 (bitwise ops convert floats with exact integer values)
print(string.format("%.2f", 2/3))  --> 0.67

4.2 Bitwise Operators (5.3+)

print(5 & 3, 5 | 3, 5 ~ 3, ~5, 1 << 4, 256 >> 4)   --> 1  7  6  -6  16  16
-- & and, | or, ~ xor (binary), ~ not (unary), << shift left, >> logical shift right

4.3 The math Library (Preview)

print(math.floor(3.7), math.ceil(3.2), math.abs(-5), math.max(1, 9, 3), math.min(4, 2))
print(math.sqrt(16), math.pi, math.huge, math.sin(math.pi / 2), math.log(8, 2), math.exp(1))
print(math.fmod(7, 3), math.modf(3.7))             --> 1  3.0  0.7
math.randomseed(os.time())                         -- 5.4 seeds randomly by default
print(math.random(), math.random(10), math.random(5, 10))