number

Contents

number#

Lua 5.3+ has two number subtypes, integer (64-bit signed) and float (double-precision). Arithmetic between integers stays integer; / always produces a float; // is floor division; % follows Lua’s “math mod” rule (result has the sign of the divisor).

Integer arithmetic stays integer.

print(1 + 2)             --> 3

/ always returns a float.

print(1 / 2)             --> 0.5

Floor division // returns an integer.

print(1 // 2)            --> 0

Exponentiation ^ always returns a float.

print(2^10)              --> 1024.0

math.type reveals the subtype.

print(math.type(1))      --> integer
print(math.type(1.0))    --> float

tonumber parses user input and returns nil on failure rather than throwing.

print(tonumber("42"))        --> 42
print(tonumber("ff", 16))    --> 255

References#

  • Casting for tonumber and number-to-integer rules.

  • Operators for the arithmetic and comparison surface.