Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

std.math.algebraic

This is a submodule of std.math.
It contains classical algebraic functions like abs, sqrt, and poly.
Authors:
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger
pure nothrow @nogc auto abs(Num)(Num x)
if (is(immutable(Num) == immutable(short)) || is(immutable(Num) == immutable(byte)) || is(typeof(Num.init >= 0)) && is(typeof(-Num.init)));
Calculates the absolute value of a number.
Parameters:
Num (template parameter) type of number
Num x real number value
Returns:
The absolute value of the number. If floating-point or integral, the return type will be the same as the input.

Limitations Does not work correctly for signed intergal types and value Num.min.

Examples:
ditto
import std.math.traits : isIdentical, isNaN;

assert(isIdentical(abs(-0.0L), 0.0L));
assert(isNaN(abs(real.nan)));
writeln(abs(-real.infinity)); // real.infinity
writeln(abs(-56)); // 56
writeln(abs(2321312L)); // 2321312L
pure nothrow @nogc @safe real fabs(real x);

pure nothrow @nogc @trusted double fabs(double d);

pure nothrow @nogc @trusted float fabs(float f);
Returns |x|
Special Values
x fabs(x)
±0.0 +0.0
±∞ +∞
Examples:
import std.math.traits : isIdentical;

assert(isIdentical(fabs(0.0f), 0.0f));
assert(isIdentical(fabs(-0.0f), 0.0f));
writeln(fabs(-10.0f)); // 10.0f

assert(isIdentical(fabs(0.0), 0.0));
assert(isIdentical(fabs(-0.0), 0.0));
writeln(fabs(-10.0)); // 10.0

assert(isIdentical(fabs(0.0L), 0.0L));
assert(isIdentical(fabs(-0.0L), 0.0L));
writeln(fabs(-10.0L)); // 10.0L
pure nothrow @nogc @safe float sqrt(float x);

pure nothrow @nogc @safe double sqrt(double x);

pure nothrow @nogc @safe real sqrt(real x);
Compute square root of x.
Special Values
x sqrt(x) invalid?
-0.0 -0.0 no
<0.0 NAN yes
+∞ +∞ no
Examples:
import std.math.operations : feqrel;
import std.math.traits : isNaN;

assert(sqrt(2.0).feqrel(1.4142) > 16);
assert(sqrt(9.0).feqrel(3.0) > 16);

assert(isNaN(sqrt(-1.0f)));
assert(isNaN(sqrt(-1.0)));
assert(isNaN(sqrt(-1.0L)));
nothrow @nogc @trusted real cbrt(real x);
Calculates the cube root of x.
Special Values
x cbrt(x) invalid?
±0.0 ±0.0 no
NAN NAN yes
±∞ ±∞ no
Examples:
import std.math.operations : feqrel;

assert(cbrt(1.0).feqrel(1.0) > 16);
assert(cbrt(27.0).feqrel(3.0) > 16);
assert(cbrt(15.625).feqrel(2.5) > 16);
pure nothrow @nogc @safe T hypot(T)(const T x, const T y)
if (isFloatingPoint!T);
Calculates the length of the hypotenuse of a right-angled triangle with sides of length x and y. The hypotenuse is the value of the square root of the sums of the squares of x and y:
sqrt(x2 + y2)
Note that hypot(x, y), hypot(y, x) and hypot(x, -y) are equivalent.
Special Values
x y hypot(x, y) invalid?
x ±0.0 |x| no
±∞ y +∞ no
±∞ NAN +∞ no
Examples:
import std.math.operations : feqrel;

assert(hypot(1.0, 1.0).feqrel(1.4142) > 16);
assert(hypot(3.0, 4.0).feqrel(5.0) > 16);
writeln(hypot(real.infinity, 1.0L)); // real.infinity
writeln(hypot(real.infinity, real.nan)); // real.infinity
pure nothrow @nogc @safe T hypot(T)(const T x, const T y, const T z)
if (isFloatingPoint!T);
Calculates the distance of the point (x, y, z) from the origin (0, 0, 0) in three-dimensional space. The distance is the value of the square root of the sums of the squares of x, y, and z:
sqrt(x2 + y2 + z2)
Note that the distance between two points (x1, y1, z1) and (x2, y2, z2) in three-dimensional space can be calculated as hypot(x2-x1, y2-y1, z2-z1).
Parameters:
T x floating point value
T y floating point value
T z floating point value
Returns:
The square root of the sum of the squares of the given arguments.
Examples:
import std.math.operations : isClose;

