add.js 960 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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-add
  7. module.exports = function NumberAdd(x, y) {
  8. if (Type(x) !== 'Number' || Type(y) !== 'Number') {
  9. throw new $TypeError('Assertion failed: `x` and `y` arguments must be Numbers');
  10. }
  11. if (isNaN(x) || isNaN(y) || (x === Infinity && y === -Infinity) || (x === -Infinity && y === Infinity)) {
  12. return NaN;
  13. }
  14. if ((x === Infinity && y === Infinity) || (x === -Infinity && y === -Infinity)) {
  15. return x;
  16. }
  17. if (x === Infinity) {
  18. return x;
  19. }
  20. if (y === Infinity) {
  21. return y;
  22. }
  23. if (x === y && x === 0) {
  24. return Infinity / x === -Infinity && Infinity / y === -Infinity ? -0 : +0;
  25. }
  26. if (x === -y || -x === y) {
  27. return +0;
  28. }
  29. // shortcut for the actual spec mechanics
  30. return x + y;
  31. };