Clojure JIRAhttp://dev.clojure.org/jira
This file is an XML representation of an issueen-us4.464925-07-2011[CLJ-1225] quot overflow issues around Long/MIN_VALUE for BigInthttp://dev.clojure.org/jira/browse/CLJ-1225
Clojure<p>In Clojure 1.5.1, see the following undesirable behavior regarding incorrect quot results for BigInts:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>user=&gt; (quot Long/MIN_VALUE -1N)
-9223372036854775808N
user=&gt; (quot (bigint Long/MIN_VALUE) -1)
-9223372036854775808N
</pre>
</div></div>
<p>Similar issue to <a href="http://dev.clojure.org/jira/browse/CLJ-1222" title="Multiplication overflow issues around Long/MIN_VALUE"><del>CLJ-1222</del></a>. The root cause is that Java division of longs gives a numerically incorrect answer of Long.MIN_VALUE for (Long.MIN_VALUE / -1), because the numerically correct answer does not fit in a long. I believe this is the only pair of arguments for long division that gives a numerically incorrect answer, because division with a denominator having an absolute value of 2 or more gives a result closer to 0 than the numerator, and everything works fine for a denominator of 1 or -1, except this one case.</p>
<p>Related issues: <a href="http://dev.clojure.org/jira/browse/CLJ-1222" title="Multiplication overflow issues around Long/MIN_VALUE"><del>CLJ-1222</del></a> for multiply, <a href="http://dev.clojure.org/jira/browse/CLJ-1253" title="Incorrect long division involving Long/MIN_VALUE">CLJ-1253</a> for / on longs, <a href="http://dev.clojure.org/jira/browse/CLJ-1254" title="Incorrect long quot result involving Long/MIN_VALUE">CLJ-1254</a> for quot on longs</p>
<p><b>Patch:</b> clj-1225-2.txt<br/>
<b>Screened by:</b> Alex Miller</p>CLJ-1225quot overflow issues around Long/MIN_VALUE for BigIntDefectMinorClosedCompletedUnassignedAndy FingerhutftmathTue, 25 Jun 2013 10:55:58 -0500Fri, 31 Jul 2015 15:39:54 -0500Fri, 31 Jul 2015 15:39:54 -0500Release 1.5Release 1.811<p>Patch clj-1225-fix-division-overflow-patch-v1.txt dated Jun 25 2013 may be one good way to address this issue. It modifies quot and / to return the numerically correct (BigInt) answer when given args Long/MIN_VALUE and -1.</p>
<p>It also removes the quotient intrinsic that does a JVM LDIV operation on longs for quot, since that operation is one of those that gives the incorrect result. I have not done any performance testing with this patch yet, but I have verified that it does not introduce any new reflection warnings when compiling Clojure itself.</p><p>Another possible approach would be to create unchecked-quotient and quot', which together with quot would correspond to the existing unchecked-multiply, *' and *. That is a more significant change. One potential concern it addresses that patch clj-1225-fix-division-overflow-patch-v1.txt does not is that patch leaves a Clojure developer with no way to do a primitive Java long division except by writing Java code.</p><p>this is two separate issues, one with longs and one with bigints. long problem should throw</p><p>Updating description for BigInt issue only. Will create separate ticket for incorrect behavior of / and quot on long type args Long/MIN_VALUE and -1.</p><p>Patch clj-1225-2.txt fixes this issue with quot on BigInts, with tests for quot and / on these values. / on BigInt worked fine before, but added the tests in case someone decides to change the implementation and forgets this corner case.</p>ApprovalOkGlobal RankPatchCode and Test