assert(isClose(hypot(1.0, 2.0, 2.0), 3.0));
assert(isClose(hypot(2.0, 3.0, 6.0), 7.0));
assert(isClose(hypot(1.0, 4.0, 8.0), 9.0));
pure nothrow @nogc @trusted Unqual!(CommonType!(T1, T2)) poly(T1, T2)(T1 x, in T2[] A)
if (isFloatingPoint!T1 && isFloatingPoint!T2);

pure nothrow @nogc @safe Unqual!(CommonType!(T1, T2)) poly(T1, T2, int N)(T1 x, ref const T2[N] A)
if (isFloatingPoint!T1 && isFloatingPoint!T2 && (N > 0) && (N <= 10));
Evaluate polynomial A(x) = a0 + a1x + a2x2 + a3x3; ...
Uses Horner's rule A(x) = a0 + x(a1 + x(a2 + x(a3 + ...)))
Parameters:
T1 x the value to evaluate.
T2[] A array of coefficients a0, a1, etc.
Examples:
real x = 3.1L;
static real[] pp = [56.1L, 32.7L, 6];

writeln(poly(x, pp)); // (56.1L + (32.7L + 6.0L * x) * x)
T nextPow2(T)(const T val)
if (isIntegral!T);

T nextPow2(T)(const T val)
if (isFloatingPoint!T);
Gives the next power of two after val. T can be any built-in numerical type.
If the operation would lead to an over/underflow, this function will return 0.
Parameters:
T val any number
Returns:
the next power of two after val
Examples:
writeln(nextPow2(2)); // 4
writeln(nextPow2(10)); // 16
writeln(nextPow2(4000)); // 4096

writeln(nextPow2(-2)); // -4
writeln(nextPow2(-10)); // -16

writeln(nextPow2(uint.max)); // 0
writeln(nextPow2(uint.min)); // 0
writeln(nextPow2(size_t.max)); // 0
writeln(nextPow2(size_t.min)); // 0

writeln(nextPow2(int.max)); // 0
writeln(nextPow2(int.min)); // 0
writeln(nextPow2(long.max)); // 0
writeln(nextPow2(long.min)); // 0
Examples:
writeln(nextPow2(2.1)); // 4.0
writeln(nextPow2(-2.0)); // -4.0
writeln(nextPow2(0.25)); // 0.5
writeln(nextPow2(-4.0)); // -8.0

writeln(nextPow2(double.max)); // 0.0
writeln(nextPow2(double.infinity)); // double.infinity
T truncPow2(T)(const T val)
if (isIntegral!T);

T truncPow2(T)(const T val)
if (isFloatingPoint!T);
Gives the last power of two before val. <> can be any built-in numerical type.
Parameters:
T val any number
Returns:
the last power of two before val
Examples:
writeln(truncPow2(3)); // 2
writeln(truncPow2(4)); // 4
writeln(truncPow2(10)); // 8
writeln(truncPow2(4000)); // 2048

writeln(truncPow2(-5)); // -4
writeln(truncPow2(-20)); // -16

writeln(truncPow2(uint.max)); // int.max + 1
writeln(truncPow2(uint.min)); // 0
writeln(truncPow2(ulong.max)); // long.max + 1
writeln(truncPow2(ulong.min)); // 0

writeln(truncPow2(int.max)); // (int.max / 2) + 1
writeln(truncPow2(int.min)); // int.min
writeln(truncPow2(long.max)); // (long.max / 2) + 1
writeln(truncPow2(long.min)); // long.min
Examples:
writeln(truncPow2(2.1)); // 2.0
writeln(truncPow2(7.0)); // 4.0
writeln(truncPow2(-1.9)); // -1.0
writeln(truncPow2(0.24)); // 0.125
writeln(truncPow2(-7.0)); // -4.0

writeln(truncPow2(double.infinity)); // double.infinity