Function std.math.abs
Calculates the absolute value of a number
Prototypes
Num abs(Num)( Num x ) pure nothrow @safe if (is(typeof(Num.init >= 0)) && is(typeof(-Num.init)) && !(is(Num* : const(ifloat*)) || is(Num* : const(idouble*)) || is(Num* : const(ireal*)))); auto abs(Num)( Num z ) pure nothrow @nogc @safe if (is(Num* : const(cfloat*)) || is(Num* : const(cdouble*)) || is(Num* : const(creal*))); auto abs(Num)( Num y ) pure nothrow @nogc @safe if (is(Num* : const(ifloat*)) || is(Num* : const(idouble*)) || is(Num* : const(ireal*)));
Parameters
Name | Description |
---|---|
Num | (template parameter) type of number |
x | real number value |
z | complex number value |
y | imaginary number value |
Returns
The absolute value of the number. If floating-point or integral, the return type will be the same as the input; if complex or imaginary, the returned value will be the corresponding floating point type.
For complex numbers, abs
(z
) = sqrt
( z.re, 2 + z.im, 2 )
= hypot
(z.re, z.im).
Example
ditto
assert(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));
Authors
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw