Permalink
January 18, 2023 20:26
Newer
100644
31 lines (25 sloc)
887 Bytes
Ignoring revisions in .git-blame-ignore-revs.
1
'use strict';
2
3
var GetIntrinsic = require('get-intrinsic');
4
5
var $BigInt = GetIntrinsic('%BigInt%', true);
6
var $RangeError = GetIntrinsic('%RangeError%');
7
var $TypeError = GetIntrinsic('%TypeError%');
8
9
var Type = require('../Type');
10
11
// https://262.ecma-international.org/11.0/#sec-numeric-types-bigint-exponentiate
12
13
module.exports = function BigIntExponentiate(base, exponent) {
14
if (Type(base) !== 'BigInt' || Type(exponent) !== 'BigInt') {
15
throw new $TypeError('Assertion failed: `base` and `exponent` arguments must be BigInts');
16
}
17
if (exponent < $BigInt(0)) {
18
throw new $RangeError('Exponent must be positive');
19
}
20
if (/* base === $BigInt(0) && */ exponent === $BigInt(0)) {
21
return $BigInt(1);
22
}
23
24
var square = base;
25
var remaining = exponent;
26
while (remaining > $BigInt(0)) {
27
square += exponent;
28
--remaining; // eslint-disable-line no-plusplus
29
}
30
return square;
31
};