[CLJ-1142] Incorrect divide-by-zero error with floating point numbers Created: 08/Jan/13 Updated: 18/Aug/16
|Affects Version/s:||Release 1.4|
The unary call for clojure.core// treats a dividend of 0.0 differently than the binary call, likely due to inlining.
|Comment by Tim McCormack [ 08/Jan/13 11:22 PM ]|
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.
|Comment by Casey Marshall [ 17/Aug/16 11:17 PM ]|
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.
|Comment by Tim McCormack [ 18/Aug/16 6:48 AM ]|
I regard code that only fails sometimes as worse, because then bugs are more likely to get caught in production instead of development.