Garry's Mod Wiki

Revision Difference

number#566256

<title>Numbers & Integers</title> <cat>Dev.Lua</cat> All numbers in Lua use the [double-precision floating-point format](https://en.wikipedia.org/wiki/double-precision_floating-point_format). Even integers are stored in this format. The <page>math</page> library is the primary number operation library that contains trigonometric, rounding and useful constants. The following is a comprehensive list of all valid ways to define numbers in Lua. All numbers in Lua use the [double-precision floating-point format](https://en.wikipedia.org/wiki/double-precision_floating-point_format). Even integers are stored in this format. The <page>math</page> library is the primary number operation library that contains trigonometric, rounding and useful constants. The following is a comprehensive list of all valid ways to define numbers in Lua. You can find the complete implementation of the number tokenization function [in the LuaJIT source code](https://github.com/LuaJIT/LuaJIT/blob/v2.1/src/lj_lex.c#L95-L137). ```lua print( 123, --- 123 123.456, --- 123.456 123.456e7, --- 1234560000⤶ 0x123.456e7, --- 291.271216...⤶ 123e4, --- 1230000⤶ 123e+4, --- 1230000⤶ 123e-4, --- 0.0123⤶ 0x0FFp2, --- 1020⤶ 0x0FFp+2, --- 1020⤶ 0x0FFp-2, --- 63.75⤶ -- 123e4.5 is not valid⤶ 123, --- 123 123.456, --- 123.456 0x0FF.123, --- 255.071...⤶ ⤶ -- Numbers can have an exponent part declared with 'e' or 'E' as a suffix⤶ -- This multiplies the base number by 10 to the power of the number after the 'e'⤶ -- As in, 123e4 is (123 * (10 ^ 4))⤶ -- The number after the marker can be positive or negative, as seen below (handled at Lex time)⤶ 123e4, --- 1230000⤶ 123e+4, --- 1230000⤶ 123e-4, --- 0.0123⤶ 123.4e5, --- 12340000⤶ ⤶ --- Hexadecimal numbers can have an exponent part too, but its marked with a 'p' or 'P'⤶ --- Follows the same syntax rules as above⤶ 0x0FFp2, --- 1020 (0x0FF * (10 ^ 2))⤶ 0x0FFp+2, --- 1020⤶ 0x0FFp-2, --- 63.75⤶ ⤶ -- 123e4.5 is not valid⤶ -- 0x0FFp123.456 is not valid ) ```