cmp

Defines a total order on all floating-point numbers.

The order is defined as follows:

  • All numbers in [-∞, +∞] are ordered the same way as by built-in comparison, with the exception of -0.0, which is less than +0.0;
  • If the sign bit is set (that is, it's 'negative'), NaN is less than any number; if the sign bit is not set (it is 'positive'), NaN is greater than any number;
  • NaNs of the same sign are ordered by the payload ('negative' ones - in reverse order).
@nogc @trusted pure nothrow
int
cmp
(
T
)
(
const(T) x
,
const(T) y
)

Return Value

Type: int

negative value if x precedes y in the order specified above; 0 if x and y are identical, and positive value otherwise.

Examples

Most numbers are ordered naturally.

assert(cmp(-double.infinity, -double.max) < 0);
assert(cmp(-double.max, -100.0) < 0);
assert(cmp(-100.0, -0.5) < 0);
assert(cmp(-0.5, 0.0) < 0);
assert(cmp(0.0, 0.5) < 0);
assert(cmp(0.5, 100.0) < 0);
assert(cmp(100.0, double.max) < 0);
assert(cmp(double.max, double.infinity) < 0);

assert(cmp(1.0, 1.0) == 0);

Positive and negative zeroes are distinct.

assert(cmp(-0.0, +0.0) < 0);
assert(cmp(+0.0, -0.0) > 0);

Depending on the sign, NaNs go to either end of the spectrum.

assert(cmp(-double.nan, -double.infinity) < 0);
assert(cmp(double.infinity, double.nan) < 0);
assert(cmp(-double.nan, double.nan) < 0);

NaNs of the same sign are ordered by the payload.

assert(cmp(NaN(10), NaN(20)) < 0);
assert(cmp(-NaN(20), -NaN(10)) < 0);

See Also

Meta

Standards

Conforms to IEEE 754-2008