According to the IEEE floating point wikipage (on IEEE 754), there is a total order on double-precision floating points (i.e. on C++11 implementations having IEEE-754 floats, li
I would use:
int totalcompare(double x, double y) {
int64_t rx, ry;
memcpy(&rx, &x, sizeof rx);
memcpy(&ry, &y, sizeof ry);
if (rx == ry) return 0;
if (rx < 0) rx ^= INT64_MAX;
if (ry < 0) ry ^= INT64_MAX;
if (rx < ry) return -1; else return 1;
}
This makes 0.0
and -0.0
compare unequal, whereas if (x==y) return 0;
in your version makes them compare equal, meaning that your version is only a preorder. NaN
values are above the rest and different NaNs compare different. All values comparable for <=
should be in the same order for the above relation.
Note: the above function is C. I do not know C++.