View source code Display the source code in std/math.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

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:

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 lhs is approximately equal to rhs admitting a maximum relative difference maxRelDiff and a maximum absolute difference maxAbsDiff.
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 cosine, complex and imaginary
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 .
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 Return a snapshot of the current state of the floating-point status flags.
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.
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: ⊂x
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 , 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.
nextUp Calculate the next largest floating point value after x.
poly Evaluate polynomial A(x) = ⊂ + ⊂x + ⊂x,2 + ⊂x,3; ...
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.
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 to the even integer.
scalbn Efficiently calculates x * 2n.
sgn Returns -1 if x < 0, x if x == 0, 1 if x > 0, and if x==.
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.
sin Returns sine for complex and imaginary arguments.
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.

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 ⊂e = 0.434294...
LOG2 ⊂2 = 0.301029...
LOG2E ⊂e = 1.442695...
LOG2T ⊂10 = 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

License

Boost License 1.0.

Comments