Numbers Tutorial 

Some languages support one or more of the following number types by default:
In the interest of simplicity Lua supports only one type of number, floating point numbers. By default these are double precision floating point numbers, but Lua can easily be recompiled to support single precision floating point numbers should you so desire. If you are unfamiliar with floating point it may be advantageous for you to read about FloatingPoint numbers.
The main thing to remember is that if you use numbers with fractional parts (or division), they may have rounding error, since they are stored in a limited amount of space. Also numbers that are short or don't have infinitely repeating patterns in decimal may have them in binary, so don't assume that any fractional number is "safe". The main things to remember are not to use the ==
operator with fractional numbers (since it checks for perfect equality), and to write your code so that rounding error can't build up to large amounts over time.
If your numbers are integers (with no fractional part), and they don't reach 2^53, then all this isn't an issue.
We can use the Lua interactive command line prompt as a calculator by prefixing an expression by =
, e.g.,
Lua 5.1 Copyright (C) 19942006 Lua.org, PUCRio > = 1 1 > = 1 + 2 3 > = 3.1415927 3.1415927 > = 5 / 6 0.83333333333333
> = 1.2345e6 1234500 > = 543.21E8 54321000000 > = 2.56e4 0.000256
> width = 7.5
> height = 12.7
> = width * height
95.25
> depth = 2.8
> area = width * height
> volume = area * depth
> print(area, volume)
95.25 266.7
Lua is equipped with a math library (see section 5.6 of the Reference Manual [1]). The functions provided are as follows:
math.abs math.acos math.asin math.atan math.atan2 math.ceil math.cos math.cosh math.deg math.exp math.floor math.fmod math.frexp math.ldexp math.log math.log10 math.max math.min math.modf math.pow math.rad math.random math.randomseed math.sin math.sinh math.sqrt math.tan math.tanh
> = math.sqrt(101) 10.049875621121 > = math.pi 3.1415926535898 > = math.sin( math.pi/3 ) 0.86602540378444
You can convert strings to numbers using the function tonumber()
. This takes a string argument and returns a number.
> = tonumber("123") + 25 148 > x = tonumber("123.456e5") > print(x) 12345600
Lua will automatically convert string and number types to the correct format in order to perform calculations. For example, if you try to apply an arithmetic operation to a string, Lua will try to convert that string to a number first, otherwise the operation will not work. If the string cannot be converted to a number an error is raised. This automatic conversion of types is called coercion.
> = 100 + "7" 107 > = "1000" + 234 1234 > = "hello" + 234 stdin:1: attempt to perform arithmetic on a string value stack traceback: stdin:1: in main chunk [C]: ? > = 234 + "1000" 1234
"hello"
cannot be converted to a number and so an error occurs. In statically typed languages (e.g. C) this would cause an error as you cannot assign a value to a variable of an incompatible type. This works in Lua because it is dynamically typed.
A notable exception: comparison operators (== ~= < > <= >=
) do not coerce their arguments.
The (in)equality operators consider a number to be not equal to its string representation
(or any nonnumber type in fact).
Ordered comparison operators throw an error when you feed them different types.
> = 100 == "100" false > = 100 ~= "hello" true > = 100 ~= {} true > = 100 == tonumber("100") true > = 100 <= "100" stdin:1: attempt to compare number with string stack traceback: stdin:1: in main chunk [C]: ?