Why is Java's double/float Math.min() implemented this way?
20
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?
java floating-point
NaN
and-0.0
. And if you don't know what they are and why they should be handled differently, then the JavaDoc doesn't really illuminate the issue. In other words: the JavaDoc is a good reference here, but not a good teacher. – Joachim Sauer 19 hours agostd::min
), not the always-NaN-propagating min(float, float) that's like C/C++fmin
). – Peter Cordes 8 hours agoMath.min
is an intrinsic in OpenJDK and as thus will have dedicated native implementations (that likely will map to the correct hardware instructions, if available), but that's definitely outside the scope of this question. – Joachim Sauer 7 hours ago