math.max() function in Lua programming


There are several occurrences when we want to get the max value from a given series of numbers and then use that value later on.

The Maximum value from the series of different numbers is the value that is the maximum of all the numbers that are present in that series.

Lua provides us with a math.max() function that we can use to find the max value out of different numbers that we pass to it as arguments.

Example

Let’s consider a simple example where we will make use of the math.max() function in Lua −

 Live Demo

a = 10
b = 11
c = 12
print(math.max(a,b,c))

Output

12

It should be noted that if we try to pass the same numbers to the math.max() function then the output will be the same number itself.

Example

Consider the example shown below −

 Live Demo

d = 11
e = 11
print(math.max(d,e))

Output

11

We can also pass negative numbers in the math.max() function as an argument.

Consider the example shown below −

Example

 Live Demo

f = -10
g = -5
print(math.max(f,g))

Output

-5

Updated on: 19-Jul-2021

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements