View source code
Display the source code in std/math.d from which this page was generated on github.
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 local clone.

Function std.math.abs

Calculates the absolute value of a number.

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

Parameters

NameDescription
Num (template parameter) type of number
x real number value

Returns

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.

Example

ditto

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

Authors

Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger

License

Boost License 1.0.