std.math
Elementary mathematical functions.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.
Unlike C, there is no global 'errno' variable. Consequently, almost all of these functions are pure nothrow.
Authors:
Walter Bright, Don Clugston
- e
- log210
- log2e
- log102
- log10e
- ln 2
- ln 10
- π
- π / 2
- π / 4
- 1 / π
- 2 / π
- 2 / &radix;π
- &radix;2
- &radix;½
- Calculates the absolute value
For complex numbers, abs(z) = sqrt( z.re2 + z.im2 ) = hypot(z.re, z.im).
- Complex conjugate
conj(x + iy) = x - iy
Note that z * conj(z) = z.re2 - z.im2 is always a real number
- Returns cosine of x. x is in radians.
Special Values x cos(x) invalid? NAN NAN yes ±∞ NAN yes
BUGS:
Results are undefined if |x| >= 264.
- Returns sine of x. x is in radians.
Special Values x sin(x) invalid? NAN NAN yes ±0.0 ±0.0 no ±∞ NAN yes
BUGS:
Results are undefined if |x| >= 264.
- sine, complex and imaginary
sin(z) = sin(z.re)*cosh(z.im) + cos(z.re)*sinh(z.im)i
If both sin(θ) and cos(θ) are required, it is most efficient to use expi(θ).
- cosine, complex and imaginary
cos(z) = cos(z.re)*cosh(z.im) - sin(z.re)*sinh(z.im)i
- Returns tangent of x. x is in radians.
Special Values x tan(x) invalid? NAN NAN yes ±0.0 ±0.0 no ±∞ NAN yes
- Calculates the arc cosine of x,
returning a value ranging from -π/2 to π/2.
Special Values x acos(x) invalid? >1.0 NAN yes <-1.0 NAN yes NAN NAN yes
- Calculates the arc sine of x,
returning a value ranging from -π/2 to π/2.
Special Values x asin(x) invalid? ±0.0 ±0.0 no >1.0 NAN yes <-1.0 NAN yes
- Calculates the arc tangent of x,
returning a value ranging from -π/2 to π/2.
Special Values x atan(x) invalid? ±0.0 ±0.0 no ±∞ NAN yes
- Calculates the arc tangent of y / x,
returning a value ranging from -π to π.
Special Values y x atan(y, x) NAN anything NAN anything NAN NAN ±0.0 >0.0 ±0.0 ±0.0 +0.0 ±0.0 ±0.0 <0.0 ±π ±0.0 -0.0 ±π >0.0 ±0.0 π/2 <0.0 ±0.0 -π/2 >0.0 ∞ ±0.0 ±∞ anything ±π/2 >0.0 -∞ ±π ±∞ ∞ ±π/4 ±∞ -∞ ±3π/4
- Calculates the hyperbolic cosine of x.
Special Values x cosh(x) invalid? ±∞ ±0.0 no
- Calculates the hyperbolic sine of x.
Special Values x sinh(x) invalid? ±0.0 ±0.0 no ±∞ ±∞ no
- Calculates the hyperbolic tangent of x.
Special Values x tanh(x) invalid? ±0.0 ±0.0 no ±∞ ±1.0 no
- Calculates the inverse hyperbolic cosine of x.
Mathematically, acosh(x) = log(x + sqrt( x*x - 1))
Special Values x acosh(x) NAN NAN <1 NAN 1 0 +∞ +∞
- Calculates the inverse hyperbolic sine of x.
Mathematically,asinh(x) = log( x + sqrt( x*x + 1 )) // if x >= +0 asinh(x) = -log(-x + sqrt( x*x + 1 )) // if x <= -0
Special Values x asinh(x) NAN NAN ±0 ±0 ±∞ ±∞
- Calculates the inverse hyperbolic tangent of x,
returning a value from ranging from -1 to 1.
Mathematically, atanh(x) = log( (1+x)/(1-x) ) / 2
Special Values x acosh(x) NAN NAN ±0 ±0 -∞ -0
- 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.
- 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.
- Compute square root of x.
Special Values x sqrt(x) invalid? -0.0 -0.0 no <0.0 NAN yes +∞ +∞ no
- ditto
Compute square root of x.
Special Values x sqrt(x) invalid? -0.0 -0.0 no <0.0 NAN yes +∞ +∞ no
- ditto
Compute square root of x.
Special Values x sqrt(x) invalid? -0.0 -0.0 no <0.0 NAN yes +∞ +∞ no
- Calculates ex.
Special Values x ex +∞ +∞ -∞ +0.0
NAN NAN
For very small x, expm1(x) is more accurate than exp(x)-1.
x | ex-1 |
---|---|
±0.0 | ±0.0 | +∞ | +∞ |
x | exp2(x) | +∞ | +∞ | -∞ | +0.0 |
---|
On many CPUs (such as x86), this is a very efficient operation; almost twice as fast as calculating sin(y) and cos(y) separately, and is the preferred method when both are required.
Returns:
Calculate and return x and exp such that value =x*2exp and .5 <= |x| < 1.0
x has same sign as value.
value | returns | exp |
---|---|---|
±0.0 | ±0.0 | 0 |
+∞ | +∞ | int.max |
-∞ | -∞ | int.min |
±NAN | ±NAN | int.min |
If x is not a special value, the result is the same as cast(int)logb(x).
x | ilogb(x) | Range error? |
---|---|---|
0 | FP_ILOGB0 | yes |
±∞ | int.max | no |
NAN | FP_ILOGBNAN | no |
References:
frexp
x | log(x) | divide by 0? | invalid? |
---|---|---|---|
±0.0 | -∞ | yes | no |
<0.0 | NAN | no | yes |
+∞ | +∞ | no | no |
x | log10(x) | divide by 0? | invalid? |
---|---|---|---|
±0.0 | -∞ | yes | no |
<0.0 | NAN | no | yes |
+∞ | +∞ | no | no |
For very small x, log1p(x) will be more accurate than log(1 + x).
x | log1p(x) | divide by 0? | invalid? |
---|---|---|---|
±0.0 | ±0.0 | no | no |
-1.0 | -∞ | yes | no |
<-1.0 | NAN | no | yes |
+∞ | -∞ | no | no |
x | log2(x) | divide by 0? | invalid? |
---|---|---|---|
±0.0 | -∞ | yes | no |
<0.0 | NAN | no | yes |
+∞ | +∞ | no | no |
If x is subnormal, it is treated as if it were normalized. For a positive, finite x:
1 <= x * FLT_RADIX-logb(x) < FLT_RADIX
x | logb(x) | divide by 0? |
---|---|---|
±∞ | +∞ | no |
±0.0 | -∞ | yes |
Returns:
The value of x - i * y, where i is the number of times that y can be completely subtracted from x. The result has the same sign as x.
x | y | modf(x, y) | invalid? |
---|---|---|---|
±0.0 | not 0.0 | ±0.0 | no |
±∞ | anything | NAN | yes |
anything | ±0.0 | NAN | yes |
!=±∞ | ±∞ | x | no |
scalbn handles underflow and overflow in the same fashion as the basic arithmetic operators.
x | scalb(x) |
---|---|
±∞ | ±∞ |
±0.0 | ±0.0 |
x | cbrt(x) | invalid? |
---|---|---|
±0.0 | ±0.0 | no |
NAN | NAN | yes |
±∞ | ±∞ | no |
x | fabs(x) |
---|---|
±0.0 | +0.0 |
±∞ | +∞ |
sqrt( + )
Note that hypot(x, y), hypot(y, x) and hypot(x, -y) are equivalent.
x | y | hypot(x, y) | invalid? |
---|---|---|---|
x | ±0.0 | |x| | no |
±∞ | y | +∞ | no |
±∞ | NAN | +∞ | no |


