add.js 882 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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-add
  5. module.exports = function NumberAdd(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 === Infinity && y === -Infinity) || (x === -Infinity && y === Infinity)) {
  10. return NaN;
  11. }
  12. if ((x === Infinity && y === Infinity) || (x === -Infinity && y === -Infinity)) {
  13. return x;
  14. }
  15. if (x === Infinity) {
  16. return x;
  17. }
  18. if (y === Infinity) {
  19. return y;
  20. }
  21. if (x === y && x === 0) {
  22. return Infinity / x === -Infinity && Infinity / y === -Infinity ? -0 : +0;
  23. }
  24. if (x === -y || -x === y) {
  25. return +0;
  26. }
  27. // shortcut for the actual spec mechanics
  28. return x + y;
  29. };