Skip to content

Commit

Permalink
Use this.unsigned in Long.fromString, fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jun 7, 2014
1 parent 1c2e981 commit d9673ca
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 28 deletions.
39 changes: 26 additions & 13 deletions Long.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,24 @@
*/
Long.ZERO = Long.fromInt(0);

/**
* @type {!Long}
* @expose
*/
Long.UZERO = Long.fromInt(0, true);

/**
* @type {!Long}
* @expose
*/
Long.ONE = Long.fromInt(1);

/**
* @type {!Long}
* @expose
*/
Long.UONE = Long.fromInt(1);

/**
* @type {!Long}
* @expose
Expand Down Expand Up @@ -702,27 +714,28 @@
if (other.isZero()) {
throw(new Error('division by zero'));
} else if (this.isZero()) {
return Long.ZERO;
return this.unsigned ? Long.UZERO : Long.ZERO;
}
var approx, rem, res;
if (this.equals(Long.MIN_SIGNED_VALUE)) {
if (other.equals(Long.ONE) || other.equals(Long.NEG_ONE)) {
return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
} else if (other.equals(Long.MIN_VALUE)) {
return Long.MIN_SIGNED_VALUE; // recall that -MIN_VALUE == MIN_VALUE
} else if (other.equals(Long.MIN_SIGNED_VALUE)) {
return Long.ONE;
} else {
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
var halfThis = this.shiftRight(1);
var approx = halfThis.div(other).shiftLeft(1);
approx = halfThis.div(other).shiftLeft(1);
if (approx.equals(Long.ZERO)) {
return other.isNegative() ? Long.ONE : Long.NEG_ONE;
} else {
var rem = this.subtract(other.multiply(approx));
var result = approx.add(rem.div(other));
return result;
rem = this.subtract(other.multiply(approx));
res = approx.add(rem.div(other));
return res;
}
}
} else if (other.equals(Long.MIN_VALUE)) {
return Long.ZERO;
} else if (other.equals(Long.MIN_SIGNED_VALUE)) {
return this.unsigned ? Long.UZERO : Long.ZERO;
}
if (this.isNegative()) {
if (other.isNegative()) {
Expand All @@ -733,18 +746,18 @@
} else if (other.isNegative()) {
return this.div(other.negate()).negate();
}

// 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.
var res = Long.ZERO;
var rem = this;
res = Long.ZERO;
rem = this;
while (rem.greaterThanOrEqual(other)) {
// Approximate the result of division. This may be a little greater or
// smaller than the actual value.
var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));

// We will tweak the approximate result by changing it in the 48-th digit or
// the smallest non-fractional digit, whichever is larger.
Expand Down
28 changes: 14 additions & 14 deletions Long.min.js

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

82 changes: 82 additions & 0 deletions docs/Long.html
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,88 @@ <h4 class="name" id="ONE"><span class="type-signature">&lt;static> </span>ONE<sp






</dl>



</dd>



<dt>
<h4 class="name" id="UONE"><span class="type-signature">&lt;static> </span>UONE<span class="type-signature"> :<a href="Long.html">Long</a></span></h4>


</dt>
<dd>



<dl class="details">























</dl>



</dd>



<dt>
<h4 class="name" id="UZERO"><span class="type-signature">&lt;static> </span>UZERO<span class="type-signature"> :<a href="Long.html">Long</a></span></h4>


</dt>
<dd>



<dl class="details">























Expand Down
10 changes: 10 additions & 0 deletions externs/Long.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,21 @@ Long.fromString = function(str, unsigned, radix) {};
*/
Long.ZERO;

/**
* @type {!Long}
*/
Long.UZERO;

/**
* @type {!Long}
*/
Long.ONE;

/**
* @type {!Long}
*/
Long.UONE;

/**
* @type {!Long}
*/
Expand Down
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": "1.1.2",
"version": "1.1.3",
"author": "Daniel Wirtz <[email protected]>",
"description": "A Long class for representing a 64-bit two's-complement integer value.",
"main": "Long.js",
Expand Down

0 comments on commit d9673ca

Please sign in to comment.