exponentiate.js 887 B

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