exponentiate.js 853 B

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