Fix two different long overflow bugs in BigDecimal division.
The two bugs were in the line:
longCompareTo(Math.abs(rem) * 2,Math.abs(divisor))
- The first bug is that divisor can be Long.MIN_VALUE, abs() of
which overflows back to Long.MIN_VALUE. This caused e.g. the
following to evaluate to -1 when it should be 0:
BigDecimal.ONE.divide(new BigDecimal(Long.MIN_VALUE, 0, HALF_EVEN)
- The second bug is that Math.abs(rem) can be > 2^62, again
leading to overflow. This caused the following to evaluate to
0 when it should be 1:
new BigDecimal(Long.MIN_VALUE / 2 - 1)
.divide(new BigDecimal(Long.MIN_VALUE), 0, HALF_DOWN)
Bug: 28398726
Change-Id: I33427c005d2928b345c5d5fa2e04dbe3fb672371
2 files changed