remainder.js 672 B

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