Skip to content

Commit

Permalink
Implement integer power method base on suggestions from #46
Browse files Browse the repository at this point in the history
  • Loading branch information
CyDragon80 authored and CyDragon80 committed Jun 18, 2018
1 parent 95ac93e commit 067aa40
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ API
* Long#**divide**/**div**(divisor: `Long | number | string`): `Long`<br />
Returns this Long divided by the specified.

* Long#**clone**(): `Long`<br />
Returns a deep copy of this Long.

* Long#**power**/**pow**(exp: `Long | number | string`): `Long`<br />
Returns this Long to given integer power.

* Long#**equals**/**eq**(other: `Long | number | string`): `boolean`<br />
Tests if this Long's value equals the specified's.

Expand Down
2 changes: 1 addition & 1 deletion dist/long.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/long.js.map

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions externs/long.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,23 @@ Long.prototype.divide = function(other) {};
*/
Long.prototype.div = function(other) {};

/**
* @return {!Long}
*/
Long.prototype.clone = function() {};

/**
* @param {!Long|number|string} other
* @return {!Long}
*/
Long.prototype.power = function(other) {};

/**
* @param {!Long|number|string} other
* @return {!Long}
*/
Long.prototype.pow = function(other) {};

/**
* @param {!Long|number|string} other
* @return {!Long}
Expand Down
15 changes: 15 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ declare class Long {
*/
div(divisor: Long | number | string): Long;

/**
* Returns a deep copy of this Long.
*/
clone(): Long;

/**
* Returns this Long to given integer power.
*/
power(exp: Long | number | string): Long;

/**
* Returns this Long to given integer power.
*/
pow(exp: Long | number | string): Long;

/**
* Tests if this Long's value equals the specified's.
*/
Expand Down
36 changes: 36 additions & 0 deletions src/long.js
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,42 @@ LongPrototype.modulo = function modulo(divisor) {
return this.sub(this.div(divisor).mul(divisor));
};

/**
* Returns a deep copy of this Long.
* @this {!Long}
* @returns {!Long} Deep copy of this Long
*/
LongPrototype.clone = function clone() {
return new Long(this.low, this.high, this.unsigned);
};

/**
* Returns this Long to given integer power.
* @this {!Long}
* @param {!Long|number|string} exp Integer power
* @returns {!Long} This Long to given integer power
*/
LongPrototype.power = function power(exp) {
var a = this;
var b = Long.fromValue(exp);
if (b.isZero()) return Long.ONE.clone();
if (a.eq(Long.ONE) || b.eq(Long.ONE)) return a.clone();
if (b.isNegative()) return Long.ONE.div(a.pow(b.neg())); // being only integers, this will probably always be zero?
while (b.isEven()) {
b = b.shru(1);
a = a.mul(a);
}
return a.pow(b.sub(1)).mul(a);
};

/**
* Returns this Long to given integer power. This is an alias of {@link Long#power}.
* @function
* @param {!Long|number|string} exp Power
* @returns {!Long} This Long to given integer power
*/
LongPrototype.pow = LongPrototype.power;

/**
* Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
* @function
Expand Down
13 changes: 13 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ function testUnsignedMsbUnsigned() {
assert.strictEqual(Long.fromString("9223372036854775808", true).toString(), "9223372036854775808");
},

function testPower() {
var a = Long.fromNumber(11);
var b = a.power(12);
var res = Long.fromNumber(3138428376721);
assert.deepEqual(b, res);
// check negative, which will probably always be zero since we can't store fractional result?
b = a.power(-3);
assert.deepEqual(b, Long.ZERO);
// except perhaps one which has a short circuit condition anyway?
b = Long.ONE.power(-1);
assert.deepEqual(b, Long.ONE);
},

function testIssue31() {
var a = new Long(0, 8, true);
var b = Long.fromNumber(2656901066, true);
Expand Down

0 comments on commit 067aa40

Please sign in to comment.