Lua math functions
Lua math functions

These are the functions in the "math" table.

Trigonometry functions use radians. To convert degrees to radians use math.rad.
Or, to look at it another way:


radians = degrees * math.pi / 180





math.abs (v)

Absolute value of v.


math.abs (-5) --> 5


math.acos (v)

Arc cosine of v. (Inverse operation of math.cos (v)).


math.deg (math.acos (0.5)) --> 60


math.asin (v)

Arc sine of v. (Inverse operation of math.sin(v)).

math.atan (v)

Arc tangent of v. (Inverse operation of math.tan (v)).

math.atan2 (v1, v2)

Arc tangent of v1/v2.

math.ceil (v)

Smallest integer >= v


math.ceil (5.5) --> 6


math.cos (v)

Cosine of v radians.


math.cos (math.rad (60)) --> 0.5


math.cosh (v)

Hyperbolic cosine of v radians.


math.cosh (math.rad (60)) --> 1.6002868577024


math.deg (v)

Convert v from radians to degrees. Note that math.rad is the inverse operation.


math.deg (1.0471975511966)  --> 60


math.exp (v)

e to the power v. Note that math.log is the inverse operation.


math.exp (1) --> 2.718281828459 (e itself in this case)


math.floor ()

Largest integer <= v


math.floor (5.5) --> 5


math.fmod (v1, v2)

The modulus (remainder) of doing: v1 / v2


math.fmod (100, 22) --> 12


m, n = math.frexp (v)

The frexp function breaks down the floating-point value (v) into a mantissa (m) and an exponent (n), such that the absolute value of m is greater than or equal to 0.5 and less than 1.0, and v = m * 2^n.
Note that math.ldexp is the inverse operation.


m, n = math.frexp (1.23456e24)
print (m) --> 0.51060204851673
print (n) --> 81
print ( 0.51060204851673 * 2^81 )  --> 1.23456e+024


math.huge

The value HUGE_VAL, a value larger than or equal to any other numerical value. Note that this is a number, not a function.


math.huge --> 1.#INF


math.ldexp (m, n)

The ldexp function returns the value of m * 2^b.
Note that math.frexp is the inverse operation.

 
math.ldexp (0.51060204851673, 81)   --> 1.23456e+024
math.ldexp (math.frexp (23452345) ) --> 23452345


math.log (v)

Natural log of v. Note that math.exp is the inverse operation.


math.log (123456) --> 11.723640096265
math.exp (math.log (123456))  --> 123456


math.log10 (v)

Log to the base 10 of v.


math.log10 (123456) --> 5.0915122016278
10 ^ 5.0915122016278 --> 123456.00000001 (slight rounding error)


math.max (v1, v2, ...)

The highest of one or more numbers.


math.max (4, 8, 100) --> 100


math.min ()

The lowest of one or more numbers.


math.min (4, 8, 100) --> 4


i, f = math.modf (v)

Returns two numbers, the integral part of v and the fractional part of v


math.modf (10.2) --> 10  0.2


math.pi

A variable representing the value of pi (not a function).


print (math.pi) --> 3.1415926535898 


math.pow (v1, v2)

v1 raised to the power v2.


math.pow (2, 8) --> 256


math.rad (v)

Convert v from degrees to radians. Note that math.deg is the inverse operation.


math.rad (60) --> 1.0471975511966
assert (math.pi / 180 == math.rad (1))   --> (they are the same)


math.random (n, u)

With no arguments, returns a random number in the range [0, 1). That is, zero up to but excluding 1.
With 1 argument, returns an integer in the range [1, n]. That is from 1 up to and including n.
With 2 arguments, returns an integer in the range [n, u]. That is from n up to and including u.
Use math.randomseed to seed the generator.


math.random () --> 0.30195013275552 (for example)
math.random (5)  --> (a number from 1 to 5)
math.random (4, 8)  --> (a number from 4 to 8)


math.randomseed ()

Seeds the random number generator with an integer. Re-seed with the same number to regenerate the same sequences by calling math.random.


math.randomseed (44)


math.sin (v)

Sine of v radians.


math.sin ( math.rad (30)) --> 0.5


math.sinh (v)

Hyperbolic sine of v radians.


math.sinh ( math.rad (30)) --> 0.54785347388804


math.sqrt ()

Square root of v.


math.sqrt (16) --> 4


math.tan (v)

Tangent of v radians.


math.tan( math.rad (45)) --> 1


math.tanh (v)

Hyperbolic tangent of v radians.


math.tanh( math.rad (45)) --> 0.65579420263267



See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_coroutines Lua coroutine functions
DOC_lua_debug Lua debug functions
DOC_lua_io Lua io functions
DOC_lua_os Lua os functions
DOC_lua_package Lua package functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions

(Help topic: general=lua_math)

DOC_contents Documentation contents page