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
a local clone.
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.
Category | Members |
---|---|
Constants | E PI PI_2 PI_4 M_1_PI M_2_PI M_2_SQRTPI LN10 LN2 LOG2 LOG2E LOG2T LOG10E SQRT2 SQRT1_2 |
Classics | abs fabs sqrt cbrt hypot poly nextPow2 truncPow2 |
Trigonometry | sin cos tan asin acos atan atan2 sinh cosh tanh asinh acosh atanh expi |
Rounding | ceil floor round lround trunc rint lrint nearbyint rndtol |
Exponentiation & Logarithms | pow exp exp2 expm1 ldexp frexp log log2 log10 logb ilogb log1p scalbn |
Modulus | fmod modf remainder |
Floating-point operations | approxEqual feqrel fdim fmax fmin fma nextDown nextUp nextafter NaN getNaNPayload cmp |
Introspection | isFinite isIdentical isInfinity isNaN isNormal isSubnormal signbit sgn copysign |
Complex Numbers | abs conj sin cos expi |
Hardware Control | IeeeFlags FloatingPointControl |
- 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
Status: The semantics and names of feqrel and approxEqual will be revised.
License:
Authors:
Source: std/math.d
- e = 2.718281...
- log210 = 3.321928...
- log2e = 1.442695...
- log102 = 0.301029...
- log10e = 0.434294...
- ln 2 = 0.693147...
- ln 10 = 2.302585...
- π = 3.141592...
- π / 2 = 1.570796...
- π / 4 = 0.785398...
- 1 / π = 0.318309...
- 2 / π = 0.636619...
- 2 / √π = 1.128379...
- √2 = 1.414213...
- √½ = 0.707106...
- pure nothrow @safe Num abs(Num)(Num x)
if (is(typeof(Num.init >= 0)) && is(typeof(-Num.init)) && !(is(Num* : const(ifloat*)) || is(Num* : const(idouble*)) || is(Num* : const(ireal*))));
pure nothrow @nogc @safe auto abs(Num)(Num z)
if (is(Num* : const(cfloat*)) || is(Num* : const(cdouble*)) || is(Num* : const(creal*)));
pure nothrow @nogc @safe auto abs(Num)(Num y)
if (is(Num* : const(ifloat*)) || is(Num* : const(idouble*)) || is(Num* : const(ireal*))); - Calculates the absolute value of a numberParameters:
Num (template parameter) type of number Num x real number value Num z complex number value Num y imaginary number value Returns:Examples:dittoassert(isIdentical(abs(-0.0L), 0.0L)); assert(isNaN(abs(real.nan))); assert(abs(-real.infinity) == real.infinity); assert(abs(-3.2Li) == 3.2L); assert(abs(71.6Li) == 71.6L); assert(abs(-56) == 56); assert(abs(2321312L) == 2321312L); assert(abs(-1L+1i) == sqrt(2.0L));
- Complex conjugateExamples:
creal c = 7 + 3Li; assert(conj(c) == 7-3Li); ireal z = -3.2Li; assert(conj(z) == -z);
- Returns cosine of x. x is in radians.Bugs:Results are undefined if |x| >= 264.
-
Parameters:
real x angle in radians (not degrees) Returns:sine of xBugs:Results are undefined if |x| >= 264.Examples:import std.math : sin, PI; import std.stdio : writefln; void someFunc() { real x = 30.0; auto result = sin(x * (PI / 180)); // convert degrees to radians writefln("The sine of %s degrees is %s", x, result); }
- Returns sine for complex and imaginary arguments.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(θ).Examples:
assert(sin(0.0+0.0i) == 0.0); assert(sin(2.0+0.0i) == sin(2.0L) );
- cosine, complex and imaginaryExamples:
assert(cos(0.0+0.0i)==1.0); assert(cos(1.3L+0.0i)==cos(1.3L)); assert(cos(5.2Li)== cosh(5.2L));
- Returns tangent of x. x is in radians.
- Calculates the arc cosine of x, returning a value ranging from 0 to π.
- Calculates the arc sine of x, returning a value ranging from -π/2 to π/2.
- Calculates the arc tangent of x, returning a value ranging from -π/2 to π/2.
- 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.
- Calculates the hyperbolic sine of x.
- Calculates the hyperbolic tangent of x.
- Calculates the inverse hyperbolic cosine of x.
- Calculates the inverse hyperbolic sine of x.
- Calculates the inverse hyperbolic tangent of x, returning a value from ranging from -1 to 1.
- 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.
- Calculates ex.
Special Values x ex +∞ +∞ -∞ +0.0 NAN NAN - Calculates the value of the natural logarithm base (e) raised to the power of x, minus 1.
- Calculates 2x.Examples:
assert(feqrel(exp2(0.5L), SQRT2) >= real.mant_dig -1); assert(exp2(8.0L) == 256.0); assert(exp2(-9.0L)== 1.0L/512.0);
- Calculate cos(y) + i sin(y).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.Examples:
assert(expi(1.3e5L) == cos(1.3e5L) + sin(1.3e5L) * 1i); assert(expi(0.0L) == 1L + 0.0Li);
- Separate floating point value into significand and exponent.Returns:Calculate and return x and exp such that value =x*2exp and .5 <= |x| < 1.0 x has same sign as value.
Special Values value returns exp ±0.0 ±0.0 0 +∞ +∞ int.max -∞ -∞ int.min ±NAN ±NAN int.min Examples:int exp; real mantissa = frexp(123.456L, exp); // check if values are equal to 19 decimal digits of precision assert(equalsDigit(mantissa * pow(2.0L, cast(real)exp), 123.456L, 19)); assert(frexp(-real.nan, exp) && exp == int.min); assert(frexp(real.nan, exp) && exp == int.min); assert(frexp(-real.infinity, exp) == -real.infinity && exp == int.min); assert(frexp(real.infinity, exp) == real.infinity && exp == int.max); assert(frexp(-0.0, exp) == -0.0 && exp == 0); assert(frexp(0.0, exp) == 0.0 && exp == 0);
- Extracts the exponent of x as a signed integral value.
- Compute n * 2exp
References: frexp
Examples:import std.meta; foreach(T; AliasSeq!(float, double, real)) { T r; r = ldexp(3.0L, 3); assert(r == 24); r = ldexp(cast(T)3.0, cast(int) 3); assert(r == 24); T n = 3.0; int exp = 3; r = ldexp(n, exp); assert(r == 24); }
- Calculate the natural logarithm of x.Examples:
assert(log(E) == 1);
- Calculate the base-10 logarithm of x.Examples:
assert(fabs(log10(1000) - 3) < .000001);
- Calculates the natural logarithm of 1 + x.
- Calculates the base-2 logarithm of x: log2xExamples:
// check if values are equal to 19 decimal digits of precision assert(equalsDigit(log2(1024.0L), 10, 19));
- Extracts the exponent of x as a signed integral value.
- Calculates the remainder from the calculation x/y.
- 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.
- Efficiently calculates x * 2n.scalbn handles underflow and overflow in the same fashion as the basic arithmetic operators.
Special Values x scalb(x) ±∞ ±∞ ±0.0 ±0.0 Examples:assert(scalbn(-real.infinity, 5) == -real.infinity);
- Calculates the cube root of x.
- Returns |x|
- 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:
- Returns the value of x rounded upward to the next integer (toward positive infinity).Examples:
assert(ceil(+123.456L) == +124); assert(ceil(-123.456L) == -123); assert(ceil(-1.234L) == -1); assert(ceil(-0.123L) == 0); assert(ceil(0.0L) == 0); assert(ceil(+0.123L) == 1); assert(ceil(+1.234L) == 2); assert(ceil(real.infinity) == real.infinity); assert(isNaN(ceil(real.nan))); assert(isNaN(ceil(real.init)));
- Returns the value of x rounded downward to the next integer (toward negative infinity).Examples:
assert(floor(+123.456L) == +123); assert(floor(-123.456L) == -124); assert(floor(-1.234L) == -2); assert(floor(-0.123L) == -1); assert(floor(0.0L) == 0); assert(floor(+0.123L) == 0); assert(floor(+1.234L) == 1); assert(floor(real.infinity) == real.infinity); assert(isNaN(floor(real.nan))); assert(isNaN(floor(real.init)));
- Rounds x to the nearest integer value, using the current rounding mode.
- 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.
- Rounds x to the nearest integer value, using the current rounding mode.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.Examples:
assert(lrint(4.5) == 4); assert(lrint(5.5) == 6); assert(lrint(-4.5) == -4); assert(lrint(-5.5) == -6); assert(lrint(int.max - 0.5) == 2147483646L); assert(lrint(int.max + 0.5) == 2147483648L); assert(lrint(int.min - 0.5) == -2147483648L); assert(lrint(int.min + 0.5) == -2147483648L);
- 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.
- 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.
- Returns the integer portion of x, dropping the fractional portion.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.
Special Values 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
- IEEE exception status flags ('sticky bits')These flags indicate that an exceptional floating-point condition has occurred. They indicate that a NaN or an infinity has been generated, that a result is inexact, or that a signalling NaN has been encountered. If floating-point exceptions are enabled (unmasked), a hardware exception will be generated instead of setting these flags.Examples:
static void func() { int a = 10 * 10; } real a=3.5; // Set all the flags to zero resetIeeeFlags(); assert(!ieeeFlags.divByZero); // Perform a division by zero. a/=0.0L; assert(a==real.infinity); assert(ieeeFlags.divByZero); // Create a NaN a*=0.0L; assert(ieeeFlags.invalid); assert(isNaN(a)); // Check that calling func() has no effect on the // status flags. IeeeFlags f = ieeeFlags; func(); assert(ieeeFlags == f);
- The result cannot be represented exactly, so rounding occurred.(example: x = sin(0.1); )
- An infinity was generated by division by zero (example: x = 3/0.0; )
- A machine NaN was generated. (example: x = real.infinity * 0.0; )
- Set all of the floating-point status flags to false.
- Return a snapshot of the current state of the floating-point status flags.
- Control the Floating point hardwareChange the IEEE754 floating-point rounding mode and the floating-point hardware exceptions. By default, the rounding mode is roundToNearest and all hardware exceptions are disabled. For most applications, debugging is easier if the division by zero, overflow, and invalid operation exceptions are enabled. These three are combined into a severeExceptions value for convenience. Note in particular that if invalidException is enabled, a hardware trap will be generated whenever an uninitialized floating-point variable is used. All changes are temporary. The previous state is restored at the end of the scope.
Example:
{ FloatingPointControl fpctrl; // Enable hardware exceptions for division by zero, overflow to infinity, // invalid operations, and uninitialized floating-point variables. fpctrl.enableExceptions(FloatingPointControl.severeExceptions); // This will generate a hardware exception, if x is a // default-initialized floating point variable: real x; // Add `= 0` or even `= real.nan` to not throw the exception. real y = x * 3.0; // The exception is only thrown for default-uninitialized NaN-s. // NaN-s with other payload are valid: real z = y * real.nan; // ok // Changing the rounding mode: fpctrl.rounding = FloatingPointControl.roundUp; assert(rint(1.1) == 2); // The set hardware exceptions will be disabled when leaving this scope. // The original rounding mode will also be restored. } // Ensure previous values are returned: assert(!FloatingPointControl.enabledExceptions); assert(FloatingPointControl.rounding == FloatingPointControl.roundToNearest); assert(rint(1.1) == 1);
- Severe = The overflow, division by zero, and invalid exceptions.
- Returns true if the current FPU supports exception trapping
- Enable (unmask) specific hardware exceptions. Multiple exceptions may be ORed together.
- Disable (mask) specific hardware exceptions. Multiple exceptions may be ORed together.
- Return the exceptions which are currently enabled (unmasked)
- Determines if x is NaN.Parameters:
X x a floating point number. Returns:true if x is Nan.Examples:assert( isNaN(float.init)); assert( isNaN(-double.init)); assert( isNaN(real.nan)); assert( isNaN(-real.nan)); assert(!isNaN(cast(float)53.6)); assert(!isNaN(cast(real)-53.6));
- Determines if x is finite.Parameters:
X x a floating point number. Returns:true if x is finite.Examples:assert( isFinite(1.23f)); assert( isFinite(float.max)); assert( isFinite(float.min_normal)); assert(!isFinite(float.nan)); assert(!isFinite(float.infinity));
- Determines if x is normalized.A normalized number must not be zero, subnormal, infinite nor NAN.Parameters:
X x a floating point number. Returns:true if x is normalized.Examples:float f = 3; double d = 500; real e = 10e+48; assert(isNormal(f)); assert(isNormal(d)); assert(isNormal(e)); f = d = e = 0; assert(!isNormal(f)); assert(!isNormal(d)); assert(!isNormal(e)); assert(!isNormal(real.infinity)); assert(isNormal(-real.max)); assert(!isNormal(real.min_normal/4));
- Determines if x is subnormal.Subnormals (also known as "denormal number"), have a 0 exponent and a 0 most significant mantissa bit.Parameters:
X x a floating point number. Returns:true if x is a denormal number.Examples:import std.meta; foreach (T; AliasSeq!(float, double, real)) { T f; for (f = 1.0; !isSubnormal(f); f /= 2) assert(f != 0); }
- Determines if x is ±∞.Parameters:
X x a floating point number. Returns:true if x is ±∞.Examples:assert(!isInfinity(float.init)); assert(!isInfinity(-float.init)); assert(!isInfinity(float.nan)); assert(!isInfinity(-float.nan)); assert(isInfinity(float.infinity)); assert(isInfinity(-float.infinity)); assert(isInfinity(-1.0f / 0.0f));
- Is the binary representation of x identical to y?Same as ==, except that positive and negative zero are not identical, and two NANs are identical if they have the same 'payload'.
- Return 1 if sign bit of e is set, 0 if not.Examples:
debug (math) printf("math.signbit.unittest\n"); assert(!signbit(float.nan)); assert(signbit(-float.nan)); assert(!signbit(168.1234f)); assert(signbit(-168.1234f)); assert(!signbit(0.0f)); assert(signbit(-0.0f)); assert(signbit(-float.max)); assert(!signbit(float.max)); assert(!signbit(double.nan)); assert(signbit(-double.nan)); assert(!signbit(168.1234)); assert(signbit(-168.1234)); assert(!signbit(0.0)); assert(signbit(-0.0)); assert(signbit(-double.max)); assert(!signbit(double.max)); assert(!signbit(real.nan)); assert(signbit(-real.nan)); assert(!signbit(168.1234L)); assert(signbit(-168.1234L)); assert(!signbit(0.0L)); assert(signbit(-0.0L)); assert(signbit(-real.max)); assert(!signbit(real.max));
- Return a value composed of to with from's sign bit.
- Returns -1 if x < 0, x if x == 0, 1 if x > 0, and NAN if x==NAN.Examples:
assert(sgn(168.1234) == 1); assert(sgn(-168.1234) == -1); assert(sgn(0.0) == 0); assert(sgn(-0.0) == 0);
- Create a quiet NAN, storing an integer inside the 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.
- Extract an integral payload from a NAN.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.
- Calculate the next largest floating point value after x.
- Calculate the next smallest floating point value before x.Return the greatest number less than x that is representable as a real; thus, it gives the previous point on the IEEE number line.
Special Values x nextDown(x) ∞ real.max ±0.0 -real.min_normal*real.epsilon -real.max -∞ -∞ -∞ NAN NAN Examples:assert( nextDown(1.0 + real.epsilon) == 1.0);
- Calculates the next representable value after x in the direction of y.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.Examples:float a = 1; assert(is(typeof(nextafter(a, a)) == float)); assert(nextafter(a, a.infinity) > a); double b = 2; assert(is(typeof(nextafter(b, b)) == double)); assert(nextafter(b, b.infinity) > b); real c = 3; assert(is(typeof(nextafter(c, c)) == real)); assert(nextafter(c, c.infinity) > c);
- Returns the positive difference between x and y.
- Returns the larger of x and y.
- Returns the smaller of x and y.
- Returns (x * y) + z, rounding only once according to the current rounding mode.Bugs:Not currently implemented - rounds twice.
- Compute the value of x n, where n is an integer
- Compute the value of an integer x, raised to the power of a positive integer n.If both x and n are 0, the result is 1. If n is negative, an integer divide error will occur at runtime, regardless of the value of x.Examples:
immutable int one = 1; immutable byte two = 2; immutable ubyte three = 3; immutable short four = 4; immutable long ten = 10; assert(pow(two, three) == 8); assert(pow(two, ten) == 1024); assert(pow(one, ten) == 1); assert(pow(ten, four) == 10_000); assert(pow(four, 10) == 1_048_576); assert(pow(three, four) == 81);
- Computes integer to floating point powers.
- Calculates xy.
- To what precision is x equal to y?
- Evaluate polynomial A(x) = a0 + a1x + a2x2 + a3x3; ...Uses Horner's rule A(x) = a0 + x(a1 + x(a2 + x(a3 + ...)))Parameters:
T1 x the value to evaluate. T2[] A array of coefficients a0, a1, etc. Examples:real x = 3.1; static real[] pp = [56.1, 32.7, 6]; assert(poly(x, pp) == (56.1L + (32.7L + 6.0L * x) * x));
- Computes whether lhs is approximately equal to rhs admitting a maximum relative difference maxRelDiff and a maximum absolute difference maxAbsDiff.
-
Examples:
assert(approxEqual(1.0, 1.0099)); assert(!approxEqual(1.0, 1.011)); float[] arr1 = [ 1.0, 2.0, 3.0 ]; double[] arr2 = [ 1.001, 1.999, 3 ]; assert(approxEqual(arr1, arr2)); real num = real.infinity; assert(num == real.infinity); // Passes. assert(approxEqual(num, real.infinity)); // Fails. num = -real.infinity; assert(num == -real.infinity); // Passes. assert(approxEqual(num, -real.infinity)); // Fails. assert(!approxEqual(3, 0)); assert(approxEqual(3, 3)); assert(approxEqual(3.0, 3)); assert(approxEqual([3, 3, 3], 3.0)); assert(approxEqual([3.0, 3.0, 3.0], 3)); int a = 10; assert(approxEqual(10, a));
- Defines a total order on all floating-point numbers.The order is defined as follows:
- All numbers in [-∞, +∞] are ordered the same way as by built-in comparison, with the exception of -0.0, which is less than +0.0;
- If the sign bit is set (that is, it's 'negative'), NAN is less than any number; if the sign bit is not set (it is 'positive'), NAN is greater than any number;
- NANs of the same sign are ordered by the payload ('negative' ones - in reverse order).
Returns:negative value if x precedes y in the order specified above; 0 if x and y are identical, and positive value otherwise.See Also:Standards:Conforms to IEEE 754-2008Examples:Most numbers are ordered naturally.assert(cmp(-double.infinity, -double.max) < 0); assert(cmp(-double.max, -100.0) < 0); assert(cmp(-100.0, -0.5) < 0); assert(cmp(-0.5, 0.0) < 0); assert(cmp(0.0, 0.5) < 0); assert(cmp(0.5, 100.0) < 0); assert(cmp(100.0, double.max) < 0); assert(cmp(double.max, double.infinity) < 0); assert(cmp(1.0, 1.0) == 0);
Examples:Positive and negative zeroes are distinct.assert(cmp(-0.0, +0.0) < 0); assert(cmp(+0.0, -0.0) > 0);
Examples:Depending on the sign, NANs go to either end of the spectrum.assert(cmp(-double.nan, -double.infinity) < 0); assert(cmp(double.infinity, double.nan) < 0); assert(cmp(-double.nan, double.nan) < 0);
Examples:NANs of the same sign are ordered by the payload.assert(cmp(NaN(10), NaN(20)) < 0); assert(cmp(-NaN(20), -NaN(10)) < 0);
- Gives the next power of two after val. <>> can be any built-in numerical type.Parameters:
T val any number Returns:the next power of two after valExamples:assert(nextPow2(2) == 4); assert(nextPow2(10) == 16); assert(nextPow2(4000) == 4096); assert(nextPow2(-2) == -4); assert(nextPow2(-10) == -16); assert(nextPow2(uint.max) == 1); assert(nextPow2(uint.min) == 0); assert(nextPow2(size_t.max) == 1); assert(nextPow2(size_t.min) == 0); assert(nextPow2(int.max) == int.min); assert(nextPow2(int.min) == -1); assert(nextPow2(long.max) == long.min); assert(nextPow2(long.min) == -1);
Examples:assert(nextPow2(2.1) == 4.0); assert(nextPow2(-2.0) == -4.0); assert(nextPow2(0.25) == 0.5); assert(nextPow2(-4.0) == -8.0); assert(nextPow2(double.max) == 0.0); assert(nextPow2(double.infinity) == double.infinity);
- Gives the last power of two before val. <>> can be any built-in numerical type.Parameters:
T val any number Returns:the last power of two before valExamples:assert(truncPow2(3) == 2); assert(truncPow2(4) == 4); assert(truncPow2(10) == 8); assert(truncPow2(4000) == 2048); assert(truncPow2(-5) == -4); assert(truncPow2(-20) == -16); assert(truncPow2(uint.max) == int.max + 1); assert(truncPow2(uint.min) == 0); assert(truncPow2(ulong.max) == long.max + 1); assert(truncPow2(ulong.min) == 0); assert(truncPow2(int.max) == (int.max / 2) + 1); assert(truncPow2(int.min) == int.min); assert(truncPow2(long.max) == (long.max / 2) + 1); assert(truncPow2(long.min) == long.min);
Examples:assert(truncPow2(2.1) == 2.0); assert(truncPow2(7.0) == 4.0); assert(truncPow2(-1.9) == -1.0); assert(truncPow2(0.24) == 0.125); assert(truncPow2(-7.0) == -4.0); assert(truncPow2(double.infinity) == double.infinity);
Copyright Digital Mars 2000 - 2011.
D implementations of tan, atan, atan2, exp, expm1, exp2, log, log10, log1p,
log2, floor, ceil and lrint functions are based on the CEPHES math library,
which is Copyright (C) 2001 Stephen L. Moshier <[email protected]>
and are incorporated herein by permission of the author. The author
reserves the right to distribute this material elsewhere under different
copying permissions. These modifications are distributed here under
the following terms:
| Page generated by
Ddoc on (no date time)