std.math

Contains the elementary mathematical functions (powers, roots, and trigonometric functions), and low-level floating-point operations. Mathematical special functions are available in std.mathspecial.

The functionality closely follows the IEEE754-2008 standard for floating-point arithmetic, including the use of camelCase names rather than C99-style lower case names. All of these functions behave correctly when presented with an infinity or NaN.

The following IEEE 'real' formats are currently supported:

  • 64 bit Big-endian 'double' (eg PowerPC)
  • 128 bit Big-endian 'quadruple' (eg SPARC)
  • 64 bit Little-endian 'double' (eg x86-SSE2)
  • 80 bit Little-endian, with implied bit 'real80' (eg x87, Itanium)
  • 128 bit Little-endian 'quadruple' (not implemented on any known processor!)
  • Non-IEEE 128 bit Big-endian 'doubledouble' (eg PowerPC) has partial support

Unlike C, there is no global 'errno' variable. Consequently, almost all of these functions are pure nothrow.

Public Imports

std.meta
public import std.meta : AliasSeq;

Members

Aliases

FP_ILOGB0
alias FP_ILOGB0 = core.stdc.math.FP_ILOGB0
FP_ILOGBNAN
alias FP_ILOGBNAN = core.stdc.math.FP_ILOGBNAN

Special return values of ilogb.

Functions

NaN
real NaN(ulong payload)

Create a quiet NaN, storing an integer inside the payload.

abs
auto abs(Num x)

Calculates the absolute value of a number.

acos
real acos(real x)
double acos(double x)
float acos(float x)

Calculates the arc cosine of x, returning a value ranging from 0 to π.

acosh
real acosh(real x)
double acosh(double x)
float acosh(float x)

Calculates the inverse hyperbolic cosine of x.

approxEqual
bool approxEqual(T value, U reference, V maxRelDiff, V maxAbsDiff)

Computes whether a values is approximately equal to a reference value, admitting a maximum relative difference, and a maximum absolute difference.

asin
real asin(real x)
double asin(double x)
float asin(float x)

Calculates the arc sine of x, returning a value ranging from -π/2 to π/2.

asinh
real asinh(real x)
double asinh(double x)
float asinh(float x)

Calculates the inverse hyperbolic sine of x.

atan
real atan(real x)
double atan(double x)
float atan(float x)

Calculates the arc tangent of x, returning a value ranging from -π/2 to π/2.

atan2
real atan2(real y, real x)
double atan2(double y, double x)
float atan2(float y, float x)

Calculates the arc tangent of y / x, returning a value ranging from -π to π.

atanh
real atanh(real x)
double atanh(double x)
float atanh(float x)

Calculates the inverse hyperbolic tangent of x, returning a value from ranging from -1 to 1.

cbrt
real cbrt(real x)

Calculates the cube root of x.

ceil
float ceil(float x)
double ceil(double x)

Returns the value of x rounded upward to the next integer (toward positive infinity).

ceil
real ceil(real x)

Returns the value of x rounded upward to the next integer (toward positive infinity).

cmp
int cmp(const(T) x, const(T) y)

Defines a total order on all floating-point numbers.

copysign
R copysign(X to, R from)
copysign
R copysign(R to, X from)
cos
real cos(real x)
double cos(double x)
float cos(float x)

Returns cosine of x. x is in radians.

cosh
real cosh(real x)
double cosh(double x)
float cosh(float x)

Calculates the hyperbolic cosine of x.

exp
real exp(real x)
double exp(double x)
float exp(float x)

Calculates ex.

exp2
real exp2(real x)
double exp2(double x)
float exp2(float x)

Calculates 2x.

expm1
real expm1(real x)
double expm1(double x)
float expm1(float x)

Calculates the value of the natural logarithm base (e) raised to the power of x, minus 1.

fabs
float fabs(float f)
double fabs(double d)

Returns |x|

fabs
real fabs(real x)

Returns |x|

fdim
real fdim(real x, real y)

Returns the positive difference between x and y.

feqrel
int feqrel(X x, X y)

To what precision is x equal to y?

floor
float floor(float x)
double floor(double x)

Returns the value of x rounded downward to the next integer (toward negative infinity).

floor
real floor(real x)

Returns the value of x rounded downward to the next integer (toward negative infinity).

fma
real fma(real x, real y, real z)

Returns (x * y) + z, rounding only once according to the current rounding mode.

fmax
real fmax(real x, real y)

Returns the larger of x and y.

fmin
real fmin(real x, real y)

Returns the smaller of x and y.

fmod
real fmod(real x, real y)

Calculates the remainder from the calculation x/y.

frexp
T frexp(T value, int exp)

Separate floating point value into significand and exponent.

getNaNPayload
ulong getNaNPayload(real x)

Extract an integral payload from a NaN.

hypot
real hypot(real x, real y)

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:

ilogb
int ilogb(T x)

Extracts the exponent of x as a signed integral value.

ilogb
int ilogb(T x)

Extracts the exponent of x as a signed integral value.

isClose
bool isClose(T lhs, U rhs, V maxRelDiff, V maxAbsDiff)

Computes whether two values are approximately equal, admitting a maximum relative difference, and a maximum absolute difference.

isFinite
bool isFinite(X x)

Determines if x is finite.

isIdentical
bool isIdentical(real x, real y)

Is the binary representation of x identical to y?

isInfinity
bool isInfinity(X x)

Determines if x is ±∞.

isNaN
bool isNaN(X x)

