Numbers
Last updated
Was this helpful?
Last updated
Was this helpful?
A number in Lua is a double precision floating point number (or just double). For instance:
5
9.12761656
-1927
In Lua, numbers can range from -1.7 × 10308 to 1.7 × 10308 (around 15 digits, positive or negative).
The sign of the number indicates whether it’s positive or negative. A signed number can be positive or negative, but an unsigned number cannot be negative. In Lua, -0
is distinct from 0
.
Numbers are notated with the most significant digits first (big-endian). There are multiple ways to notate number literals in Lua:
— Write the digits of the number normally using digits 0–9 with a single optional decimal point, for example 7
, 1.25
, or -22.5
.
— Write a decimal number followed by e
or e+
, then an integer to raise the decimal number to a power of 10. For instance, 12e3
is 12 × 10^3 (12,000).
— Begin the number with 0x
followed by digits 0–9 or A–F (capitalization ignored). For example, 0xF
is 15 and 0x3FC
is 1020.
— Begin the number with 0b
followed by 0s or 1s, for instance 0b1100
(12 in decimal format).
In BrickLua, there is no technical difference between the following types of numbers. However, number classifications are used in documentation to indicate which kind of number is involved with an API member.
The int
number type refers to a number without a fractional portion (integer) like 0, 60, or -42. Properties and functions that expect integers may automatically round or raise errors when provided with non-integers.
When working with integers in Lua, note the following:
The fractional portion of a number can be trimmed by rounding down with math.floor()
.
You can determine if a number is an integer by comparing math.floor(x) == x
.
The int64
number type refers to a signed 64-bit integer, a signed 64-bit integer is expected (-263 to 263 - 1).
Lua math and relational can be used on numbers to manipulate and compare them. Mathematical functions such as math.sqrt()
and math.exp()
can be found in the library and, for bitwise operations, the library has been back-ported.
To a number to the nearest integer (half up), use math.floor(x + 0.5)
.
The float
number type refers to a . This type isn’t as precise as normal numbers, but the difference typically won’t matter.