## Introduction to Lua math

In Lua, math is defined as a standard library that consists of mathematical functions that are used in Lua programs for dealing with mathematical concepts easily by declaring this library in the program. In general, Lua math is a standard library provided by Lua programming language to work with mathematical concepts using the functions that are defined in this library for solving mathematical related problems such as logarithmic functions (log), exponential functions (exp), trigonometric functions (sin, cos, tan, etc), rounding functions (floor, ceil) and many other functions like max, min, pi, etc are defined within this Lua math library which is an interface to standard C math library.

**Syntax:**

`math.function_name(argument_list or parameter);`

In the above syntax, we can when we want to use any math functions then we need to use math library provided by Lua standard library and to define or use any kind of math functions then we have to use math library starting with math then followed by a dot and then followed by the function name along with the parameters that need to be declared or used in the function for calculations.

### Working of math library in Lua programming language

The Lua math library can be included directly with its function name and the parameters are passed to this function to include the variable values that are needed to calculate according to the function defined. In Lua, there are various functions for different mathematical calculations in the math library and a few of them are listed in the below section. But before that, we will see a list of math functions that are defined within this math library.

math.max (x, y, z, ….) => this math function is used when a developer wants to find the maximum among the given numbers in the parameters that are passed to this function.

math.min (x, y, z, ….) => this math function is used when a developer wants to find the minimum among the given numbers which are passed as parameters to the function. These max and min functions are known as comparative functions in the math library.

math.sin(x), math.cos(x), math.tan(x), math.asin(x), math.acos(x), etc are some of the trigonometric functions for getting sine, cosine and tangent for radiant values that are passed as parameter to the function and asin and acos are for getting arc sine and arc cosine in radians for the given values in the parameter.

In Lua, the math library also provides random functions such as math.random([x [,y]]) to get random numbers.

There are many other functions in the library for rounding the numbers such as math, floor(x), math.ceil(x), some exponential and log functions are math.exp(x), math.log(x).

41 Online Courses | 13 Hands-on Projects| 322+ Hours| Verifiable Certificate of Completion

4.5

View Course

**Example:**

In the below example let us see few math functions demonstration using math library.

`print( "Demonstration of math library function in Lua programming language ")`

print("\n")

print("Rounding functions in math library are as follows:")

a = 89.50983

print(" The floor value of a is ", math.floor(a))

print(" The ceil value of a is ", math.ceil(a))

print("\n")

print(" Comparative function are as follows: ")

print(" The maximum of the given numbers is : ", math.max(30, 46, 29, 78, 56, 9))

print(" The minimum of the given numbers is : ",math.min(30, 46, 29, 78, 56,9))

print("\n")

print(" The trigonometric functions provided in the math library is as follows: ")

print("\n")

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

print("The pi value for calculating trigonometric values is : ", p)

print("\n")

print("The sine value of the given radian which is 90 degree is : ", string.format("%.1f ", math.sin(p)))

print("The cosine value of the given radian which is 90 degree is : ", string.format("%.1f ", math.cos(p)))

print("The tangent value of the given radian which is 90 degree is : ", string.format("%.1f ", math.tan(p)))

print("The sine hyperbolic value of the given radian which is 90 degree is : ", string.format("%.1f ", math.sinh(p)))

print("The cosine hyperbolic value of the given radian which is 90 degree is : ", string.format("%.1f ", math.cosh(p)))

print("The tangent hyperbolic value of the given radian which is 90 degree is : ", string.format("%.1f ", math.tanh(p)))

**Output:**

In the above program, we can see some of the math functions that are defined using the math library in the program. In the first we are finding the rounding of functions such as math.floor() which will round of to the previous value and math.ceil() will round off the value to the next value of the given number. Then we are defining comparative functions such as max and min which will display the maximum and minimum number in the given list of the numbers as parameters to this function. Then we have defined few trigonometric functions and to display the values for the radian with 90 degrees where we have converted the pi value to the radian value using rad() such as math.rad(math.pi / 2), whereas if we want to display the degree value then we can use deg() function as math.deg(math.pi / 2). In the above program, we have displayed the values of sine, cosine, and tangent along with its hyperbolic values also. The output of all these functions can be seen in the above screenshot.

Let us see another simple example with a few common math functions in the below section.

**Example:**

`print( "Demonstration of some other math library function in Lua programming language ")`

print("\n")

print("The squareroot of given number a is :", math.sqrt(24))

print("\n")

print("The 2 to the power of 5 is :", math.pow(2,5))

print("\n")

print("The absolute value of the given number is :", math.abs(-45))

print("\n")

print("The exponential value of the given number is :", math.exp(4))

print("\n")

print("The logarithmic value of the given number is :",math.log(2))

print("\n")

print("Random number between 70 and 80 is ",math.random(70,80))

**Output:**

In the above program, we can see we have defined some other math function provided by the math library such as math.sqrt() will display the square root of the specified number, math.pow() will specify the powers of the given number in the above program, it is 2 ^ 5 =32, then we saw math.abs() which will display the absolute value which will always display positive number, then math.exp() will display the exponential value, math.log() displays logarithmic values, then we have also defined the math.random() function to display the random number. The output in the above screenshot will show the result of these functions respectively.

### Conclusion

In this article, we conclude that Lua provides a standard library that contains mathematical functions for some mathematical calculations in the program and it is provided by the math library. In this, we don’t need to separately import or include the math library we can directly use it with the math function with a dot followed by the math function name. Therefore unlike other programming languages, this also has a simple math library for mathematical operations.

### Recommended Articles

This is a guide to Lua math. Here we discuss the Working of the math library in Lua programming language along with the examples. You may also have a look at the following articles to learn more –