remainder.js 962 B

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