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.

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

NameDescription
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

License

Boost License 1.0.

Comments