View source code
Display the source code in std/math/algebraic.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.algebraic.abs

Calculates the absolute value of a number.

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


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


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


When x is a signed integral equal to Num.min the value of x will be returned instead. Note for 2's complement; -Num.min (= Num.max + 1) is not representable due to overflow.


import std.math.traits : isIdentical, isNaN;

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


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


Boost License 1.0.