remainder.js 706 B

123456789101112131415161718192021222324252627282930
  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. var zero = $BigInt && $BigInt(0);
  8. // https://262.ecma-international.org/11.0/#sec-numeric-types-bigint-remainder
  9. module.exports = function BigIntRemainder(n, d) {
  10. if (Type(n) !== 'BigInt' || Type(d) !== 'BigInt') {
  11. throw new $TypeError('Assertion failed: `n` and `d` arguments must be BigInts');
  12. }
  13. if (d === zero) {
  14. throw new $RangeError('Division by zero');
  15. }
  16. if (n === zero) {
  17. return zero;
  18. }
  19. // shortcut for the actual spec mechanics
  20. return n % d;
  21. };