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

Activity

Show:
Pavlos Vinieratos
October 14, 2016, 11:06 AM

fix for this issue

Alex Miller
March 7, 2014, 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.

import
March 7, 2014, 1:54 PM

Comment made by: yongqli

But in the first example it produces an ArithmeticException.

Alex Miller
March 7, 2014, 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

Assignee

Unassigned

Reporter

import

Labels

Approval

Ok

Patch

Code and Test

Priority

Trivial

Fix versions