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