I was looking through some things in the source of java.lang.Math
, and I noticed that while Math.min(int, int)
(or its long counterpart) is implemented this way:
public static int min(int a, int b) {
return a <= b ? a : b;
}
And this makes complete sense to me and it is the same as what I would do. However, the double/float implementation is this:
public static float min(float a, float b) {
if (a != a) {
return a;
} else if (a == 0.0F && b == 0.0F && (long)Float.floatToRawIntBits(b) == negativeZeroFloatBits) {
return b;
} else {
return a <= b ? a : b;
}
}
I'm completely dumbfounded. Comparing a
to itself? What's the second check even for? Why isn't it implemented in the same way as the int/long version?