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

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:

  1. x number
  2. y number

Returns:

  1. 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:

  1. x1 number
  2. x2 number (optional)
  3. x3 number (optional)
  4. xn number and so on (optional)

Returns:

  1. number diagonal

math.clamp()

Returns x clamped to min and max boundaries.

( number x, number min, number max )

Returns:

  1. number clamped

math.sgn(x)

Returns 0 if x == 0, 1 if x > 0, -1 if x < 0

Parameters:

  1. x number

Returns:

  1. number sign

math.mix(x, y, a)

Returns linear interpolation between x and y with ratio a (x+(y-x)*a).

Parameters:

  1. x number
  2. y number
  3. a number

Returns:

  1. 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:

  1. x number
  2. decimals number

Returns:

  1. number rounded

math.erf(x)

Returns erf(x), the Gauss error function, between -1 and 1.

Parameters:

  1. x number

Returns:

  1. 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:

  1. edge0 number
  2. edge1 number
  3. v number

Returns:

  1. number smoothstep