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.

Status

The semantics and names of feqrel and approxEqual will be revised.

Functions

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

Structs

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

Enum values

Name Type Description
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...

Authors

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

License

Boost License 1.0.

Comments