Lua/Math Library

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. 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.
 * 1) Add the following code and save the page:

Test Your Lua Script
To test your Lua script:
 * 1) Navigate to either the Module_talk:Sandbox page, the Sandbox page, or your own user or sandbox page.
 * 2) Add the following code and save the page:

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


 * rad
 * math.rad(45) is 0.78539816339745


 * 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

Understand Your Lua Script
To understand your Lua script:
 * 1)   returns the absolute value of   returns.
 * 2)   returns the arc cosine of   (given in radians).
 * 3)   returns the arc sine of   (given in radians).
 * 4)   returns the arc tangent of   (given in radians).
 * 5)   returns the arc tangent of   (given in radians), using the signs of both parameters to find the quadrant of the result.
 * 6)   returns the smallest integer larger than or equal to.
 * 7)   returns the cosine of   (given in radians).
 * 8)   returns the hyperbolic cosine of.
 * 9)   returns the angle   (given in radians) in degrees.
 * 10)   returns the value.
 * 11)   returns the largest integer smaller than or equal to.
 * 12)   returns the remainder of the division of   by   that rounds the quotient towards zero.
 * 13)   returns two values m and e such that   = m times 2^e, e is an integer, and the absolute value of m is in the range [0.5, 1)
 * 14)   returns the value representing positive infinity; larger than or equal to any other numerical value.
 * 15)   returns   times   (  should be an integer).
 * 16)   returns the natural logarithm of.
 * 17)   returns the base-10 logarithm of.
 * 18)   returns the ma imum value among its arguments.
 * 19)   returns the minimum value among its arguments.
 * 20)   returns two numbers, the integral part of   and the fractional part of.
 * 21)   returns the value of pi.
 * 22)   returns.
 * 23)   returns the angle   (given in degrees) in radians.
 * 24)   returns a pseudo-random integer in the range.
 * Note that unless randomseed is called first, the random number sequence will be the same every time, meaning not random.
 * 1)   seeds the random number generator with the current server operating system elapsed time in seconds.
 * 2)   returns the sine of   (given in radians).
 * 3)   returns the hyperbolic sine of.
 * 4)   returns the square root of.
 * 5)   returns the tangent of   (given in radians).
 * 6)   returns the hyperbolic tangent of.

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.