[CLJ-1371] divide(Object, Object) with (NaN, 0) does not return NaN Created: 07/Mar/14 Updated: 07/Mar/14
user=> (def x Double/NaN)
ArithmeticException Divide by zero clojure.lang.Numbers.divide (Numbers.java:156)
|Comment by Alex Miller [ 07/Mar/14 7:50 AM ]|
As per the Java Language Specification (http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.4),
"All numeric operations with NaN as an operand produce NaN as a result."
|Comment by Yongqian Li [ 07/Mar/14 7:54 AM ]|
But in the first example it produces an ArithmeticException.
|Comment by Alex Miller [ 07/Mar/14 9:27 AM ]|
Ah, I see the question now.
Here we are dividing a double by a long. In the first case, this is parsed as divide(Object, long) which then calls divide(Object, Object), which throws ArithmeticException if the second arg is 0 (regardless of the first arg).
In the second case it's parsed as divide(double, long) which just relies on Java to properly upcast the primitive long to a double to do the divide.
Note that making this call with 2 doubles does return NaN:
or type hinting x to a double works as well:
I think one option to "fix" this behavior would be to add checks in divide(Object, Object) to check whether x is NaN and instead return NaN.