Doubled up
Can you guess the output of the code below? And no, it isn’t a compilation failure, even I can write code which compiles (at least after enough coffee)
double d1 = Double.NaN;
double d2 = Double.NaN;
String verdict = "Not guilty";
if ( d1 == d2 ) {
if ( ! new Double(d1).equals(d2) ) {
verdict = 0 < Double.MAX_VALUE ?
"less than MAX_VALUE" :
"greater than MAX_VALUE";
}
} else {
verdict = 0 > Double.MIN_VALUE ?
"greater than MIN_VALUE" :
"less than MIN_VALUE";
}
System.out.println(verdict);
}
Well in fact it prints out ‘less then MIN_VALUE’, so if you got that result - well done!
There are two slightly counter intuituve aspects to this example.
Firstly Double.NaN != Double.NaN. although new Double(Double.NaN).equals(Double.NaN);
This is reasonably widely known, but also the cause of many bugs, since it is all too easy to forget the == comparator does not work in this case. To be safe you have to remember to use the isNaN() method on Double, if testing NaN conditions.
Secondly, since Double.MAX_VALUE is the maximum value a Double can take (apart from inifinity), it might be natural to think that Double.MIN_VALUE is the minimum value a Double can take. In fact, it is not - it is actually the minimum positive value a Double can take! Easy to overlook, and once again, a potential cause of many bugs.
We hit a bug with this recently when writing a max value calculator which took MIN_VALUE as its starting point, and iterated through a set of values. During the iteration, if a subsequent value was higher than the currently stored max value, its value was taken as the new max. This all worked brilliantly - until we ran the calculator against a number set consisting of just negative values, in which case the result was returned as MIN_VALUE!
Surely a better name for this constant would be MIN_POSITIVE_VALUE.
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply