# Lua/Math Library

< Lua

Lua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code.[1] This lesson will show you how to use the Lua Math library in your scripts.

## Prerequisites

This lesson assumes you have already completed the Frame Object lesson.

## Create a Lua Script that Uses the Math Library

To create a Lua script that uses the Math library:

1. Navigate to Module:Sandbox.
2. Clear all existing code.
It's a sandbox. Everyone is free to play in the sandbox. But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
3. Add the following code and save the page:
local p = {}

function p.abs(frame)
local x = frame.args[1]
return ';abs\n:math.abs(' .. x .. ') is ' .. math.abs(x) .. '\n'
end

function p.acos(frame)
local x = frame.args[1]
return ';acos\n:math.acos(' .. x .. ') is ' .. math.acos(x) .. '\n'
end

function p.asin(frame)
local x = frame.args[1]
return ';asin\n:math.asin(' .. x .. ') is ' .. math.asin(x) .. '\n'
end

function p.atan(frame)
local x = frame.args[1]
return ';atan\n:math.atan(' .. x .. ') is ' .. math.atan(x) .. '\n'
end

function p.atan2(frame)
local y = frame.args[1]
local x = frame.args[2]
return ';atan2\n:math.atan2(' .. y .. ', ' .. x .. ') is ' .. math.atan2(y, x) .. '\n'
end

function p.ceil(frame)
local x = frame.args[1]
return ';ceil\n:math.ceil(' .. x .. ') is ' .. math.ceil(x) .. '\n'
end

function p.cos(frame)
local x = frame.args[1]
return ';cos\n:math.cos(' .. x .. ') is ' .. math.cos(x) .. '\n'
end

function p.cosh(frame)
local x = frame.args[1]
return ';cosh\n:math.cosh(' .. x .. ') is ' .. math.cosh(x) .. '\n'
end

function p.deg(frame)
local x = frame.args[1]
return ';deg\n:math.deg(' .. x .. ') is ' .. math.deg(x) .. '\n'
end

function p.exp(frame)
local x = frame.args[1]
return ';exp\n:math.exp(' .. x .. ') is ' .. math.exp(x) .. '\n'
end

function p.floor(frame)
local x = frame.args[1]
return ';floor\n:math.floor(' .. x .. ') is ' .. math.floor(x) .. '\n'
end

function p.fmod(frame)
local x = frame.args[1]
local y = frame.args[2]
return ';fmod\n:math.fmod(' .. x .. ', ' .. y .. ') is ' .. math.fmod(x, y) .. '\n'
end

function p.frexp(frame)
local x = frame.args[1]
return ';frexp\n:math.frexp(' .. x .. ') is ' .. math.frexp(x) .. '\n'
end

function p.huge()
return ';huge\n:math.huge is ' .. math.huge .. '\n'
end

function p.ldexp(frame)
local m = frame.args[1]
local e = frame.args[2]
return ';ldexp\n:math.ldexp(' .. m .. ', ' .. e .. ') is ' .. math.ldexp(m, e) .. '\n'
end

function p.log(frame)
local x = frame.args[1]
return ';log\n:math.log(' .. x .. ') is ' .. math.log(x) .. '\n'
end

function p.log10(frame)
local x = frame.args[1]
return ';log10\n:math.log10(' .. x .. ') is ' .. math.log10(x) .. '\n'
end

function p.max(frame)
local x = frame.args[1]
local y = frame.args[2]
return ';max\n:math.max(' .. x .. ', ' .. y .. ') is ' .. math.max(x, y) .. '\n'
end

function p.min(frame)
local x = frame.args[1]
local y = frame.args[2]
return ';min\n:math.min(' .. x .. ', ' .. y .. ') is ' .. math.min(x, y) .. '\n'
end

function p.modf(frame)
local x = frame.args[1]
return ';modf\n:math.modf(' .. x .. ') is ' .. math.modf(x) .. '\n'
end

function p.pi()
return ';pi\n:math.pi is ' .. math.pi .. '\n'
end

function p.pow(frame)
local x = frame.args[1]
local y = frame.args[2]
return ';pow\n:math.pow(' .. x .. ', ' .. y .. ') is ' .. math.pow(x, y) .. '\n'
end

local x = frame.args[1]
end

function p.random(frame)
local m = frame.args[1]
local n = frame.args[2]
return ';random\n:math.random(' .. m .. ', ' .. n .. ') is ' .. math.random(m, n) .. '\n'
end

function p.randomseed(frame)
local m = frame.args[1]
local n = frame.args[2]
math.randomseed(os.time())
return ';randomseed\n:math.random(' .. m .. ', ' .. n .. ') is ' .. math.random(m, n) .. '\n'
end

function p.sin(frame)
local x = frame.args[1]
return ';sin\n:math.sin(' .. x .. ') is ' .. math.sin(x) .. '\n'
end

function p.sinh(frame)
local x = frame.args[1]
return ';sinh\n:math.sinh(' .. x .. ') is ' .. math.sinh(x) .. '\n'
end

function p.sqrt(frame)
local x = frame.args[1]
return ';sqrt\n:math.sqrt(' .. x .. ') is ' .. math.sqrt(x) .. '\n'
end

function p.tan(frame)
local x = frame.args[1]
return ';tan\n:math.tan(' .. x .. ') is ' .. math.tan(x) .. '\n'
end

function p.tanh(frame)
local x = frame.args[1]
return ';tanh\n:math.tanh(' .. x .. ') is ' .. math.tanh(x) .. '\n'
end

