Lua's math library contains a collection of functions that are basically a subset of C's math library.
The math library is always available, and its functions can be accessed by name as shown in the following example:
print(math.abs(-123)) -- 123
The math library contains the following functions:
- math.abs (x)
- Returns the absolute value of
x - math.acos (x)
- Returns the arc-cosine of
x, in radians. - math.asin (x)
- Returns the arc-sine of
x, in radians. - math.atan (x)
- Returns the arc-tangent of
x, in radians. - math.atan2 (x, y)
- Returns the arc-tangent of y/x, in radians, based on the signs of both values to determine the correct quadrant.
- math.ceil (x)
- Returns the smallest integer value greater than or equal to
x. - math.cos (x)
- Returns the cosine of
x, wherexis specified in radians. - math.deg (x)
- Convert
x, specified in radians, to degrees - math.exp (x)
- Returns the value of e raised to the xth power.
- math.floor (x)
- Returns the largest integer value less than or equal to
x. - math.fmod (x, y)
- Return a value equal to
x - n*yfor some integernsuch that the result has the same sign asxand magnitude less thanabs(y). - math.frexp (x)
- Returns values (m, e) such that
m*2^e==x. - math.ldexp (x, y)
- Return
x * (2**y), essentially the inverse of functionmath.frexp. - math.log (x)
- Return the natural log of
x - math.log10 (x)
- Return the base-10 logarithm of
x - math.max (x, ...)
- Returns the maximum value among its arguments.
- math.min (x, ...)
- Returns the minimum value among its arguments.
- math.modf (x)
- Spits real number
xinto it's integer and fractional parts. - math.pi
- The constant pi, approximately equal to 3.14159.
- math.pow (x, y)
- Returns
xraised to the power ofy. - math.rad (x)
- Convert
x, specified in degrees, to radians - math.sin (x)
- Returns the sine of
x, wherexis specified in radians. - math.sqrt (x)
- Returns the square root of
x - math.tan (x)
- Returns the tangent of
x, wherexis specified in radians. - math.random ()
- Return a uniformly-distributed
pseudo-random number between
0(inclusive) and1 - math.random (x)
- Return a uniformly-distributed
pseudo-random number between
1(inclusive) andx - math.random (x, y)
- Return a uniformly-distributed,
pseudo-random number between
x(inclusive) andy(inclusive). - math.randomseed (x)
- Sets
xas the "random seed" for the pseudo-random generator