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?

Share
Improve this question
6
  • 20
    You do not seem to have read the documentation or at least you do not refer to it. While looking at code is nice, looking at documentation is important too. – Trilarion 19 hours ago
  • 10
    @Trilarion: while this behaviour is explained in the JavaDoc, it's very possible that it's the first time that many people will see 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 ago
  • 2
    related: What is the instruction that gives branchless FP min and max on x86? - the hardware instruction implements the simple ternary (like C++ std::min), not the always-NaN-propagating min(float, float) that's like C/C++ fmin). – Peter Cordes 8 hours ago
  • @PeterCordes: that's a fascinating read. For anyone interested in what Java actually executes: I'm almost sure that every overload of Math.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
  • 1
    Related: How can a Java variable be different from itself? and How can a primitive float value be -0.0? What does that mean?. Note that one variable is called "negativeZero...", from where one might reasonably try to Google what "negative zero" is (in Java or elsewhere). – Bernhard Barker 7 hours ago

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue