abs

Calculates the absolute value of a number.

@nogc pure nothrow
abs
(
Num
)
(
Num x
)
if (
(
is(immutable Num == immutable short) ||
is(immutable Num == immutable byte)
)
||
(
is(typeof(Num.init >= 0)) &&
is(typeof(-Num.init))
)
)

Parameters

Num

(template parameter) type of number

x Num

real number value

Return Value

Type: auto

The absolute value of the number. If floating-point or integral, the return type will be the same as the input.

Limitations: Does not work correctly for signed intergal types and value Num.min.

Examples

ditto

import std.math.traits : isIdentical, isNaN;

assert(isIdentical(abs(-0.0L), 0.0L));
assert(isNaN(abs(real.nan)));
assert(abs(-real.infinity) == real.infinity);
assert(abs(-56) == 56);
assert(abs(2321312L)  == 2321312L);

Meta