Determines if x is NaN.

isNormal
bool isNormal(X x)

Determines if x is normalized.

isPowerOf2
bool isPowerOf2(X x)

Check whether a number is an integer power of two.

isSubnormal
bool isSubnormal(X x)

Determines if x is subnormal.

ldexp
real ldexp(real n, int exp)
double ldexp(double n, int exp)
float ldexp(float n, int exp)

Compute n * 2exp References: frexp

log
real log(real x)

Calculate the natural logarithm of x.

log10
real log10(real x)

Calculate the base-10 logarithm of x.

log1p
real log1p(real x)

Calculates the natural logarithm of 1 + x.

log2
real log2(real x)

Calculates the base-2 logarithm of x: log2x

logb
real logb(real x)

Extracts the exponent of x as a signed integral value.

lrint
long lrint(real x)

Rounds x to the nearest integer value, using the current rounding mode.

lround
long lround(real x)

Return the value of x rounded to the nearest integer.

modf
real modf(real x, real i)

Breaks x into an integral part and a fractional part, each of which has the same sign as x. The integral part is stored in i.

nearbyint
real nearbyint(real x)

Rounds x to the nearest integer value, using the current rounding mode.

nextDown
real nextDown(real x)
double nextDown(double x)
float nextDown(float x)

Calculate the next smallest floating point value before x.

nextPow2
T nextPow2(T val)

Gives the next power of two after val. T can be any built-in numerical type.

nextUp
real nextUp(real x)
double nextUp(double x)
float nextUp(float x)

Calculate the next largest floating point value after x.

nextafter
T nextafter(T x, T y)

Calculates the next representable value after x in the direction of y.

poly
Unqual!(CommonType!(T1, T2)) poly(T1 x, T2[] A)
Unqual!(CommonType!(T1, T2)) poly(T1 x, T2[N] A)

Evaluate polynomial A(x) = a0 + a1x + a2x2 + a3x3; ...

pow
Unqual!F pow(F x, G n)

Compute the value of x n, where n is an integer

pow
typeof(Unqual!(F).init * Unqual!(G).init) pow(F x, G n)

Compute the power of two integral numbers.

pow
real pow(I x, F y)

Computes integer to floating point powers.

pow
Unqual!(Largest!(F, G)) pow(F x, G y)

Calculates xy.

powmod
Unqual!(Largest!(F, H)) powmod(F x, G n, H m)

Computes the value of a positive integer x, raised to the power n, modulo m.

quantize
Unqual!F quantize(F val, F unit)

Round val to a multiple of unit. rfunc specifies the rounding function to use; by default this is rint, which uses the current rounding mode.

quantize
Unqual!F quantize(F val, E exp)
Unqual!F quantize(F val)

Round val to a multiple of pow(base, exp). rfunc specifies the rounding function to use; by default this is rint, which uses the current rounding mode.

remainder
real remainder(real x, real y)
remquo
real remquo(real x, real y, int n)

Calculate the remainder x REM y, following IEC 60559.

resetIeeeFlags
void resetIeeeFlags()

Set all of the floating-point status flags to false.

rint
real rint(real x)
double rint(double x)
float rint(float x)

Rounds x to the nearest integer value, using the current rounding mode.

rndtol
long rndtol(float x)
long rndtol(double x)

Returns x rounded to a long value using the current rounding mode. If the integer value of x is greater than long.max, the result is indeterminate.

rndtol
long rndtol(real x)

Returns x rounded to a long value using the current rounding mode. If the integer value of x is greater than long.max, the result is indeterminate.

rndtonl
real rndtonl(real x)

Deprecated. Please use round instead.

round
auto round(real x)

Return the value of x rounded to the nearest integer. If the fractional part of x is exactly 0.5, the return value is rounded away from zero.

scalbn
real scalbn(real x, int n)

Efficiently calculates x * 2n.

sgn
F sgn(F x)

Returns -1 if x < 0, x if x == 0, 1 if x > 0, and NaN if x==NaN.

signbit
int signbit(X x)

Return 1 if sign bit of e is set, 0 if not.

sin
float sin(float x)
double sin(double x)

Returns sine of x. x is in radians.

sin
real sin(real x)

Returns sine of x. x is in radians.

sinh
float sinh(float x)
double sinh(double x)

Calculates the hyperbolic sine of x.

sinh
real sinh(real x)

Calculates the hyperbolic sine of x.

sqrt
real sqrt(real x)
double sqrt(double x)

Compute square root of x.

sqrt
float sqrt(float x)

Compute square root of x.

tan
float tan(float x)
double tan(double x)

Returns tangent of x. x is in radians.

tan
real tan(real x)

Returns tangent of x. x is in radians.

tanh
float tanh(float x)
double tanh(double x)

Calculates the hyperbolic tangent of x.

tanh
real tanh(real x)

Calculates the hyperbolic tangent of x.

trunc
real trunc(real x)

Returns the integer portion of x, dropping the fractional portion. This is also known as "chop" rounding. pure on all platforms.

truncPow2
T truncPow2(T val)

Gives the last power of two before val. $(T) can be any built-in numerical type.

Properties

ieeeFlags
IeeeFlags ieeeFlags [@property getter]

Structs

FloatingPointControl
struct FloatingPointControl

Control the Floating point hardware

IeeeFlags
struct IeeeFlags

IEEE exception status flags ('sticky bits')

Meta

Authors

Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger

Suggestion Box / Bug Report