return p

1. Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
2. Add the following code and save the page:
{{#invoke:Sandbox|abs|-1}}
{{#invoke:Sandbox|acos|1}}
{{#invoke:Sandbox|asin|1}}
{{#invoke:Sandbox|atan|1}}
{{#invoke:Sandbox|atan2|1|1}}
{{#invoke:Sandbox|ceil|1.5}}
{{#invoke:Sandbox|cos|0.78539816339745}}
{{#invoke:Sandbox|cosh|0.78539816339745}}
{{#invoke:Sandbox|deg|0.78539816339745}}
{{#invoke:Sandbox|exp|1}}
{{#invoke:Sandbox|floor|1.5}}
{{#invoke:Sandbox|fmod|5|3}}
{{#invoke:Sandbox|frexp|1}}
{{#invoke:Sandbox|huge}}
{{#invoke:Sandbox|ldexp|1|2}}
{{#invoke:Sandbox|log| 2.718281828459}}
{{#invoke:Sandbox|log10|100}}
{{#invoke:Sandbox|max|1|2}}
{{#invoke:Sandbox|min|1|2}}
{{#invoke:Sandbox|modf|1.5}}
{{#invoke:Sandbox|pi}}
{{#invoke:Sandbox|pow|10|2}}
{{#invoke:Sandbox|random|1|6}}
{{#invoke:Sandbox|randomseed|1|6}}
{{#invoke:Sandbox|sin|0.78539816339745}}
{{#invoke:Sandbox|sinh|0.78539816339745}}
{{#invoke:Sandbox|sqrt|100}}
{{#invoke:Sandbox|tan|0.78539816339745}}
{{#invoke:Sandbox|tanh|0.78539816339745}}

The result should be similar to:

abs
math.abs(-1) is 1
acos
math.acos(1) is 0
asin
math.asin(1) is 1.5707963267949
atan
math.atan(1) is 0.78539816339745
atan2
math.atan2(1, 1) is 0.78539816339745
ceil
math.ceil(1.5) is 2
cos
math.cos(0.78539816339745) is 0.70710678118655
cosh
math.cosh(0.78539816339745) is 1.324609089252
deg
math.deg(0.78539816339745) is 45
exp
math.exp(1) is 2.718281828459
floor
math.floor(1.5) is 1
fmod
math.fmod(5, 3) is 2
frexp
math.frexp(1) is 0.5
huge
math.huge is inf
ldexp
math.ldexp(1, 2) is 4
log
math.log( 2.718281828459) is 0.99999999999998
log10
math.log10(100) is 2
max
math.max(1, 2) is 2
min
math.min(1, 2) is 1
modf
math.modf(1.5) is 1
pi
math.pi is 3.1415926535898
pow
math.pow(10, 2) is 100
random
math.random(1, 6) is 2
randomseed
math.random(1, 6) is 1
sin
math.sin(0.78539816339745) is 0.70710678118655
sinh
math.sinh(0.78539816339745) is 0.86867096148601
sqrt
math.sqrt(100) is 10
tan
math.tan(0.78539816339745) is 1
tanh
math.tanh(0.78539816339745) is 0.65579420263267

1. math.abs(x) returns the absolute value of x returns .
2. math.acos(x) returns the arc cosine of x (given in radians).
3. math.asin(x) returns the arc sine of x (given in radians).
4. math.atan(x) returns the arc tangent of x (given in radians).
5. math.atan2(y, x) returns the arc tangent of y/x (given in radians), using the signs of both parameters to find the quadrant of the result.
6. math.ceil(x) returns the smallest integer larger than or equal to x.
7. math.cos(x) returns the cosine of x (given in radians).
8. math.cosh(x) returns the hyperbolic cosine of x.
9. math.deg(x) returns the angle x (given in radians) in degrees.
10. math.exp(x) returns the value e^x.
11. math.floor(x) returns the largest integer smaller than or equal to x.
12. math.fmod(x, y) returns the remainder of the division of x by y that rounds the quotient towards zero.
13. math.frexp(x) returns two values m and e such that x = m times 2^e, e is an integer, and the absolute value of m is in the range [0.5, 1)
14. math.huge returns the value representing positive infinity; larger than or equal to any other numerical value.
15. math.ldexp(m, e) returns m times 2^e (e should be an integer).
16. math.log(x) returns the natural logarithm of x.
17. math.log10(x) returns the base-10 logarithm of x.
18. math.max(x, y) returns the maximum value among its arguments.
19. math.min(x, y) returns the minimum value among its arguments.
20. math.modf(x) returns two numbers, the integral part of x and the fractional part of x.
21. math.pi returns the value of pi.
22. math.pow(x, y) returns x^y.
24. math.random(m, n) returns a pseudo-random integer in the range [m,n].
Note that unless randomseed is called first, the random number sequence will be the same every time, meaning not random.
25. math.randomseed(os.time()) seeds the random number generator with the current server operating system elapsed time in seconds.
26. math.sin(x) returns the sine of x (given in radians).
27. math.sinh(x) returns the hyperbolic sine of x.
28. math.sqrt(x) returns the square root of x.
29. math.tan(x) returns the tangent of x (given in radians).
30. math.tanh(x) returns the hyperbolic tangent of x.

## Conclusion

Congratulations! You've now created, tested, and understood a Lua script that uses the Math library. Return to the main Lua page to learn about other Lua code libraries.