Incorrect divide-by-zero error with floating point numbers

Description

The unary call for clojure.core// treats a dividend of 0.0 differently than the binary call, likely due to inlining.

Environment

None

Activity

Show:
Tim McCormack
August 18, 2016, 12:48 PM

I don't know if this affects any other instance of division except divide-by-zero, so it's possibly not a practical problem.

I regard code that only fails sometimes as worse, because then bugs are more likely to get caught in production instead of development.

import
August 18, 2016, 5:17 AM

Comment made by: csm

I think the issue here is that in clojure.lang.Numbers, different variants of divide have different semantics. Numbers.divide(Object, Object) performs the isZero check, but Numbers.divide(long, double) does not (it just uses Java's division operator, which since the denominator is a double with value 0.0, produces Infinity).

A statement like (/ 1 0.0) gets compiled to call Numbers.divide(long, double), and thus produces Infinity. If the second argument is a function call or a var, it looks like an Object, so it gets compiled to use Numbers.divide(Object, Object), and that call throws when the second arg is zero (actually it compiles to a call to Numbers.divide(long, Object), but that just boxes the first argument and calls the other variant).

It does seem incorrect to have different semantics for division based on the inferred type at compile time; however, I don't know if this affects any other instance of division except divide-by-zero, so it's possibly not a practical problem.

Tim McCormack
January 9, 2013, 5:22 AM

The relevant code seems to be this in clojure.lang.Numbers/divide:

Making Numbers/divide be more restrictive than double arithmetic seems like a bug; explicitly throwing an ArithmeticException instead of letting the JVM figure it just seems like more work than necessary.

Your pinned fields
Click on the next to a field label to start pinning.

Assignee

Unassigned

Reporter

Tim McCormack

Labels

Approval

Triaged

Priority

Minor

Affects versions