Here's a revised version of the patch following suggestions from Andrew
Haley.
Ian
--- java/lang/Float.java 2006-12-10 15:25:44.000000000 -0500
+++ java/lang/Float.java 2007-07-02 12:17:29.000000000 -0400
@@ -596,16 +596,25 @@
*/
public static int compare(float x, float y)
{
- if (isNaN(x))
- return isNaN(y) ? 0 : 1;
- if (isNaN(y))
- return -1;
- // recall that 0.0 == -0.0, so we convert to infinities and try again
- if (x == 0 && y == 0)
- return (int) (1 / x - 1 / y);
- if (x == y)
- return 0;
+ // handle the easy cases:
+ if (x < y)
+ return -1;
+ if (x > y)
+ return 1;
- return x > y ? 1 : -1;
+ // handle equality respecting that 0.0 != -0.0 (hence not using x == y):
+ int ix = floatToRawIntBits(x);
+ int iy = floatToRawIntBits(y);
+ if (ix == iy)
+ return 0;
+
+ // handle NaNs:
+ if (x != x)
+ return (y != y) ? 0 : 1;
+ else if (y != y)
+ return -1;
+
+ // handle +/- 0.0
+ return (ix < iy) ? -1 : 1;
}
}
--- java/lang/Double.java 2006-12-10 15:25:44.000000000 -0500
+++ java/lang/Double.java 2007-07-02 12:18:36.000000000 -0400
@@ -587,16 +587,25 @@
*/
public static int compare(double x, double y)
{
- if (isNaN(x))
- return isNaN(y) ? 0 : 1;
- if (isNaN(y))
- return -1;
- // recall that 0.0 == -0.0, so we convert to infinites and try again
- if (x == 0 && y == 0)
- return (int) (1 / x - 1 / y);
- if (x == y)
- return 0;
+ // handle the easy cases:
+ if (x < y)
+ return -1;
+ if (x > y)
+ return 1;
- return x > y ? 1 : -1;
+ // handle equality respecting that 0.0 != -0.0 (hence not using x == y):
+ long lx = doubleToRawLongBits(x);
+ long ly = doubleToRawLongBits(y);
+ if (lx == ly)
+ return 0;
+
+ // handle NaNs:
+ if (x != x)
+ return (y != y) ? 0 : 1;
+ else if (y != y)
+ return -1;
+
+ // handle +/- 0.0
+ return (lx < ly) ? -1 : 1;
}
}