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