std.math.abs(int.min) == int.min?

Jens Mueller jens.k.mueller at gmx.de
Mon Jan 2 06:36:46 PST 2012


Hi,

in 2's complement the value T.min is special because its inverse is
itself. I'm using std.math.abs which computes the inverse using the
unary operator -. That's why it holds
assert(std.math.abs(int.min) == int.min)

Which is on one hand a bit strange because I'd expect the absolute of a
negative number to be positive. But on the other hand I see no way to
properly fix this. As the mathematical absolute of T.min is not in the
value range of T.
What is the post condition of std.math.abs? Must abs(x) >= 0 hold? One
could remove T.min from its definition range using a pre condition. This
is how it's done in C. But in D it should be done using contracts which
unfortunately will be compiled out in phobos (release mode is default).

In any case, IMHO the documentation needs clarification for this special
case.

Jens


More information about the Digitalmars-d mailing list