|
283 | 283 | */ |
284 | 284 | Long.ZERO = Long.fromInt(0); |
285 | 285 |
|
| 286 | + /** |
| 287 | + * @type {!Long} |
| 288 | + * @expose |
| 289 | + */ |
| 290 | + Long.UZERO = Long.fromInt(0, true); |
| 291 | + |
286 | 292 | /** |
287 | 293 | * @type {!Long} |
288 | 294 | * @expose |
289 | 295 | */ |
290 | 296 | Long.ONE = Long.fromInt(1); |
291 | 297 |
|
| 298 | + /** |
| 299 | + * @type {!Long} |
| 300 | + * @expose |
| 301 | + */ |
| 302 | + Long.UONE = Long.fromInt(1); |
| 303 | + |
292 | 304 | /** |
293 | 305 | * @type {!Long} |
294 | 306 | * @expose |
|
702 | 714 | if (other.isZero()) { |
703 | 715 | throw(new Error('division by zero')); |
704 | 716 | } else if (this.isZero()) { |
705 | | - return Long.ZERO; |
| 717 | + return this.unsigned ? Long.UZERO : Long.ZERO; |
706 | 718 | } |
| 719 | + var approx, rem, res; |
707 | 720 | if (this.equals(Long.MIN_SIGNED_VALUE)) { |
708 | 721 | 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)) { |
711 | 724 | return Long.ONE; |
712 | 725 | } else { |
713 | 726 | // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. |
714 | 727 | var halfThis = this.shiftRight(1); |
715 | | - var approx = halfThis.div(other).shiftLeft(1); |
| 728 | + approx = halfThis.div(other).shiftLeft(1); |
716 | 729 | if (approx.equals(Long.ZERO)) { |
717 | 730 | return other.isNegative() ? Long.ONE : Long.NEG_ONE; |
718 | 731 | } 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; |
722 | 735 | } |
723 | 736 | } |
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; |
726 | 739 | } |
727 | 740 | if (this.isNegative()) { |
728 | 741 | if (other.isNegative()) { |
|
733 | 746 | } else if (other.isNegative()) { |
734 | 747 | return this.div(other.negate()).negate(); |
735 | 748 | } |
736 | | - |
| 749 | + |
737 | 750 | // Repeat the following until the remainder is less than other: find a |
738 | 751 | // floating-point that approximates remainder / other *from below*, add this |
739 | 752 | // into the result, and subtract it from the remainder. It is critical that |
740 | 753 | // the approximate value is less than or equal to the real value so that the |
741 | 754 | // remainder never becomes negative. |
742 | | - var res = Long.ZERO; |
743 | | - var rem = this; |
| 755 | + res = Long.ZERO; |
| 756 | + rem = this; |
744 | 757 | while (rem.greaterThanOrEqual(other)) { |
745 | 758 | // Approximate the result of division. This may be a little greater or |
746 | 759 | // 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())); |
748 | 761 |
|
749 | 762 | // We will tweak the approximate result by changing it in the 48-th digit or |
750 | 763 | // the smallest non-fractional digit, whichever is larger. |
|
0 commit comments