Skip to content

Commit d9673ca

Browse files
committed
Use this.unsigned in Long.fromString, fixes #6
1 parent 1c2e981 commit d9673ca

5 files changed

Lines changed: 133 additions & 28 deletions

File tree

Long.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,24 @@
283283
*/
284284
Long.ZERO = Long.fromInt(0);
285285

286+
/**
287+
* @type {!Long}
288+
* @expose
289+
*/
290+
Long.UZERO = Long.fromInt(0, true);
291+
286292
/**
287293
* @type {!Long}
288294
* @expose
289295
*/
290296
Long.ONE = Long.fromInt(1);
291297

298+
/**
299+
* @type {!Long}
300+
* @expose
301+
*/
302+
Long.UONE = Long.fromInt(1);
303+
292304
/**
293305
* @type {!Long}
294306
* @expose
@@ -702,27 +714,28 @@
702714
if (other.isZero()) {
703715
throw(new Error('division by zero'));
704716
} else if (this.isZero()) {
705-
return Long.ZERO;
717+
return this.unsigned ? Long.UZERO : Long.ZERO;
706718
}
719+
var approx, rem, res;
707720
if (this.equals(Long.MIN_SIGNED_VALUE)) {
708721
if (other.equals(Long.ONE) || other.equals(Long.NEG_ONE)) {
709-
return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
710-
} else if (other.equals(Long.MIN_VALUE)) {
722+
return Long.MIN_SIGNED_VALUE; // recall that -MIN_VALUE == MIN_VALUE
723+
} else if (other.equals(Long.MIN_SIGNED_VALUE)) {
711724
return Long.ONE;
712725
} else {
713726
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
714727
var halfThis = this.shiftRight(1);
715-
var approx = halfThis.div(other).shiftLeft(1);
728+
approx = halfThis.div(other).shiftLeft(1);
716729
if (approx.equals(Long.ZERO)) {
717730
return other.isNegative() ? Long.ONE : Long.NEG_ONE;
718731
} else {
719-
var rem = this.subtract(other.multiply(approx));
720-
var result = approx.add(rem.div(other));
721-
return result;
732+
rem = this.subtract(other.multiply(approx));
733+
res = approx.add(rem.div(other));
734+
return res;
722735
}
723736
}
724-
} else if (other.equals(Long.MIN_VALUE)) {
725-
return Long.ZERO;
737+
} else if (other.equals(Long.MIN_SIGNED_VALUE)) {
738+
return this.unsigned ? Long.UZERO : Long.ZERO;
726739
}
727740
if (this.isNegative()) {
728741
if (other.isNegative()) {
@@ -733,18 +746,18 @@
733746
} else if (other.isNegative()) {
734747
return this.div(other.negate()).negate();
735748
}
736-
749+
737750
// Repeat the following until the remainder is less than other: find a
738751
// floating-point that approximates remainder / other *from below*, add this
739752
// into the result, and subtract it from the remainder. It is critical that
740753
// the approximate value is less than or equal to the real value so that the
741754
// remainder never becomes negative.
742-
var res = Long.ZERO;
743-
var rem = this;
755+
res = Long.ZERO;
756+
rem = this;
744757
while (rem.greaterThanOrEqual(other)) {
745758
// Approximate the result of division. This may be a little greater or
746759
// smaller than the actual value.
747-
var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
760+
approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
748761

749762
// We will tweak the approximate result by changing it in the 48-th digit or
750763
// the smallest non-fractional digit, whichever is larger.

Long.min.js

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/Long.html

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,88 @@ <h4 class="name" id="ONE"><span class="type-signature">&lt;static> </span>ONE<sp
555555

556556

557557

558+
559+
560+
561+
</dl>
562+
563+
564+
565+
</dd>
566+
567+
568+
569+
<dt>
570+
<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>
571+
572+
573+
</dt>
574+
<dd>
575+
576+
577+
578+
<dl class="details">
579+
580+
581+
582+
583+
584+
585+
586+
587+
588+
589+
590+
591+
592+
593+
594+
595+
596+
597+
598+
599+
600+
601+
602+
</dl>
603+
604+
605+
606+
</dd>
607+
608+
609+
610+
<dt>
611+
<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>
612+
613+
614+
</dt>
615+
<dd>
616+
617+
618+
619+
<dl class="details">
620+
621+
622+
623+
624+
625+
626+
627+
628+
629+
630+
631+
632+
633+
634+
635+
636+
637+
638+
639+
558640

559641

560642

externs/Long.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,21 @@ Long.fromString = function(str, unsigned, radix) {};
7878
*/
7979
Long.ZERO;
8080

81+
/**
82+
* @type {!Long}
83+
*/
84+
Long.UZERO;
85+
8186
/**
8287
* @type {!Long}
8388
*/
8489
Long.ONE;
8590

91+
/**
92+
* @type {!Long}
93+
*/
94+
Long.UONE;
95+
8696
/**
8797
* @type {!Long}
8898
*/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "long",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"author": "Daniel Wirtz <dcode@dcode.io>",
55
"description": "A Long class for representing a 64-bit two's-complement integer value.",
66
"main": "Long.js",

0 commit comments

Comments
 (0)