Lua max()
Lua’s math.max() function is a standard function that is a part of the math library, and it is the best solution for determining the maximum value from a list of numerical inputs, regardless of the number of values in this list. Whether the task is just simple arithmetic or a complex mathematical algorithm, math.max() is a versatile and fundamental helper function for a variety of cases.
Note:
math.min()is the opposing function, which will return the minimum value among a set of numerical values.
Syntax
Since max() is a method from Lua’s library math, it is called as math.max().
math.max(value1, value2, value3, .....)
math.max() returns the maximum value among the input numerical values.
Example 1
To find the highest value among three variables:
a = 22b = 23c = 24print(math.max(a,b,c))
This results in the following output:
24
Example 2
In a comparison of two equal values the function will still return a single number:
a = 10b = 10print(math.max(a,b))
This results in the following output:
10
Example 3
It also works with negative values:
a = -6b = -10print(math.max(a,b))
This results in the following output:
-6
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Lua on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
- Beginner Friendly.4 hours