# gfm.math.funcs

Useful math functions and range-based statistic computations.

If you need real statistics, consider using the Dstats library.

## Members

### Functions

average
double average(R r)

Arithmetic mean.

clamp
T clamp(T x, T min, T max)

Clamp x in [min, max], akin to GLSL's clamp.

degrees
T degrees(T x)

fract
T fract(real x)
ilog2
int ilog2(T i)

Integer log2 TODO: use bt intrinsics

inverseSqrt
T inverseSqrt(T x)

SSE approximation of reciprocal square root.

isPowerOf2
bool isPowerOf2(T i)
lerp
S lerp(S a, S b, T t)

Linear interpolation, akin to GLSL's mix.

lfloor
long lfloor(real x)

Integer flooring.

ltrunc
long ltrunc(real x)

Integer truncation.

maxElement
double maxElement(R r)

Maximum of a range.

minElement
double minElement(R r)

Minimum of a range.

moduloWrap
T moduloWrap(T a, T b)

Signed integer modulo a/b where the remainder is guaranteed to be in [0..b[, even if a is negative. Only support positive dividers.

nextPowerOf2
int nextPowerOf2(int i)

Computes next power of 2.

nextPowerOf2
long nextPowerOf2(long i)

Computes next power of 2.

safeAcos
T safeAcos(T x)

Safe acos: input clamped to [-1, 1]

safeAsin
T safeAsin(T x)

Safe asin: input clamped to [-1, 1]

sinOverX
T sinOverX(T x)

Computes sin(x)/x accurately.

smoothStep
T smoothStep(T a, T b, T t)

Same as GLSL smoothstep function. See: http://en.wikipedia.org/wiki/Smoothstep

solveCubic
int solveCubic(T a, T b, T c, T d, T[] outRoots)

Finds the roots of a cubic polynomial a + b x + c x^2 + d x^3 = 0

solveLinear
int solveLinear(T a, T b, out T root)

Find the root of a linear polynomial a + b x = 0

int solveQuadratic(T a, T b, T c, T[] outRoots)

Finds the root roots of a quadratic polynomial a + b x + c x^2 = 0

solveQuartic
int solveQuartic(T a, T b, T c, T d, T e, T[] roots)

Returns the roots of a quartic polynomial a + b x + c x^2 + d x^3 + e x^4 = 0

standardDeviation
double standardDeviation(R r)

Standard deviation of a range.

step
T step(T edge, T x)

Same as GLSL step function. 0.0 is returned if x < edge, and 1.0 is returned otherwise.

variance
double variance(R r)

Variance of a range.