Skip to content

Commit

Permalink
Division of unsigned values always yields unsigned results, fixes dco…
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Mar 31, 2016
1 parent 27b43e5 commit 7ccf3dd
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 94 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "long",
"version": "3.0.3",
"version": "3.1.0",
"author": "Daniel Wirtz <[email protected]>",
"description": "A Long class for representing a 64 bit two's-complement integer value.",
"main": "dist/long.js",
Expand Down
66 changes: 40 additions & 26 deletions dist/long.js
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,8 @@
LongPrototype.mul = LongPrototype.multiply;

/**
* Returns this Long divided by the specified.
* Returns this Long divided by the specified. The result is signed if this Long is signed or
* unsigned if this Long is unsigned.
* @param {!Long|number|string} divisor Divisor
* @returns {!Long} Quotient
* @expose
Expand All @@ -961,38 +962,51 @@
if (this.isZero())
return this.unsigned ? UZERO : ZERO;
var approx, rem, res;
if (this.eq(MIN_VALUE)) {
if (divisor.eq(ONE) || divisor.eq(NEG_ONE))
return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
else if (divisor.eq(MIN_VALUE))
return ONE;
else {
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
var halfThis = this.shr(1);
approx = halfThis.div(divisor).shl(1);
if (approx.eq(ZERO)) {
return divisor.isNegative() ? ONE : NEG_ONE;
} else {
rem = this.sub(divisor.mul(approx));
res = approx.add(rem.div(divisor));
return res;
if (!this.unsigned) {
if (this.eq(MIN_VALUE)) {
if (divisor.eq(ONE) || divisor.eq(NEG_ONE))
return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
else if (divisor.eq(MIN_VALUE))
return ONE;
else {
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
var halfThis = this.shr(1);
approx = halfThis.div(divisor).shl(1);
if (approx.eq(ZERO)) {
return divisor.isNegative() ? ONE : NEG_ONE;
} else {
rem = this.sub(divisor.mul(approx));
res = approx.add(rem.div(divisor));
return res;
}
}
}
} else if (divisor.eq(MIN_VALUE))
return this.unsigned ? UZERO : ZERO;
if (this.isNegative()) {
if (divisor.isNegative())
return this.neg().div(divisor.neg());
return this.neg().div(divisor).neg();
} else if (divisor.isNegative())
return this.div(divisor.neg()).neg();
} else if (divisor.eq(MIN_VALUE))
return this.unsigned ? UZERO : ZERO;
if (this.isNegative()) {
if (divisor.isNegative())
return this.neg().div(divisor.neg());
return this.neg().div(divisor).neg();
} else if (divisor.isNegative())
return this.div(divisor.neg()).neg();
} else if (!divisor.unsigned)
divisor = divisor.toUnsigned();

// The algorithm below has not been made for unsigned longs. It's therefore
// required to take special care of the MSB prior to running it.
if (this.unsigned) {
if (divisor.gt(this))
return UZERO;
if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true
return UONE;
res = UZERO;
} else
res = ZERO;

// Repeat the following until the remainder is less than other: find a
// floating-point that approximates remainder / other *from below*, add this
// into the result, and subtract it from the remainder. It is critical that
// the approximate value is less than or equal to the real value so that the
// remainder never becomes negative.
res = ZERO;
rem = this;
while (rem.gte(divisor)) {
// Approximate the result of division. This may be a little greater or
Expand Down
25 changes: 13 additions & 12 deletions dist/long.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/long.min.js.gz
Binary file not shown.
6 changes: 3 additions & 3 deletions dist/long.min.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "long",
"version": "3.0.3",
"version": "3.1.0",
"author": "Daniel Wirtz <[email protected]>",
"description": "A Long class for representing a 64-bit two's-complement integer value.",
"main": "dist/long.js",
Expand Down
Loading

0 comments on commit 7ccf3dd

Please sign in to comment.