Incorrect long division involving Long/MIN_VALUE

Description

user=> (/ Long/MIN_VALUE -1) -9223372036854775808

Similar issue to CLJ-1222, with the same root cause as described for CLJ-1225.

Environment

None

Attachments

1
  • 06 Sep 2013, 02:56 PM

Activity

Show:

Alex Miller October 5, 2021 at 5:28 PM

This matches Java behavior, which is specifically called out in JLS https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.2:

“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.”

Alex Miller October 5, 2021 at 5:26 PM

This matches Java behavior, which is specifically called out in JLS https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.2:

> 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.

Andy Fingerhut September 6, 2013 at 2:56 PM

Patch clj-1253-1.txt corrects LongOps method divide for the case of args Long/MIN_VALUE and -1. It returns a BigInt in this case, not a Long, but most other pairs of values passed to this function return a Ratio exact answer, so it seems reasonable in this one case to return a BigInt exact answer when it will not fit in a Long.

Won't Fix

Details

Assignee

Reporter

Labels

Approval

Patch

Priority

Affects versions

Created September 6, 2013 at 2:37 PM
Updated October 5, 2021 at 5:28 PM
Resolved October 5, 2021 at 5:26 PM