remainder.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var isNaN = require('../../helpers/isNaN');
  5. var isFinite = require('../../helpers/isFinite');
  6. var truncate = require('../truncate');
  7. var Type = require('../Type');
  8. // https://262.ecma-international.org/14.0/#sec-numeric-types-number-remainder
  9. module.exports = function NumberRemainder(n, d) {
  10. if (Type(n) !== 'Number' || Type(d) !== 'Number') {
  11. throw new $TypeError('Assertion failed: `n` and `d` arguments must be Numbers');
  12. }
  13. // If either operand is NaN, the result is NaN.
  14. // If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
  15. if (isNaN(n) || isNaN(d) || !isFinite(n) || d === 0) {
  16. return NaN;
  17. }
  18. // If the dividend is finite and the divisor is an infinity, the result equals the dividend.
  19. // If the dividend is a zero and the divisor is nonzero and finite, the result is the same as the dividend.
  20. if (!isFinite(d) || n === 0) {
  21. return n;
  22. }
  23. if (!isFinite(n) || !isFinite(d) || n === 0 || d === 0) {
  24. throw new $TypeError('Assertion failed: `n` and `d` arguments must be finite and nonzero');
  25. }
  26. var quotient = n / d;
  27. var q = truncate(quotient);
  28. var r = n - (d * q);
  29. if (r === 0 && n < 0) {
  30. return -0;
  31. }
  32. return r;
  33. };