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.cmp
Defines a total order on all floating-point numbers.
int cmp(T)
(
const(T) x,
const(T) y
) pure nothrow @nogc @trusted
if (isFloatingPoint!T);
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).
Returns
negative value if x
precedes y
in the order specified above;
0 if x
and y
are identical, and positive value otherwise.
See Also
Standards
Conforms to IEEE 754-2008
Example
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);
writeln(cmp(1.0, 1.0)); // 0
Example
Positive and negative zeroes are distinct.
assert(cmp(-0.0, +0.0) < 0);
assert(cmp(+0.0, -0.0) > 0);
Example
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);
Example
NANs of the same sign are ordered by the payload.
assert(cmp(NaN(10), NaN(20)) < 0);
assert(cmp(-NaN(20), -NaN(10)) < 0);
Authors
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger
License
Copyright © 1999-2018 by the D Language Foundation | Page generated by ddox.