diff --git a/index.js b/index.js index 4983233..8306b9f 100644 --- a/index.js +++ b/index.js @@ -1060,6 +1060,7 @@ LongPrototype.divide = function divide(divisor) { // positive number, due to two's complement. if ( !this.unsigned && + this.low === 0 && this.high === -0x80000000 && divisor.low === -1 && divisor.high === -1 diff --git a/tests/index.js b/tests/index.js index 3eab85e..53b4471 100644 --- a/tests/index.js +++ b/tests/index.js @@ -178,6 +178,23 @@ var tests = [ assert.strictEqual(longVal.toString(), Long.MIN_VALUE.toString()); }, + function testSignedNegHighWordDivNegOne() { + // A negative dividend whose high word is 0x80000000 but whose low word is + // non-zero must negate correctly when divided by -1. Only the exact + // MIN_VALUE / -1 is a true two's-complement overflow. + assert.strictEqual( + Long.fromString("-9223372036854775807").div(Long.fromInt(-1)).toString(), + "9223372036854775807", + ); + assert.strictEqual( + Long.fromString("-9223372036854775296").div(Long.fromInt(-1)).toString(), + "9223372036854775296", + ); + // The exact MIN_VALUE / -1 overflow still wraps to MIN_VALUE. + var overflow = Long.MIN_VALUE.div(Long.fromInt(-1)); + assert.strictEqual(overflow.toString(), Long.MIN_VALUE.toString()); + }, + function testUnsignedMsbUnsigned() { var longVal = Long.UONE.shiftLeft(63); assert.strictEqual(longVal.notEquals(Long.MIN_VALUE), true);