Egor Pasko wrote:
> I'd suggest a slightly different code path: move native calls further
> below. floatToRawIntBits() is *very* slow and only useful for -0f
> checking. Hitting two zeroes is less likely than hitting a NaN IMHO.
Thanks Egor. I've reproduced the full method below for clarity and
further comment:
public static int compare(float float1, float float2) {
// Non-zero, non-NaN checking.
if (float1 > float2) {
return 1;
}
if (float2 > float1) {
return -1;
}
if (float1 == float2 && (0.0f != float1 || 0.0f != float2)) {
return 0;
}
// NaNs are equal to other NaNs and larger than any other float
if (isNaN(float1)) {
if (isNaN(float2)) {
return 0;
}
return 1;
} else if (isNaN(float2)) {
return -1;
}
// Deal with +0.0 and -0.0
int f1 = floatToRawIntBits(float1);
int f2 = floatToRawIntBits(float2);
if (f1 == f2) {
return 0;
}
return (f1 < f2) ? -1 : 1;
}