Numbers

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).

Signed and Unsigned

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.

Notation

Numbers are notated with the most significant digits first (big-endian). There are multiple ways to notate number literals in Lua:

  • Decimal (base-10) — 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.

  • Scientific notation — 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).

  • Hexadecimal (base-16) — Begin the number with 0x followed by digits 0–9 or A–F (capitalization ignored). For example, 0xF is 15 and 0x3FC is 1020.

  • Binary (base-2) — Begin the number with 0b followed by 0s or 1s, for instance 0b1100 (12 in decimal format).

To aid in the readability of long numbers, underscores can be included anywhere within a number literal without changing the value, except at the beginning (in which case it would become an identifier). For example, 1_234_567 is the same as 1234567, both of which are equal to 1,234,567.

Operations

Lua math and relational operators can be used on numbers to manipulate and compare them. Mathematical functions such as math.sqrt() and math.exp() can be found in the math library and, for bitwise operations, the bit32 library has been back-ported.

Number Classifications

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.

int

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.

  • To round a number to the nearest integer (half up), use math.floor(x + 0.5).

int64

The int64 number type refers to a signed 64-bit integer, a signed 64-bit integer is expected (-263 to 263 - 1).

float

The float number type refers to a single-precision (32-bit) floating point number. This type isn’t as precise as normal numbers, but the difference typically won’t matter.

Last updated