gfm

Useful math functions and range-based statistic computations.

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


pure nothrow @nogc T  degrees(T)(T x) if (!isIntegral!T);

Convert from radians to  degrees.


pure nothrow @nogc T  radians(T)(T x) if (!isIntegral!T);

Convert from degrees to  radians.


pure nothrow @nogc S  lerp(S, T)(S a, S b, T t) if (is(typeof(t * b + (1 - t) * a) : S));

Linear interpolation, akin to GLSL's mix.


pure nothrow @nogc T  clamp(T)(T x, T min, T max);

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


nothrow @nogc long  ltrunc(real x);

Integer truncation.


nothrow @nogc long  lfloor(real x);

Integer flooring.


nothrow @nogc T  fract(T)(real x);

Returns
Fractional part of x.

pure nothrow @nogc T  safeAsin(T)(T x);

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


pure nothrow @nogc T  safeAcos(T)(T x);

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


pure nothrow @nogc T  step(T)(T edge, T x);

Same as GLSL  step function.

0.0 is returned if x < edge, and 1.0 is returned otherwise.


pure nothrow @nogc T  smoothStep(T)(T a, T b, T t);

Same as GLSL smoothstep function.

See:
http:
//en.wikipedia.org/wiki/Smoothstep

pure nothrow @nogc bool  isPowerOf2(T)(T i) if (isIntegral!T);

Returns
true of i is a power of 2.

nothrow @nogc int  ilog2(T)(T i) if (isIntegral!T);

Integer log2

TODO:
use bt intrinsics

pure nothrow @nogc int  nextPowerOf2(int i);

Computes next power of 2.


pure nothrow @nogc long  nextPowerOf2(long i);

Computes next power of 2.


pure nothrow @nogc T  sinOverX(T)(T x);

Computes sin(x)/x accurately.

See Also
www.plunk.org/~hatch/rightway.php

pure nothrow @nogc T  moduloWrap(T)(T a, T b) if (isSigned!T);

Signed integer modulo a/b where the remainder is guaranteed to be in [0..b[,

even if a is negative. Only support positive dividers.


pure nothrow @nogc int  solveLinear(T)(T a, T b, out T root) if (isFloatingPoint!T);

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

Returns
Number of roots.

pure nothrow @nogc int  solveQuadratic(T)(T a, T b, T c, T[] outRoots) if (isFloatingPoint!T);

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

Parameters
T[] outRoots array of root results, should have room for at least 2 elements.
Returns
Number of roots in outRoots.

pure nothrow @nogc int  solveCubic(T)(T a, T b, T c, T d, T[] outRoots) if (isFloatingPoint!T);

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

Parameters
T[] outRoots array of root results, should have room for at least 2 elements.
Returns
Number of roots in outRoots.
See Also
www.codeguru.com/forum/archive/index.php/t-265551.html

pure nothrow @nogc int  solveQuartic(T)(T a, T b, T c, T d, T e, T[] roots) if (isFloatingPoint!T);

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

Returns number of roots. roots slice should have room for up to 4 elements.

Known Bugs
doesn't pass unit-test!
See Also
mathworld.wolfram.com/QuarticEquation.html

double  average(R)(R r) if (isInputRange!R);

Arithmetic mean.


double  minElement(R)(R r) if (isInputRange!R);

Minimum of a range.


double  maxElement(R)(R r) if (isInputRange!R);

Maximum of a range.


double  variance(R)(R r) if (isForwardRange!R);

Variance of a range.


double  standardDeviation(R)(R r) if (isForwardRange!R);

Standard deviation of a range.


pure nothrow @nogc T  inverseSqrt(T)(T x) if (isFloatingPoint!T);

SSE approximation of reciprocal square root.