Last Updated:

Lua Math Library

Lua Math

Lua is a scripting programming language. It is convenient, lightweight and fast. The main purpose of its creation is the database programming language. Therefore, all programming in Lua boils down to different manipulations with tables.

Tables are a set of structured data where each key has a corresponding value. The ease of embedding and the simple syntax of Lua in the application have ensured wide distribution in various fields. For example, mathematical operations in engineering and scientific calculations are often required, where the Lua standard library will come in handy.

Functions in the Mathematical Library

Function NameFunction Description
math.abs (x)Returns the absolute value of x
math.cos (x)Returns cosine x (assumed in radians).Returns arccosine from x (in radians)
math.asin (x)Returns arksinus x (in radians)
Mathematics (x)Returns arctangent x (in radians)
math.atan2 (y, x)

 

Returns the arctangent y / x (in radians), but applies the signs of both parameters to find the quadrant of the result. (It also correctly handles the case where x is zero.)
math.hugeThe value HUGE_VAL, the value is equal to or greater than any other numeric value.
math.acos (x)Returns arcosine from x (in radians)
math.cosh (x)Returns the hyperbolic cosine x
math.deg (x)Returns the angle x (in radians) in degrees
math.exp (x)Returns e power x
math.floor (x)Returns the largest integer equal to or less than x.
math.fmod (x, y)Returns the remainder of dividing x by y, which rounds the quotient to zero
math.pow (x, y)

 

 

Returns hu. (You can also use the expression x^y to calculate this value.)
math.ceil (x)Returns the smallest integer equal to or greater than x.
math.ldexp (m, e)Returns m2e (e must be an integer)
math.log (x)Returns the natural logarithm x
math.log10 (x)Returns base-10 logarithm x
math.max (x, ...)Returns the maximum value among its arguments.
math.min (x, ...)Returns the minimum value among its arguments.
math.modf (x)Returns two numbers, an integral part of x and a fractional part of x.
math.piThe meaning of pi.
math.frexp (x)Returns m and e, such that x = m2e, e is an integer, and the absolute value of m is in the range [0,5, 1) (or zero when x is zero).
math.rad (x)Returns the angle x (in degrees) in radians
math.random ([m [, n]])

 

 

This function acts as an interface to a simple pseudorandom random rand generator function provided by ANSI C. When called without arguments, returns a uniform pseudorandom real number in the range [0,1). When called with an integer m, math.random returns a uniform pseudorandom integer in the range [1, m]. When called with two integers m and n, math.random returns a uniform pseudorandom integer in the range [m, n].
mathematics. random seed (x)

 

 

Sets x as the "starting number" for a pseudo-random generator: equal initial numbers create equal sequences of numbers.
Mathematics (x)Returns sine x (assumed in radians).
math.sinh (x)Returns the hyperbolic sine x
math.sqrt (x)

 

 

Returns the square root of x. (To calculate this value, you can use the expression x^0.5)
Mathematics (x)Returns the tangent x (assumed in radians)
Mathematics (x)Returns the hyperbolic tangent x

Trigonometric functions

An example of using the trigonometric function:

radianVal = math.rad(math.pi /2)

io.write(radianVal,»\n»)

—Sin value of 90(math.pi /2) degrees
io.write(string.format(«%.1f «, math.sin(radianVal)),»\n»)

—Cos value of 90(math.pi /2) degrees
io.write(string.format(«%.1f «, math.cos(radianVal)),»\n»)

—Tan value of 90(math.pi /2) degrees
io.write(string.format(«%.1f «, math.tan(radianVal)),»\n»)

—Cosh value of 90(math.pi /2) degrees
io.write(string.format(«%.1f «, math.cosh(radianVal)),»\n»)

—PiValuein degrees
io.write(math.deg(math.pi),»\n»)

When you run the program, you will get the following:

0.027415567780804
0.0
1.0
0.0
1.0
180

Other common math functions

An example of using common mathematical functions:

—Floor
io.write(«Floor of 10.5055 is «, math.floor(10.5055),»\n»)

—Ceil
io.write(«Ceil of 10.5055 is «, math.ceil(10.5055),»\n»)

—Square root
io.write(«Square root of 16 is «,math.sqrt(16),»\n»)

—Power
io.write(«10 power 2 is «,math.pow(10,2),»\n»)
io.write(«100 power 0.5 is «,math.pow(100,0.5),»\n»)

—Absolute
io.write(«Absolute value of -10 is «,math.abs(-10),»\n»)

—Random
math.randomseed(os.time())
io.write(«Random number between 1 and 100 is «,math.random(),»\n»)

—Random between 1 to 100
io.write(«Random number between 1 and 100 is «,math.random(1,100),»\n»)

—Max
io.write(«Maximum in the input array is «,math.max(1,100,101,99,999),»\n»)

—Min
io.write(«Minimum in the input array is «,math.min(1,100,101,99,999),»\n»)

When you run the program, you will get the following output:

Floor of 10.5055 is 10
Ceil of 10.5055 is 11
Square root of 16 is 4
10 power 2 is 100
100 power 0.5 is 10
Absolute value of -10 is 10
Random number between 1 and 100 is 0.22876674703207
Random number between 1 and 100 is 7
Maximum in the input array is 999
Minimum in the input array is 1

These are just a few examples out of many. The mathematical library can be used for a variety of purposes, so we recommend that you learn these functions to understand where they can be used.