Returns the base e (2.718...) logarithm of the absolute value of the gamma function of the argument.
For reals, lgamma is equivalent to log(fabs(gamma(x))).
x | lgamma(x) | invalid? |
---|---|---|
NAN | NAN | yes |
integer <= 0 | +∞ | yes |
±∞ | +∞ | no |
Γ(x) is a generalisation of the factorial function to real and complex numbers. Like x!, Γ(x+1) = x*Γ(x).
Mathematically, if z.re > 0 then Γ(z) = ∫0∞ tz-1e-t dt
x | Γ(x) | invalid? |
---|---|---|
NAN | NAN | yes |
±0.0 | ±∞ | yes |
integer >0 | (x-1)! | no |
integer <0 | NAN | yes |
+∞ | +∞ | no |
-∞ | NAN | yes |
References:
http://en.wikipedia.org/wiki/Gamma_function, http://www.netlib.org/cephes/ldoubdoc.html#gamma
Unlike the rint functions, nearbyint does not raise the FE_INEXACT exception.
This is generally the fastest method to convert a floating-point number to an integer. Note that the results from this function depend on the rounding mode, if the fractional part of x is exactly 0.5. If using the default rounding mode (ties round to even integers) lrint(4.5) == 4, lrint(5.5)==6.
If the fractional part of x is exactly 0.5, the return value is rounded away from zero.
This is also known as "chop" rounding.
REM is the value of x - y * n, where n is the integer nearest the exact value of x / y. If |n - x / y| == 0.5, n is even. If the result is zero, it has the same sign as x. Otherwise, the sign of the result is the sign of x / y. Precision mode has no effect on the remainder functions.
remquo returns n in the parameter n.
x | y | remainder(x, y) | n | invalid? |
---|---|---|---|---|
±0.0 | not 0.0 | ±0.0 | 0.0 | no |
±∞ | anything | NAN | ? | yes |
anything | ±0.0 | NAN | ? | yes |
!= ±∞ | ±∞ | x | ? | no |
Note:
remquo not supported on windows
Same as ==, except that positive and negative zero are not identical, and two NANs are identical if they have the same 'payload'.
For floats, the largest possible payload is 0x3F_FFFF. For doubles, it is 0x3_FFFF_FFFF_FFFF. For 80-bit or 128-bit reals, it is 0x3FFF_FFFF_FFFF_FFFF.
Returns:
the integer payload as a ulong.
For floats, the largest possible payload is 0x3F_FFFF. For doubles, it is 0x3_FFFF_FFFF_FFFF. For 80-bit or 128-bit reals, it is 0x3FFF_FFFF_FFFF_FFFF.
Return the least number greater than x that is representable as a real; thus, it gives the next point on the IEEE number line.
x | nextUp(x) |
---|---|
-∞ | -real.max |
±0.0 | real.min*real.epsilon |
real.max | ∞ |
∞ | ∞ |
NAN | NAN |
Return the greatest number less than x that is representable as a real; thus, it gives the previous point on the IEEE number line.
x | nextDown(x) |
---|---|
∞ | real.max |
±0.0 | -real.min*real.epsilon |
-real.max | -∞ |
-∞ | -∞ |
NAN | NAN |
If y > x, the result will be the next largest floating-point value; if y < x, the result will be the next smallest value. If x == y, the result is y.
Remarks:
This function is not generally very useful; it's almost always better to use the faster functions nextUp() or nextDown() instead.
The FE_INEXACT and FE_OVERFLOW exceptions will be raised if x is finite and the function result is infinite. The FE_INEXACT and FE_UNDERFLOW exceptions will be raised if the function value is subnormal, and x is not equal to y.
Returns:
x, y | fdim(x, y) |
---|---|
x > y | x - y |
x <= y | +0.0 |
BUGS:
Not currently implemented - rounds twice.
Returns:
the number of mantissa bits which are equal in x and y. eg, 0x1.F8p+60 and 0x1.F1p+60 are equal to 5 bits of precision.
x | y | feqrel(x, y) |
---|---|---|
x | x | real.mant_dig |
x | >= 2*x | 0 |
x | <= x/2 | 0 |
NAN | any | 0 |
any | NAN | 0 |
Uses Horner's rule A(x) = a0 + x(a1 + x(a2 + x(a3 + ...)))
Params:
real[] A | array of coefficients a0, a1, etc. |