View source code
Display the source code in std/math.d from which this page was generated on github.
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 local clone.

Module 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.

Functions

NameDescription
abs(x) Calculates the absolute value of a number
acos(x) Calculates the arc cosine of x, returning a value ranging from 0 to π.
acosh(x) Calculates the inverse hyperbolic cosine of x.
approxEqual(lhs, rhs) Computes whether two values are approximately equal, admitting a maximum relative difference, and a maximum absolute difference.
asin(x) Calculates the arc sine of x, returning a value ranging from -π/2 to π/2.
asinh(x) Calculates the inverse hyperbolic sine of x.
atan(x) Calculates the arc tangent of x, returning a value ranging from -π/2 to π/2.
atan2(y, x) Calculates the arc tangent of y / x, returning a value ranging from -π to π.
atanh(x) Calculates the inverse hyperbolic tangent of x, returning a value from ranging from -1 to 1.
cbrt(x) Calculates the cube root of x.
ceil(x) Returns the value of x rounded upward to the next integer (toward positive infinity).
cmp(x, y) Defines a total order on all floating-point numbers.
copysign(to, from)
cos(x) Returns cosine of x. x is in radians.
cosh(x) Calculates the hyperbolic cosine of x.
equalsDigit(x, y, ndigits) Compare floating point numbers to n decimal digits of precision.
exp(x) Calculates ex.
exp2(x) Calculates 2x.
expm1(x) Calculates the value of the natural logarithm base (e) raised to the power of x, minus 1.
fabs(x) Returns |x|
fdim(x, y) Returns the positive difference between x and y.
feqrel(x, y) To what precision is x equal to y?
floor(x) Returns the value of x rounded downward to the next integer (toward negative infinity).
fma(x, y, z) Returns (x * y) + z, rounding only once according to the current rounding mode.
fmax(x, y) Returns the larger of x and y.
fmin(x, y) Returns the smaller of x and y.
fmod(x, y) Calculates the remainder from the calculation x/y.
frexp(value, exp) Separate floating point value into significand and exponent.
getNaNPayload(x) Extract an integral payload from a NAN.
hypot(x, 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:
ieeeFlags()
ilogb(x) Extracts the exponent of x as a signed integral value.
isFinite(x) Determines if x is finite.
isIdentical(x, y) Is the binary representation of x identical to y?
isInfinity(x) Determines if x is ±∞.
isNaN(x) Determines if x is NaN.
isNormal(x) Determines if x is normalized.
isPowerOf2(x) Check whether a number is an integer power of two.
isSubnormal(x) Determines if x is subnormal.
ldexp(n, exp) Compute n * 2exp
log(x) Calculate the natural logarithm of x.
log10(x) Calculate the base-10 logarithm of x.
log1p(x) Calculates the natural logarithm of 1 + x.
log2(x) Calculates the base-2 logarithm of x: log2x
logb(x) Extracts the exponent of x as a signed integral value.
lrint(x) Rounds x to the nearest integer value, using the current rounding mode.
lround(x) Return the value of x rounded to the nearest integer.
modf(x, 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.
NaN(payload) Create a quiet NAN, storing an integer inside the payload.
nearbyint(x) Rounds x to the nearest integer value, using the current rounding mode.
nextafter(x, y) Calculates the next representable value after x in the direction of y.
nextDown(x) Calculate the next smallest floating point value before x.
nextPow2(val) Gives the next power of two after val. T can be any built-in numerical type.
nextUp(x) Calculate the next largest floating point value after x.
poly(x, A) Evaluate polynomial A(x) = a0 + a1x + a2x2 + a3x3; ...
pow(x, n) Compute the value of x n, where n is an integer
pow(x, n) Compute the value of an integer x, raised to the power of a positive integer n.
pow(x, y) Computes integer to floating point powers.
pow(x, y) Calculates xy.
powmod(x, n, m) Computes the value of a positive integer x, raised to the power n, modulo m.
quantize(val, 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(val, exp) 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(x, y) Calculate the remainder x REM y, following IEC 60559.
remquo(x, y, n) Calculate the remainder x REM y, following IEC 60559.
resetIeeeFlags() Set all of the floating-point status flags to false.
rint(x) Rounds x to the nearest integer value, using the current rounding mode.
rndtol(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(x) Deprecated. Please use round instead.
round(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(x, n) Efficiently calculates x * 2n.
sgn(x) Returns -1 if x < 0, x if x == 0, 1 if x > 0, and NAN if x==NAN.
signbit(x) Return 1 if sign bit of e is set, 0 if not.
sin(x) Returns sine of x. x is in radians.
sinh(x) Calculates the hyperbolic sine of x.
sqrt(x) Compute square root of x.
tan(x) Returns tangent of x. x is in radians.
tanh(x) Calculates the hyperbolic tangent of x.
trunc(x) Returns the integer portion of x, dropping the fractional portion. This is also known as "chop" rounding. pure on all platforms.
truncPow2(val) Gives the last power of two before val. <> can be any built-in numerical type.

Structs

NameDescription
FloatingPointControl Control the Floating point hardware
IeeeFlags IEEE exception status flags ('sticky bits')

Manifest constants

NameTypeDescription
E e = 2.718281...
LN10 ln 10 = 2.302585...
LN2 ln 2 = 0.693147...
LOG10E log10e = 0.434294...
LOG2 log102 = 0.301029...
LOG2E log2e = 1.442695...
LOG2T log210 = 3.321928...
M_1_PI 1 / π = 0.318309...
M_2_PI 2 / π = 0.636619...
M_2_SQRTPI 2 / √π = 1.128379...
PI π = 3.141592...
PI_2 π / 2 = 1.570796...
PI_4 π / 4 = 0.785398...
SQRT1_2 √½ = 0.707106...
SQRT2 √2 = 1.414213...

Aliases

NameTypeDescription
FP_ILOGB0 Special return values of ilogb.
FP_ILOGBNAN Special return values of ilogb.

Authors

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

License

Boost License 1.0.