number.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.cbrt = exports.atanh = exports.asinh = exports.acosh = exports.DBL_EPSILON = void 0;
  6. exports.copysign = copysign;
  7. exports.cosh = void 0;
  8. exports.digits = digits;
  9. exports.expm1 = void 0;
  10. exports.format = format;
  11. exports.isInteger = isInteger;
  12. exports.log2 = exports.log1p = exports.log10 = void 0;
  13. exports.nearlyEqual = nearlyEqual;
  14. exports.roundDigits = roundDigits;
  15. exports.sinh = exports.sign = void 0;
  16. exports.splitNumber = splitNumber;
  17. exports.tanh = void 0;
  18. exports.toEngineering = toEngineering;
  19. exports.toExponential = toExponential;
  20. exports.toFixed = toFixed;
  21. exports.toPrecision = toPrecision;
  22. var _is = require("./is.js");
  23. /**
  24. * @typedef {{sign: '+' | '-' | '', coefficients: number[], exponent: number}} SplitValue
  25. */
  26. /**
  27. * Check if a number is integer
  28. * @param {number | boolean} value
  29. * @return {boolean} isInteger
  30. */
  31. function isInteger(value) {
  32. if (typeof value === 'boolean') {
  33. return true;
  34. }
  35. return isFinite(value) ? value === Math.round(value) : false;
  36. }
  37. /**
  38. * Calculate the sign of a number
  39. * @param {number} x
  40. * @returns {number}
  41. */
  42. var sign = /* #__PURE__ */Math.sign || function (x) {
  43. if (x > 0) {
  44. return 1;
  45. } else if (x < 0) {
  46. return -1;
  47. } else {
  48. return 0;
  49. }
  50. };
  51. /**
  52. * Calculate the base-2 logarithm of a number
  53. * @param {number} x
  54. * @returns {number}
  55. */
  56. exports.sign = sign;
  57. var log2 = /* #__PURE__ */Math.log2 || function log2(x) {
  58. return Math.log(x) / Math.LN2;
  59. };
  60. /**
  61. * Calculate the base-10 logarithm of a number
  62. * @param {number} x
  63. * @returns {number}
  64. */
  65. exports.log2 = log2;
  66. var log10 = /* #__PURE__ */Math.log10 || function log10(x) {
  67. return Math.log(x) / Math.LN10;
  68. };
  69. /**
  70. * Calculate the natural logarithm of a number + 1
  71. * @param {number} x
  72. * @returns {number}
  73. */
  74. exports.log10 = log10;
  75. var log1p = /* #__PURE__ */Math.log1p || function (x) {
  76. return Math.log(x + 1);
  77. };
  78. /**
  79. * Calculate cubic root for a number
  80. *
  81. * Code from es6-shim.js:
  82. * https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1564-L1577
  83. *
  84. * @param {number} x
  85. * @returns {number} Returns the cubic root of x
  86. */
  87. exports.log1p = log1p;
  88. var cbrt = /* #__PURE__ */Math.cbrt || function cbrt(x) {
  89. if (x === 0) {
  90. return x;
  91. }
  92. var negate = x < 0;
  93. var result;
  94. if (negate) {
  95. x = -x;
  96. }
  97. if (isFinite(x)) {
  98. result = Math.exp(Math.log(x) / 3);
  99. // from https://en.wikipedia.org/wiki/Cube_root#Numerical_methods
  100. result = (x / (result * result) + 2 * result) / 3;
  101. } else {
  102. result = x;
  103. }
  104. return negate ? -result : result;
  105. };
  106. /**
  107. * Calculates exponentiation minus 1
  108. * @param {number} x
  109. * @return {number} res
  110. */
  111. exports.cbrt = cbrt;
  112. var expm1 = /* #__PURE__ */Math.expm1 || function expm1(x) {
  113. return x >= 2e-4 || x <= -2e-4 ? Math.exp(x) - 1 : x + x * x / 2 + x * x * x / 6;
  114. };
  115. /**
  116. * Formats a number in a given base
  117. * @param {number} n
  118. * @param {number} base
  119. * @param {number} size
  120. * @returns {string}
  121. */
  122. exports.expm1 = expm1;
  123. function formatNumberToBase(n, base, size) {
  124. var prefixes = {
  125. 2: '0b',
  126. 8: '0o',
  127. 16: '0x'
  128. };
  129. var prefix = prefixes[base];
  130. var suffix = '';
  131. if (size) {
  132. if (size < 1) {
  133. throw new Error('size must be in greater than 0');
  134. }
  135. if (!isInteger(size)) {
  136. throw new Error('size must be an integer');
  137. }
  138. if (n > Math.pow(2, size - 1) - 1 || n < -Math.pow(2, size - 1)) {
  139. throw new Error("Value must be in range [-2^".concat(size - 1, ", 2^").concat(size - 1, "-1]"));
  140. }
  141. if (!isInteger(n)) {
  142. throw new Error('Value must be an integer');
  143. }
  144. if (n < 0) {
  145. n = n + Math.pow(2, size);
  146. }
  147. suffix = "i".concat(size);
  148. }
  149. var sign = '';
  150. if (n < 0) {
  151. n = -n;
  152. sign = '-';
  153. }
  154. return "".concat(sign).concat(prefix).concat(n.toString(base)).concat(suffix);
  155. }
  156. /**
  157. * Convert a number to a formatted string representation.
  158. *
  159. * Syntax:
  160. *
  161. * format(value)
  162. * format(value, options)
  163. * format(value, precision)
  164. * format(value, fn)
  165. *
  166. * Where:
  167. *
  168. * {number} value The value to be formatted
  169. * {Object} options An object with formatting options. Available options:
  170. * {string} notation
  171. * Number notation. Choose from:
  172. * 'fixed' Always use regular number notation.
  173. * For example '123.40' and '14000000'
  174. * 'exponential' Always use exponential notation.
  175. * For example '1.234e+2' and '1.4e+7'
  176. * 'engineering' Always use engineering notation.
  177. * For example '123.4e+0' and '14.0e+6'
  178. * 'auto' (default) Regular number notation for numbers
  179. * having an absolute value between
  180. * `lowerExp` and `upperExp` bounds, and
  181. * uses exponential notation elsewhere.
  182. * Lower bound is included, upper bound
  183. * is excluded.
  184. * For example '123.4' and '1.4e7'.
  185. * 'bin', 'oct, or
  186. * 'hex' Format the number using binary, octal,
  187. * or hexadecimal notation.
  188. * For example '0b1101' and '0x10fe'.
  189. * {number} wordSize The word size in bits to use for formatting
  190. * in binary, octal, or hexadecimal notation.
  191. * To be used only with 'bin', 'oct', or 'hex'
  192. * values for 'notation' option. When this option
  193. * is defined the value is formatted as a signed
  194. * twos complement integer of the given word size
  195. * and the size suffix is appended to the output.
  196. * For example
  197. * format(-1, {notation: 'hex', wordSize: 8}) === '0xffi8'.
  198. * Default value is undefined.
  199. * {number} precision A number between 0 and 16 to round
  200. * the digits of the number.
  201. * In case of notations 'exponential',
  202. * 'engineering', and 'auto',
  203. * `precision` defines the total
  204. * number of significant digits returned.
  205. * In case of notation 'fixed',
  206. * `precision` defines the number of
  207. * significant digits after the decimal
  208. * point.
  209. * `precision` is undefined by default,
  210. * not rounding any digits.
  211. * {number} lowerExp Exponent determining the lower boundary
  212. * for formatting a value with an exponent
  213. * when `notation='auto`.
  214. * Default value is `-3`.
  215. * {number} upperExp Exponent determining the upper boundary
  216. * for formatting a value with an exponent
  217. * when `notation='auto`.
  218. * Default value is `5`.
  219. * {Function} fn A custom formatting function. Can be used to override the
  220. * built-in notations. Function `fn` is called with `value` as
  221. * parameter and must return a string. Is useful for example to
  222. * format all values inside a matrix in a particular way.
  223. *
  224. * Examples:
  225. *
  226. * format(6.4) // '6.4'
  227. * format(1240000) // '1.24e6'
  228. * format(1/3) // '0.3333333333333333'
  229. * format(1/3, 3) // '0.333'
  230. * format(21385, 2) // '21000'
  231. * format(12.071, {notation: 'fixed'}) // '12'
  232. * format(2.3, {notation: 'fixed', precision: 2}) // '2.30'
  233. * format(52.8, {notation: 'exponential'}) // '5.28e+1'
  234. * format(12345678, {notation: 'engineering'}) // '12.345678e+6'
  235. *
  236. * @param {number} value
  237. * @param {Object | Function | number} [options]
  238. * @return {string} str The formatted value
  239. */
  240. function format(value, options) {
  241. if (typeof options === 'function') {
  242. // handle format(value, fn)
  243. return options(value);
  244. }
  245. // handle special cases
  246. if (value === Infinity) {
  247. return 'Infinity';
  248. } else if (value === -Infinity) {
  249. return '-Infinity';
  250. } else if (isNaN(value)) {
  251. return 'NaN';
  252. }
  253. // default values for options
  254. var notation = 'auto';
  255. var precision;
  256. var wordSize;
  257. if (options) {
  258. // determine notation from options
  259. if (options.notation) {
  260. notation = options.notation;
  261. }
  262. // determine precision from options
  263. if ((0, _is.isNumber)(options)) {
  264. precision = options;
  265. } else if ((0, _is.isNumber)(options.precision)) {
  266. precision = options.precision;
  267. }
  268. if (options.wordSize) {
  269. wordSize = options.wordSize;
  270. if (typeof wordSize !== 'number') {
  271. throw new Error('Option "wordSize" must be a number');
  272. }
  273. }
  274. }
  275. // handle the various notations
  276. switch (notation) {
  277. case 'fixed':
  278. return toFixed(value, precision);
  279. case 'exponential':
  280. return toExponential(value, precision);
  281. case 'engineering':
  282. return toEngineering(value, precision);
  283. case 'bin':
  284. return formatNumberToBase(value, 2, wordSize);
  285. case 'oct':
  286. return formatNumberToBase(value, 8, wordSize);
  287. case 'hex':
  288. return formatNumberToBase(value, 16, wordSize);
  289. case 'auto':
  290. // remove trailing zeros after the decimal point
  291. return toPrecision(value, precision, options && options).replace(/((\.\d*?)(0+))($|e)/, function () {
  292. var digits = arguments[2];
  293. var e = arguments[4];
  294. return digits !== '.' ? digits + e : e;
  295. });
  296. default:
  297. throw new Error('Unknown notation "' + notation + '". ' + 'Choose "auto", "exponential", "fixed", "bin", "oct", or "hex.');
  298. }
  299. }
  300. /**
  301. * Split a number into sign, coefficients, and exponent
  302. * @param {number | string} value
  303. * @return {SplitValue}
  304. * Returns an object containing sign, coefficients, and exponent
  305. */
  306. function splitNumber(value) {
  307. // parse the input value
  308. var match = String(value).toLowerCase().match(/^(-?)(\d+\.?\d*)(e([+-]?\d+))?$/);
  309. if (!match) {
  310. throw new SyntaxError('Invalid number ' + value);
  311. }
  312. var sign = match[1];
  313. var digits = match[2];
  314. var exponent = parseFloat(match[4] || '0');
  315. var dot = digits.indexOf('.');
  316. exponent += dot !== -1 ? dot - 1 : digits.length - 1;
  317. var coefficients = digits.replace('.', '') // remove the dot (must be removed before removing leading zeros)
  318. .replace(/^0*/, function (zeros) {
  319. // remove leading zeros, add their count to the exponent
  320. exponent -= zeros.length;
  321. return '';
  322. }).replace(/0*$/, '') // remove trailing zeros
  323. .split('').map(function (d) {
  324. return parseInt(d);
  325. });
  326. if (coefficients.length === 0) {
  327. coefficients.push(0);
  328. exponent++;
  329. }
  330. return {
  331. sign: sign,
  332. coefficients: coefficients,
  333. exponent: exponent
  334. };
  335. }
  336. /**
  337. * Format a number in engineering notation. Like '1.23e+6', '2.3e+0', '3.500e-3'
  338. * @param {number | string} value
  339. * @param {number} [precision] Optional number of significant figures to return.
  340. */
  341. function toEngineering(value, precision) {
  342. if (isNaN(value) || !isFinite(value)) {
  343. return String(value);
  344. }
  345. var split = splitNumber(value);
  346. var rounded = roundDigits(split, precision);
  347. var e = rounded.exponent;
  348. var c = rounded.coefficients;
  349. // find nearest lower multiple of 3 for exponent
  350. var newExp = e % 3 === 0 ? e : e < 0 ? e - 3 - e % 3 : e - e % 3;
  351. if ((0, _is.isNumber)(precision)) {
  352. // add zeroes to give correct sig figs
  353. while (precision > c.length || e - newExp + 1 > c.length) {
  354. c.push(0);
  355. }
  356. } else {
  357. // concatenate coefficients with necessary zeros
  358. // add zeros if necessary (for example: 1e+8 -> 100e+6)
  359. var missingZeros = Math.abs(e - newExp) - (c.length - 1);
  360. for (var i = 0; i < missingZeros; i++) {
  361. c.push(0);
  362. }
  363. }
  364. // find difference in exponents
  365. var expDiff = Math.abs(e - newExp);
  366. var decimalIdx = 1;
  367. // push decimal index over by expDiff times
  368. while (expDiff > 0) {
  369. decimalIdx++;
  370. expDiff--;
  371. }
  372. // if all coefficient values are zero after the decimal point and precision is unset, don't add a decimal value.
  373. // otherwise concat with the rest of the coefficients
  374. var decimals = c.slice(decimalIdx).join('');
  375. var decimalVal = (0, _is.isNumber)(precision) && decimals.length || decimals.match(/[1-9]/) ? '.' + decimals : '';
  376. var str = c.slice(0, decimalIdx).join('') + decimalVal + 'e' + (e >= 0 ? '+' : '') + newExp.toString();
  377. return rounded.sign + str;
  378. }
  379. /**
  380. * Format a number with fixed notation.
  381. * @param {number | string} value
  382. * @param {number} [precision=undefined] Optional number of decimals after the
  383. * decimal point. null by default.
  384. */
  385. function toFixed(value, precision) {
  386. if (isNaN(value) || !isFinite(value)) {
  387. return String(value);
  388. }
  389. var splitValue = splitNumber(value);
  390. var rounded = typeof precision === 'number' ? roundDigits(splitValue, splitValue.exponent + 1 + precision) : splitValue;
  391. var c = rounded.coefficients;
  392. var p = rounded.exponent + 1; // exponent may have changed
  393. // append zeros if needed
  394. var pp = p + (precision || 0);
  395. if (c.length < pp) {
  396. c = c.concat(zeros(pp - c.length));
  397. }
  398. // prepend zeros if needed
  399. if (p < 0) {
  400. c = zeros(-p + 1).concat(c);
  401. p = 1;
  402. }
  403. // insert a dot if needed
  404. if (p < c.length) {
  405. c.splice(p, 0, p === 0 ? '0.' : '.');
  406. }
  407. return rounded.sign + c.join('');
  408. }
  409. /**
  410. * Format a number in exponential notation. Like '1.23e+5', '2.3e+0', '3.500e-3'
  411. * @param {number | string} value
  412. * @param {number} [precision] Number of digits in formatted output.
  413. * If not provided, the maximum available digits
  414. * is used.
  415. */
  416. function toExponential(value, precision) {
  417. if (isNaN(value) || !isFinite(value)) {
  418. return String(value);
  419. }
  420. // round if needed, else create a clone
  421. var split = splitNumber(value);
  422. var rounded = precision ? roundDigits(split, precision) : split;
  423. var c = rounded.coefficients;
  424. var e = rounded.exponent;
  425. // append zeros if needed
  426. if (c.length < precision) {
  427. c = c.concat(zeros(precision - c.length));
  428. }
  429. // format as `C.CCCe+EEE` or `C.CCCe-EEE`
  430. var first = c.shift();
  431. return rounded.sign + first + (c.length > 0 ? '.' + c.join('') : '') + 'e' + (e >= 0 ? '+' : '') + e;
  432. }
  433. /**
  434. * Format a number with a certain precision
  435. * @param {number | string} value
  436. * @param {number} [precision=undefined] Optional number of digits.
  437. * @param {{lowerExp: number | undefined, upperExp: number | undefined}} [options]
  438. * By default:
  439. * lowerExp = -3 (incl)
  440. * upper = +5 (excl)
  441. * @return {string}
  442. */
  443. function toPrecision(value, precision, options) {
  444. if (isNaN(value) || !isFinite(value)) {
  445. return String(value);
  446. }
  447. // determine lower and upper bound for exponential notation.
  448. var lowerExp = options && options.lowerExp !== undefined ? options.lowerExp : -3;
  449. var upperExp = options && options.upperExp !== undefined ? options.upperExp : 5;
  450. var split = splitNumber(value);
  451. var rounded = precision ? roundDigits(split, precision) : split;
  452. if (rounded.exponent < lowerExp || rounded.exponent >= upperExp) {
  453. // exponential notation
  454. return toExponential(value, precision);
  455. } else {
  456. var c = rounded.coefficients;
  457. var e = rounded.exponent;
  458. // append trailing zeros
  459. if (c.length < precision) {
  460. c = c.concat(zeros(precision - c.length));
  461. }
  462. // append trailing zeros
  463. // TODO: simplify the next statement
  464. c = c.concat(zeros(e - c.length + 1 + (c.length < precision ? precision - c.length : 0)));
  465. // prepend zeros
  466. c = zeros(-e).concat(c);
  467. var dot = e > 0 ? e : 0;
  468. if (dot < c.length - 1) {
  469. c.splice(dot + 1, 0, '.');
  470. }
  471. return rounded.sign + c.join('');
  472. }
  473. }
  474. /**
  475. * Round the number of digits of a number *
  476. * @param {SplitValue} split A value split with .splitNumber(value)
  477. * @param {number} precision A positive integer
  478. * @return {SplitValue}
  479. * Returns an object containing sign, coefficients, and exponent
  480. * with rounded digits
  481. */
  482. function roundDigits(split, precision) {
  483. // create a clone
  484. var rounded = {
  485. sign: split.sign,
  486. coefficients: split.coefficients,
  487. exponent: split.exponent
  488. };
  489. var c = rounded.coefficients;
  490. // prepend zeros if needed
  491. while (precision <= 0) {
  492. c.unshift(0);
  493. rounded.exponent++;
  494. precision++;
  495. }
  496. if (c.length > precision) {
  497. var removed = c.splice(precision, c.length - precision);
  498. if (removed[0] >= 5) {
  499. var i = precision - 1;
  500. c[i]++;
  501. while (c[i] === 10) {
  502. c.pop();
  503. if (i === 0) {
  504. c.unshift(0);
  505. rounded.exponent++;
  506. i++;
  507. }
  508. i--;
  509. c[i]++;
  510. }
  511. }
  512. }
  513. return rounded;
  514. }
  515. /**
  516. * Create an array filled with zeros.
  517. * @param {number} length
  518. * @return {Array}
  519. */
  520. function zeros(length) {
  521. var arr = [];
  522. for (var i = 0; i < length; i++) {
  523. arr.push(0);
  524. }
  525. return arr;
  526. }
  527. /**
  528. * Count the number of significant digits of a number.
  529. *
  530. * For example:
  531. * 2.34 returns 3
  532. * 0.0034 returns 2
  533. * 120.5e+30 returns 4
  534. *
  535. * @param {number} value
  536. * @return {number} digits Number of significant digits
  537. */
  538. function digits(value) {
  539. return value.toExponential().replace(/e.*$/, '') // remove exponential notation
  540. .replace(/^0\.?0*|\./, '') // remove decimal point and leading zeros
  541. .length;
  542. }
  543. /**
  544. * Minimum number added to one that makes the result different than one
  545. */
  546. var DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16;
  547. /**
  548. * Compares two floating point numbers.
  549. * @param {number} x First value to compare
  550. * @param {number} y Second value to compare
  551. * @param {number} [epsilon] The maximum relative difference between x and y
  552. * If epsilon is undefined or null, the function will
  553. * test whether x and y are exactly equal.
  554. * @return {boolean} whether the two numbers are nearly equal
  555. */
  556. exports.DBL_EPSILON = DBL_EPSILON;
  557. function nearlyEqual(x, y, epsilon) {
  558. // if epsilon is null or undefined, test whether x and y are exactly equal
  559. if (epsilon === null || epsilon === undefined) {
  560. return x === y;
  561. }
  562. if (x === y) {
  563. return true;
  564. }
  565. // NaN
  566. if (isNaN(x) || isNaN(y)) {
  567. return false;
  568. }
  569. // at this point x and y should be finite
  570. if (isFinite(x) && isFinite(y)) {
  571. // check numbers are very close, needed when comparing numbers near zero
  572. var diff = Math.abs(x - y);
  573. if (diff < DBL_EPSILON) {
  574. return true;
  575. } else {
  576. // use relative error
  577. return diff <= Math.max(Math.abs(x), Math.abs(y)) * epsilon;
  578. }
  579. }
  580. // Infinite and Number or negative Infinite and positive Infinite cases
  581. return false;
  582. }
  583. /**
  584. * Calculate the hyperbolic arccos of a number
  585. * @param {number} x
  586. * @return {number}
  587. */
  588. var acosh = Math.acosh || function (x) {
  589. return Math.log(Math.sqrt(x * x - 1) + x);
  590. };
  591. exports.acosh = acosh;
  592. var asinh = Math.asinh || function (x) {
  593. return Math.log(Math.sqrt(x * x + 1) + x);
  594. };
  595. /**
  596. * Calculate the hyperbolic arctangent of a number
  597. * @param {number} x
  598. * @return {number}
  599. */
  600. exports.asinh = asinh;
  601. var atanh = Math.atanh || function (x) {
  602. return Math.log((1 + x) / (1 - x)) / 2;
  603. };
  604. /**
  605. * Calculate the hyperbolic cosine of a number
  606. * @param {number} x
  607. * @returns {number}
  608. */
  609. exports.atanh = atanh;
  610. var cosh = Math.cosh || function (x) {
  611. return (Math.exp(x) + Math.exp(-x)) / 2;
  612. };
  613. /**
  614. * Calculate the hyperbolic sine of a number
  615. * @param {number} x
  616. * @returns {number}
  617. */
  618. exports.cosh = cosh;
  619. var sinh = Math.sinh || function (x) {
  620. return (Math.exp(x) - Math.exp(-x)) / 2;
  621. };
  622. /**
  623. * Calculate the hyperbolic tangent of a number
  624. * @param {number} x
  625. * @returns {number}
  626. */
  627. exports.sinh = sinh;
  628. var tanh = Math.tanh || function (x) {
  629. var e = Math.exp(2 * x);
  630. return (e - 1) / (e + 1);
  631. };
  632. /**
  633. * Returns a value with the magnitude of x and the sign of y.
  634. * @param {number} x
  635. * @param {number} y
  636. * @returns {number}
  637. */
  638. exports.tanh = tanh;
  639. function copysign(x, y) {
  640. var signx = x > 0 ? true : x < 0 ? false : 1 / x === Infinity;
  641. var signy = y > 0 ? true : y < 0 ? false : 1 / y === Infinity;
  642. return signx ^ signy ? -x : x;
  643. }