Module MathExtra
math extensions
See also:
Functions
- math.hypot
- Returns the length of hypotenuse of right angle triangle with sides x and y, equivalent to sqrt(xx + yy), but has better numerical stability and internally handles intermediate overflows/underflows, but is also slower.
- math.diag
- Returns the length of the diagonal of an n-dimensional box (or the length of an n-component vector).
- math.clamp
- Returns x clamped to min and max boundaries.
- math.sgn
- Returns 0 if x == 0, 1 if x > 0, -1 if x < 0
- math.mix
- Returns linear interpolation between x and y with ratio a (x+(y-x)*a).
- math.round
- Returns x rounded to n decimals, if n is omitted or <=0, rounds to nearest integer.
- math.erf
- Returns erf(x), the Gauss error function, between -1 and 1.
- math.smoothstep
- Applies the smoothstep function
- math.normalize
- Returns the normalize vector of an given vector.
Functions
math.hypot(x, 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.
Parameters:
-
x
number -
y
number
Returns:
- number sqrt(xx+yy)
math.diag(x1[, x2[, x3[, xn]]])
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.
Parameters:
-
x1
number -
x2
number (optional) -
x3
number (optional) -
xn
number and so on (optional)
Returns:
- number diagonal
math.clamp()
Returns x clamped to min and max boundaries.
( number x, number min, number max )
Returns:
- number clamped
math.sgn(x)
Returns 0 if x == 0, 1 if x > 0, -1 if x < 0
Parameters:
-
x
number
Returns:
- number sign
math.mix(x, y, a)
Returns linear interpolation between x and y with ratio a (x+(y-x)*a).
Parameters:
-
x
number -
y
number -
a
number
Returns:
- number (x+(y-x)*a)
math.round(x, decimals)
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.
Parameters:
-
x
number -
decimals
number
Returns:
- number rounded
math.erf(x)
Returns erf(x), the Gauss error function, between -1 and 1.
Parameters:
-
x
number
Returns:
- number erf
math.smoothstep(edge0, edge1, v)
Applies the smoothstep function
Clamps and rescales v to a value between [0; 1] based on the edges and then applies the smoothstep function. For example math.smoothstep(10, 25, 15) is 0.259, because 15 is 0.333 of the way from 10 to 25, and smoothstep(0.333) is 0.259
Parameters:
-
edge0
number -
edge1
number -
v
number
Returns:
- number smoothstep
math.normalize(x1[, x2[, xN]])
Returns the normalize vector of an given vector.
Parameters:
-
x1
number -
x2
number (optional) -
xN
number and so on (optional)
Returns:
- number normalized1
- number normalized2 and so on