Fork me on GitHub
#clojure-dev
<
2021-10-05
>
pyr10:10:58

Looking back at it, I don't get how (/ -9223372036854775808 -1) is not a bug, should I file a Jira or do something in ask Clojure?

thumbnail11:10:41

(/ -9223372036854775808 -1)
=> -9223372036854775808
(/ -9223372036854775808M -1)
=> 9223372036854775808M
As a sidenote; Bignums work as expected

pyr11:10:14

yes, they do fortunately. Other operations like these tend to fail (as would (inc 9223372036854775807) which would make sense

pyr11:10:30

silently returning the wrong result feels wrong

1
Alex Miller (Clojure team)13:10:53

there are three families of arithmetic operations - overflow checked (the default), unchecked (via unchecked-ops and/or unchecked dynvar), and promoting (via op'). you say above "In most other cases there is a silent type promotion" - this is wrong. Automatic type promotion occurs when you have a mixture of types - then automatic widening promotion occurs, but that is not relevant here. This case is the default / operator, which has overflow check semantics. But, this case is overflowing and does not throw - why? This specific case is special in Java (and thus also in Clojure which is trying to surface the same safe, fast default Java behavior here). See https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.2 says: > There is one special case that does not satisfy this rule: if the dividend is the negative integer of largest possible magnitude for its type, and the divisor is -1, then integer overflow occurs and the result is equal to the dividend. Despite the overflow, no exception is thrown in this case.

😮 2
🤯 2
Alex Miller (Clojure team)13:10:06

in general, per "where do I ask this question", it is always ok and preferred to ask it in http://ask.clojure.org - that allows other people to see it and answer it, the conversation to remain persistent (vs here, logging aside), and for me to track and log it as a jira if appropriate.

pyr13:10:34

@alexmiller Yes right, I was wrong about silent promotion and got confused with old behavior.

pyr13:10:14

In the meantime found out the same info in a different place: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorDiv-long-long-

pyr13:10:04

@alexmiller Thanks about the confirmation anyway