Skip to content

Commit

Permalink
Power: Restrict exponent to 32 bit value. Negative exponents truncate…
Browse files Browse the repository at this point in the history
… to zero (unless value was one).
  • Loading branch information
CyDragon80 authored and CyDragon80 committed Jun 19, 2018
1 parent 4f0deea commit 8dfd251
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ API
* 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#**power**/**pow**(exp: `Long | number`): `Long`<br />
Returns this Long to given 32bit 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.

4 changes: 2 additions & 2 deletions externs/long.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,13 @@ Long.prototype.div = function(other) {};
Long.prototype.clone = function() {};

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

/**
* @param {!Long|number|string} other
* @param {!Long|number} other
* @return {!Long}
*/
Long.prototype.pow = function(other) {};
Expand Down
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ declare class Long {
clone(): Long;

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

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

/**
* Tests if this Long's value equals the specified's.
Expand Down
22 changes: 11 additions & 11 deletions src/long.js
Original file line number Diff line number Diff line change
Expand Up @@ -1096,28 +1096,28 @@ LongPrototype.clone = function clone() {
};

/**
* Returns this Long to given integer power.
* Returns this Long to given 32bit integer power.
* @this {!Long}
* @param {!Long|number|string} exp Integer power
* @param {!Long|number} exp Integer power (32bit)
* @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;
if (a.eq(Long.ONE) || b.eq(Long.ONE)) return a;
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);
if (isLong(exp)) exp = exp.toInt();
if (exp===0) return Long.ONE;
if (a.eq(Long.ONE) || exp===1) return a;
if (exp < 0) return Long.ZERO; // Long.ONE.div(a.pow(-exp)); // being only integers, this will probably always be zero?
while ((exp & 1)===0) {
exp >>>= 1;
a = a.mul(a);
}
return a.pow(b.sub(1)).mul(a);
return a.pow(exp-1).mul(a);
};

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

0 comments on commit 8dfd251

Please sign in to comment.