remainder.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var isNaN = require('../../helpers/isNaN');
  5. var Type = require('../Type');
  6. // https://262.ecma-international.org/11.0/#sec-numeric-types-number-remainder
  7. module.exports = function NumberRemainder(n, d) {
  8. if (Type(n) !== 'Number' || Type(d) !== 'Number') {
  9. throw new $TypeError('Assertion failed: `n` and `d` arguments must be Numbers');
  10. }
  11. // If either operand is NaN, the result is NaN.
  12. // If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
  13. if (isNaN(n) || isNaN(d) || !isFinite(n) || d === 0) {
  14. return NaN;
  15. }
  16. // If the dividend is finite and the divisor is an infinity, the result equals the dividend.
  17. // If the dividend is a zero and the divisor is nonzero and finite, the result is the same as the dividend.
  18. if (!isFinite(d) || (n === 0 && d !== 0)) {
  19. return n;
  20. }
  21. // In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved…
  22. return n % d;
  23. };