mid( first, second, third )
- Returns the middle of three numbers.
- first
-
- The first number.
- second
-
- The second number.
- third
-
- The third number.
This is a straightforward function with an often-overlooked use: clamping a value to a range.
If you call mid(min_val,val_to_clamp,max_val)
, it acts exactly like the similarly-formatted clamp()
function in other languages. The result will be min_val
if val is too low, max_val
if val is too high, and val
itself if it is in range.
Examples
print(mid(8, 2, 4)) -- 4
print(mid(-3.5, -3.4, -3.6)) -- -3.5
print(mid(6, 6, 8)) -- 6
-- clamp to 0..1
print(mid(0, v, 1))