Lua MathExtra
From Spring
Jump to navigationJump to searchDevelopment < Lua Scripting < Lua MathExtra
Additional math functions
New in Engine Version 0.95 and up.
math.clamp
( number x,
number min,
number max )
return: number clamped
- Returns x clamped to min and max boundaries.
math.diag
( number x1,
number x2,
number x3, ... )
return: number diagonal
- Returns the length of the diagonal of an n-dimensional box (or the length of an n-component vector). Rather quick method that does not handle intermediate overflows/underflows nor is made for numerical stability.
math.erf
( number x )
return: number erf
- Returns erf(x).
math.hypot
( number x,
number y )
return: number sqrt(x*x+y*y)
- Returns the length of hypotenuse of right angle triangle with sides x and y, equivalent to sqrt(x*x + y*y), but has better numerical stability and internally handles intermediate overflows/underflows, but is also slower.
math.mix
( number x,
number y,
number a )
return: number (x+(y-x)*a)
- Returns linear interpolation between x and y with ratio a (x+(y-x)*a).
math.round
( number x,
number decimals )
return: number rounded
- Returns x rounded to n decimals, if n is omitted or <=0, rounds to nearest integer. Note that Spring's Lua interpreter uses 32-bit floats for all numbers so max. precision is ~7 decimal digits.
math.sgn
( number x )
return: number sign
- Returns 0 if x ==0, 1 if x > 0, -1 if x < 0 sgn(x).
math.smoothstep
( number edge0,
number edge1,
number x )
return: number smoothstep
- Returns smoothstep(edge0, edge1, v).