multiply.js 770 B

1234567891011121314151617181920212223242526272829
  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-multiply
  5. module.exports = function NumberMultiply(x, y) {
  6. if (typeof x !== 'number' || typeof y !== 'number') {
  7. throw new $TypeError('Assertion failed: `x` and `y` arguments must be Numbers');
  8. }
  9. if (isNaN(x) || isNaN(y) || (x === 0 && !isFinite(y)) || (!isFinite(x) && y === 0)) {
  10. return NaN;
  11. }
  12. if (!isFinite(x) && !isFinite(y)) {
  13. return x === y ? Infinity : -Infinity;
  14. }
  15. if (!isFinite(x) && y !== 0) {
  16. return x > 0 ? Infinity : -Infinity;
  17. }
  18. if (!isFinite(y) && x !== 0) {
  19. return y > 0 ? Infinity : -Infinity;
  20. }
  21. // shortcut for the actual spec mechanics
  22. return x * y;
  23. };