divide(Object, Object) with (NaN, 0) does not return NaN

Description

Would expect both of these to return NaN but first does not:

Cause: 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:

Proposed: Add checks in divide(Object, Object) to check whether x is NaN and instead return NaN.

Patch: clj-1371-2.patch
Prescreened by: Alex Miller

Environment

None

Attachments

2

Activity

Show:

Pavlos VinieratosOctober 14, 2016 at 11:06 AM

fix for this issue

Alex MillerMarch 7, 2014 at 3:27 PM

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.

importMarch 7, 2014 at 1:54 PM

Comment made by: yongqli

But in the first example it produces an ArithmeticException.

Alex MillerMarch 7, 2014 at 1:50 PM

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."

Completed

Details

Assignee

Reporter

Labels

Approval

Ok

Patch

Code and Test

Priority

Fix versions

Created March 7, 2014 at 8:36 AM
Updated September 6, 2017 at 5:42 PM
Resolved September 6, 2017 at 5:42 PM