Extra math functions (extends Lua's math library)
More...
These are added to Lua's standard math library, so math.floor(), math.random(), etc. still work like in normal Lua (see the Lua manual).
|
| number | math.range (number num, number min, number max) |
| |
| boolean | math.inrange (number num, number min, number max) |
| |
| number | math.round (number num, number decimals) |
| |
◆ math.inrange()
| boolean math.inrange |
( |
number | num, |
|
|
number | min, |
|
|
number | max ) |
Checks if a number is between a minimum and a maximum (both included)
- Parameters
-
| num | The number |
| min | The minimum |
| max | The maximum |
- Returns
true if min <= num <= max
- Example:
-- Is the touch inside a button?
if math.inrange(touch.x, 100, 300) and math.inrange(touch.y, 50, 100) then ... end
◆ math.range()
| number math.range |
( |
number | num, |
|
|
number | min, |
|
|
number | max ) |
Keeps a number between a minimum and a maximum (also known as "clamp")
- Parameters
-
| num | The number |
| min | The minimum |
| max | The maximum |
- Returns
min if num is smaller than it, max if num is bigger than it, otherwise num
- Example:
volume = math.range(volume + 10, 0, 100) -- never goes over 100
◆ math.round()
| number math.round |
( |
number | num, |
|
|
number | decimals ) |
Rounds a number to the nearest whole number, or to a number of decimal places
- Parameters
-
| num | The number |
| decimals | How many decimal places to keep (optional, default: 0) |
- Returns
- The rounded number
- Example:
print(math.round(2.5)) -- 3
print(math.round(3.14159, 2)) -- 3.14