5.5 – Mathematical Functions
This library is an interface to most of the functions of the standard C math library. (Some have slightly different names.) It provides all its functions inside the table math
. In addition, it registers the global __pow
for the binary exponentiation operator ^
, so that x^y
returns xy. The library provides the following functions:
- math.abs math.acos math.asin math.atan math.atan2
- math.ceil math.cos math.deg math.exp math.floor
- math.log math.log10 math.max math.min math.mod
- math.pow math.rad math.sin math.sqrt math.tan
- math.frexp math.ldexp math.random math.randomseed
plus a variable math.pi
. Most of them are only interfaces to the corresponding functions in the C library. All trigonometric functions work in radians (previous versions of Lua used degrees). The functions math.deg
and math.rad
convert between radians and degrees.
The function math.max
returns the maximum value of its numeric arguments. Similarly, math.min
computes the minimum. Both can be used with 1, 2, or more arguments.
The functions math.random
and math.randomseed
are interfaces to the simple random generator functions rand
and srand
that are provided by ANSI C. (No guarantees can be given for their statistical properties.) When called without arguments, math.random
returns a pseudo-random real number in the range [0,1). When called with a number n, math.random
returns a pseudo-random integer in the range [1,n]. When called with two arguments, l and u, math.random
returns a pseudo-random integer in the range [l,u]. The math.randomseed
function sets a “seed” for the pseudo-random generator: Equal seeds produce equal sequences of numbers.