1
|
|
|
/*! bignumber.js v4.0.4 https://github.com/MikeMcl/bignumber.js/LICENCE */ |
2
|
|
|
|
3
|
|
|
;(function (globalObj) { |
4
|
|
|
'use strict'; |
5
|
|
|
|
6
|
|
|
/* |
7
|
|
|
bignumber.js v4.0.4 |
8
|
|
|
A JavaScript library for arbitrary-precision arithmetic. |
9
|
|
|
https://github.com/MikeMcl/bignumber.js |
10
|
|
|
Copyright (c) 2017 Michael Mclaughlin <[email protected]> |
11
|
|
|
MIT Expat Licence |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
var BigNumber, |
16
|
|
|
isNumeric = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i, |
17
|
|
|
mathceil = Math.ceil, |
18
|
|
|
mathfloor = Math.floor, |
19
|
|
|
notBool = ' not a boolean or binary digit', |
20
|
|
|
roundingMode = 'rounding mode', |
21
|
|
|
tooManyDigits = 'number type has more than 15 significant digits', |
22
|
|
|
ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_', |
23
|
|
|
BASE = 1e14, |
24
|
|
|
LOG_BASE = 14, |
25
|
|
|
MAX_SAFE_INTEGER = 0x1fffffffffffff, // 2^53 - 1 |
26
|
|
|
// MAX_INT32 = 0x7fffffff, // 2^31 - 1 |
27
|
|
|
POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13], |
28
|
|
|
SQRT_BASE = 1e7, |
29
|
|
|
|
30
|
|
|
/* |
31
|
|
|
* The limit on the value of DECIMAL_PLACES, TO_EXP_NEG, TO_EXP_POS, MIN_EXP, MAX_EXP, and |
32
|
|
|
* the arguments to toExponential, toFixed, toFormat, and toPrecision, beyond which an |
33
|
|
|
* exception is thrown (if ERRORS is true). |
34
|
|
|
*/ |
35
|
|
|
MAX = 1E9; // 0 to MAX_INT32 |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/* |
39
|
|
|
* Create and return a BigNumber constructor. |
40
|
|
|
*/ |
41
|
|
|
function constructorFactory(config) { |
42
|
|
|
var div, parseNumeric, |
43
|
|
|
|
44
|
|
|
// id tracks the caller function, so its name can be included in error messages. |
45
|
|
|
id = 0, |
46
|
|
|
P = BigNumber.prototype, |
47
|
|
|
ONE = new BigNumber(1), |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/********************************* EDITABLE DEFAULTS **********************************/ |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/* |
54
|
|
|
* The default values below must be integers within the inclusive ranges stated. |
55
|
|
|
* The values can also be changed at run-time using BigNumber.config. |
56
|
|
|
*/ |
57
|
|
|
|
58
|
|
|
// The maximum number of decimal places for operations involving division. |
59
|
|
|
DECIMAL_PLACES = 20, // 0 to MAX |
60
|
|
|
|
61
|
|
|
/* |
62
|
|
|
* The rounding mode used when rounding to the above decimal places, and when using |
63
|
|
|
* toExponential, toFixed, toFormat and toPrecision, and round (default value). |
64
|
|
|
* UP 0 Away from zero. |
65
|
|
|
* DOWN 1 Towards zero. |
66
|
|
|
* CEIL 2 Towards +Infinity. |
67
|
|
|
* FLOOR 3 Towards -Infinity. |
68
|
|
|
* HALF_UP 4 Towards nearest neighbour. If equidistant, up. |
69
|
|
|
* HALF_DOWN 5 Towards nearest neighbour. If equidistant, down. |
70
|
|
|
* HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour. |
71
|
|
|
* HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity. |
72
|
|
|
* HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity. |
73
|
|
|
*/ |
74
|
|
|
ROUNDING_MODE = 4, // 0 to 8 |
75
|
|
|
|
76
|
|
|
// EXPONENTIAL_AT : [TO_EXP_NEG , TO_EXP_POS] |
77
|
|
|
|
78
|
|
|
// The exponent value at and beneath which toString returns exponential notation. |
79
|
|
|
// Number type: -7 |
80
|
|
|
TO_EXP_NEG = -7, // 0 to -MAX |
81
|
|
|
|
82
|
|
|
// The exponent value at and above which toString returns exponential notation. |
83
|
|
|
// Number type: 21 |
84
|
|
|
TO_EXP_POS = 21, // 0 to MAX |
85
|
|
|
|
86
|
|
|
// RANGE : [MIN_EXP, MAX_EXP] |
87
|
|
|
|
88
|
|
|
// The minimum exponent value, beneath which underflow to zero occurs. |
89
|
|
|
// Number type: -324 (5e-324) |
90
|
|
|
MIN_EXP = -1e7, // -1 to -MAX |
91
|
|
|
|
92
|
|
|
// The maximum exponent value, above which overflow to Infinity occurs. |
93
|
|
|
// Number type: 308 (1.7976931348623157e+308) |
94
|
|
|
// For MAX_EXP > 1e7, e.g. new BigNumber('1e100000000').plus(1) may be slow. |
95
|
|
|
MAX_EXP = 1e7, // 1 to MAX |
96
|
|
|
|
97
|
|
|
// Whether BigNumber Errors are ever thrown. |
98
|
|
|
ERRORS = true, // true or false |
99
|
|
|
|
100
|
|
|
// Change to intValidatorNoErrors if ERRORS is false. |
101
|
|
|
isValidInt = intValidatorWithErrors, // intValidatorWithErrors/intValidatorNoErrors |
102
|
|
|
|
103
|
|
|
// Whether to use cryptographically-secure random number generation, if available. |
104
|
|
|
CRYPTO = false, // true or false |
105
|
|
|
|
106
|
|
|
/* |
107
|
|
|
* The modulo mode used when calculating the modulus: a mod n. |
108
|
|
|
* The quotient (q = a / n) is calculated according to the corresponding rounding mode. |
109
|
|
|
* The remainder (r) is calculated as: r = a - n * q. |
110
|
|
|
* |
111
|
|
|
* UP 0 The remainder is positive if the dividend is negative, else is negative. |
112
|
|
|
* DOWN 1 The remainder has the same sign as the dividend. |
113
|
|
|
* This modulo mode is commonly known as 'truncated division' and is |
114
|
|
|
* equivalent to (a % n) in JavaScript. |
115
|
|
|
* FLOOR 3 The remainder has the same sign as the divisor (Python %). |
116
|
|
|
* HALF_EVEN 6 This modulo mode implements the IEEE 754 remainder function. |
117
|
|
|
* EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). |
118
|
|
|
* The remainder is always positive. |
119
|
|
|
* |
120
|
|
|
* The truncated division, floored division, Euclidian division and IEEE 754 remainder |
121
|
|
|
* modes are commonly used for the modulus operation. |
122
|
|
|
* Although the other rounding modes can also be used, they may not give useful results. |
123
|
|
|
*/ |
124
|
|
|
MODULO_MODE = 1, // 0 to 9 |
125
|
|
|
|
126
|
|
|
// The maximum number of significant digits of the result of the toPower operation. |
127
|
|
|
// If POW_PRECISION is 0, there will be unlimited significant digits. |
128
|
|
|
POW_PRECISION = 0, // 0 to MAX |
129
|
|
|
|
130
|
|
|
// The format specification used by the BigNumber.prototype.toFormat method. |
131
|
|
|
FORMAT = { |
132
|
|
|
decimalSeparator: '.', |
133
|
|
|
groupSeparator: ',', |
134
|
|
|
groupSize: 3, |
135
|
|
|
secondaryGroupSize: 0, |
136
|
|
|
fractionGroupSeparator: '\xA0', // non-breaking space |
137
|
|
|
fractionGroupSize: 0 |
138
|
|
|
}; |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/******************************************************************************************/ |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
// CONSTRUCTOR |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/* |
148
|
|
|
* The BigNumber constructor and exported function. |
149
|
|
|
* Create and return a new instance of a BigNumber object. |
150
|
|
|
* |
151
|
|
|
* n {number|string|BigNumber} A numeric value. |
152
|
|
|
* [b] {number} The base of n. Integer, 2 to 64 inclusive. |
153
|
|
|
*/ |
154
|
|
|
function BigNumber( n, b ) { |
155
|
|
|
var c, e, i, num, len, str, |
156
|
|
|
x = this; |
157
|
|
|
|
158
|
|
|
// Enable constructor usage without new. |
159
|
|
|
if ( !( x instanceof BigNumber ) ) { |
160
|
|
|
|
161
|
|
|
// 'BigNumber() constructor call without new: {n}' |
162
|
|
|
if (ERRORS) raise( 26, 'constructor call without new', n ); |
163
|
|
|
return new BigNumber( n, b ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// 'new BigNumber() base not an integer: {b}' |
167
|
|
|
// 'new BigNumber() base out of range: {b}' |
168
|
|
|
if ( b == null || !isValidInt( b, 2, 64, id, 'base' ) ) { |
169
|
|
|
|
170
|
|
|
// Duplicate. |
171
|
|
|
if ( n instanceof BigNumber ) { |
172
|
|
|
x.s = n.s; |
173
|
|
|
x.e = n.e; |
174
|
|
|
x.c = ( n = n.c ) ? n.slice() : n; |
175
|
|
|
id = 0; |
176
|
|
|
return; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if ( ( num = typeof n == 'number' ) && n * 0 == 0 ) { |
180
|
|
|
x.s = 1 / n < 0 ? ( n = -n, -1 ) : 1; |
181
|
|
|
|
182
|
|
|
// Fast path for integers. |
183
|
|
|
if ( n === ~~n ) { |
184
|
|
|
for ( e = 0, i = n; i >= 10; i /= 10, e++ ); |
|
|
|
|
185
|
|
|
x.e = e; |
186
|
|
|
x.c = [n]; |
187
|
|
|
id = 0; |
188
|
|
|
return; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
str = n + ''; |
192
|
|
|
} else { |
193
|
|
|
if ( !isNumeric.test( str = n + '' ) ) return parseNumeric( x, str, num ); |
194
|
|
|
x.s = str.charCodeAt(0) === 45 ? ( str = str.slice(1), -1 ) : 1; |
195
|
|
|
} |
196
|
|
|
} else { |
197
|
|
|
b = b | 0; |
198
|
|
|
str = n + ''; |
199
|
|
|
|
200
|
|
|
// Ensure return value is rounded to DECIMAL_PLACES as with other bases. |
201
|
|
|
// Allow exponential notation to be used with base 10 argument. |
202
|
|
|
if ( b == 10 ) { |
203
|
|
|
x = new BigNumber( n instanceof BigNumber ? n : str ); |
204
|
|
|
return round( x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// Avoid potential interpretation of Infinity and NaN as base 44+ values. |
208
|
|
|
// Any number in exponential form will fail due to the [Ee][+-]. |
209
|
|
|
if ( ( num = typeof n == 'number' ) && n * 0 != 0 || |
210
|
|
|
!( new RegExp( '^-?' + ( c = '[' + ALPHABET.slice( 0, b ) + ']+' ) + |
211
|
|
|
'(?:\\.' + c + ')?$',b < 37 ? 'i' : '' ) ).test(str) ) { |
212
|
|
|
return parseNumeric( x, str, num, b ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if (num) { |
216
|
|
|
x.s = 1 / n < 0 ? ( str = str.slice(1), -1 ) : 1; |
217
|
|
|
|
218
|
|
|
if ( ERRORS && str.replace( /^0\.0*|\./, '' ).length > 15 ) { |
219
|
|
|
|
220
|
|
|
// 'new BigNumber() number type has more than 15 significant digits: {n}' |
221
|
|
|
raise( id, tooManyDigits, n ); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// Prevent later check for length on converted number. |
225
|
|
|
num = false; |
226
|
|
|
} else { |
227
|
|
|
x.s = str.charCodeAt(0) === 45 ? ( str = str.slice(1), -1 ) : 1; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
str = convertBase( str, 10, b, x.s ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// Decimal point? |
234
|
|
|
if ( ( e = str.indexOf('.') ) > -1 ) str = str.replace( '.', '' ); |
235
|
|
|
|
236
|
|
|
// Exponential form? |
237
|
|
|
if ( ( i = str.search( /e/i ) ) > 0 ) { |
238
|
|
|
|
239
|
|
|
// Determine exponent. |
240
|
|
|
if ( e < 0 ) e = i; |
241
|
|
|
e += +str.slice( i + 1 ); |
242
|
|
|
str = str.substring( 0, i ); |
243
|
|
|
} else if ( e < 0 ) { |
244
|
|
|
|
245
|
|
|
// Integer. |
246
|
|
|
e = str.length; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// Determine leading zeros. |
250
|
|
|
for ( i = 0; str.charCodeAt(i) === 48; i++ ); |
|
|
|
|
251
|
|
|
|
252
|
|
|
// Determine trailing zeros. |
253
|
|
|
for ( len = str.length; str.charCodeAt(--len) === 48; ); |
|
|
|
|
254
|
|
|
str = str.slice( i, len + 1 ); |
255
|
|
|
|
256
|
|
|
if (str) { |
257
|
|
|
len = str.length; |
258
|
|
|
|
259
|
|
|
// Disallow numbers with over 15 significant digits if number type. |
260
|
|
|
// 'new BigNumber() number type has more than 15 significant digits: {n}' |
261
|
|
|
if ( num && ERRORS && len > 15 && ( n > MAX_SAFE_INTEGER || n !== mathfloor(n) ) ) { |
262
|
|
|
raise( id, tooManyDigits, x.s * n ); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
e = e - i - 1; |
266
|
|
|
|
267
|
|
|
// Overflow? |
268
|
|
|
if ( e > MAX_EXP ) { |
269
|
|
|
|
270
|
|
|
// Infinity. |
271
|
|
|
x.c = x.e = null; |
272
|
|
|
|
273
|
|
|
// Underflow? |
274
|
|
|
} else if ( e < MIN_EXP ) { |
275
|
|
|
|
276
|
|
|
// Zero. |
277
|
|
|
x.c = [ x.e = 0 ]; |
278
|
|
|
} else { |
279
|
|
|
x.e = e; |
280
|
|
|
x.c = []; |
281
|
|
|
|
282
|
|
|
// Transform base |
283
|
|
|
|
284
|
|
|
// e is the base 10 exponent. |
285
|
|
|
// i is where to slice str to get the first element of the coefficient array. |
286
|
|
|
i = ( e + 1 ) % LOG_BASE; |
287
|
|
|
if ( e < 0 ) i += LOG_BASE; |
288
|
|
|
|
289
|
|
|
if ( i < len ) { |
290
|
|
|
if (i) x.c.push( +str.slice( 0, i ) ); |
291
|
|
|
|
292
|
|
|
for ( len -= LOG_BASE; i < len; ) { |
293
|
|
|
x.c.push( +str.slice( i, i += LOG_BASE ) ); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
str = str.slice(i); |
297
|
|
|
i = LOG_BASE - str.length; |
298
|
|
|
} else { |
299
|
|
|
i -= len; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
for ( ; i--; str += '0' ); |
|
|
|
|
303
|
|
|
x.c.push( +str ); |
304
|
|
|
} |
305
|
|
|
} else { |
306
|
|
|
|
307
|
|
|
// Zero. |
308
|
|
|
x.c = [ x.e = 0 ]; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
id = 0; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
|
315
|
|
|
// CONSTRUCTOR PROPERTIES |
316
|
|
|
|
317
|
|
|
|
318
|
|
|
BigNumber.another = constructorFactory; |
319
|
|
|
|
320
|
|
|
BigNumber.ROUND_UP = 0; |
321
|
|
|
BigNumber.ROUND_DOWN = 1; |
322
|
|
|
BigNumber.ROUND_CEIL = 2; |
323
|
|
|
BigNumber.ROUND_FLOOR = 3; |
324
|
|
|
BigNumber.ROUND_HALF_UP = 4; |
325
|
|
|
BigNumber.ROUND_HALF_DOWN = 5; |
326
|
|
|
BigNumber.ROUND_HALF_EVEN = 6; |
327
|
|
|
BigNumber.ROUND_HALF_CEIL = 7; |
328
|
|
|
BigNumber.ROUND_HALF_FLOOR = 8; |
329
|
|
|
BigNumber.EUCLID = 9; |
330
|
|
|
|
331
|
|
|
|
332
|
|
|
/* |
333
|
|
|
* Configure infrequently-changing library-wide settings. |
334
|
|
|
* |
335
|
|
|
* Accept an object or an argument list, with one or many of the following properties or |
336
|
|
|
* parameters respectively: |
337
|
|
|
* |
338
|
|
|
* DECIMAL_PLACES {number} Integer, 0 to MAX inclusive |
339
|
|
|
* ROUNDING_MODE {number} Integer, 0 to 8 inclusive |
340
|
|
|
* EXPONENTIAL_AT {number|number[]} Integer, -MAX to MAX inclusive or |
341
|
|
|
* [integer -MAX to 0 incl., 0 to MAX incl.] |
342
|
|
|
* RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or |
343
|
|
|
* [integer -MAX to -1 incl., integer 1 to MAX incl.] |
344
|
|
|
* ERRORS {boolean|number} true, false, 1 or 0 |
345
|
|
|
* CRYPTO {boolean|number} true, false, 1 or 0 |
346
|
|
|
* MODULO_MODE {number} 0 to 9 inclusive |
347
|
|
|
* POW_PRECISION {number} 0 to MAX inclusive |
348
|
|
|
* FORMAT {object} See BigNumber.prototype.toFormat |
349
|
|
|
* decimalSeparator {string} |
350
|
|
|
* groupSeparator {string} |
351
|
|
|
* groupSize {number} |
352
|
|
|
* secondaryGroupSize {number} |
353
|
|
|
* fractionGroupSeparator {string} |
354
|
|
|
* fractionGroupSize {number} |
355
|
|
|
* |
356
|
|
|
* (The values assigned to the above FORMAT object properties are not checked for validity.) |
357
|
|
|
* |
358
|
|
|
* E.g. |
359
|
|
|
* BigNumber.config(20, 4) is equivalent to |
360
|
|
|
* BigNumber.config({ DECIMAL_PLACES : 20, ROUNDING_MODE : 4 }) |
361
|
|
|
* |
362
|
|
|
* Ignore properties/parameters set to null or undefined. |
363
|
|
|
* Return an object with the properties current values. |
364
|
|
|
*/ |
365
|
|
|
BigNumber.config = BigNumber.set = function () { |
366
|
|
|
var v, p, |
367
|
|
|
i = 0, |
368
|
|
|
r = {}, |
369
|
|
|
a = arguments, |
370
|
|
|
o = a[0], |
371
|
|
|
has = o && typeof o == 'object' |
372
|
|
|
? function () { if ( o.hasOwnProperty(p) ) return ( v = o[p] ) != null; } |
373
|
|
|
: function () { if ( a.length > i ) return ( v = a[i++] ) != null; }; |
374
|
|
|
|
375
|
|
|
// DECIMAL_PLACES {number} Integer, 0 to MAX inclusive. |
376
|
|
|
// 'config() DECIMAL_PLACES not an integer: {v}' |
377
|
|
|
// 'config() DECIMAL_PLACES out of range: {v}' |
378
|
|
|
if ( has( p = 'DECIMAL_PLACES' ) && isValidInt( v, 0, MAX, 2, p ) ) { |
379
|
|
|
DECIMAL_PLACES = v | 0; |
380
|
|
|
} |
381
|
|
|
r[p] = DECIMAL_PLACES; |
382
|
|
|
|
383
|
|
|
// ROUNDING_MODE {number} Integer, 0 to 8 inclusive. |
384
|
|
|
// 'config() ROUNDING_MODE not an integer: {v}' |
385
|
|
|
// 'config() ROUNDING_MODE out of range: {v}' |
386
|
|
|
if ( has( p = 'ROUNDING_MODE' ) && isValidInt( v, 0, 8, 2, p ) ) { |
387
|
|
|
ROUNDING_MODE = v | 0; |
388
|
|
|
} |
389
|
|
|
r[p] = ROUNDING_MODE; |
390
|
|
|
|
391
|
|
|
// EXPONENTIAL_AT {number|number[]} |
392
|
|
|
// Integer, -MAX to MAX inclusive or [integer -MAX to 0 inclusive, 0 to MAX inclusive]. |
393
|
|
|
// 'config() EXPONENTIAL_AT not an integer: {v}' |
394
|
|
|
// 'config() EXPONENTIAL_AT out of range: {v}' |
395
|
|
|
if ( has( p = 'EXPONENTIAL_AT' ) ) { |
396
|
|
|
|
397
|
|
|
if ( isArray(v) ) { |
398
|
|
|
if ( isValidInt( v[0], -MAX, 0, 2, p ) && isValidInt( v[1], 0, MAX, 2, p ) ) { |
399
|
|
|
TO_EXP_NEG = v[0] | 0; |
400
|
|
|
TO_EXP_POS = v[1] | 0; |
401
|
|
|
} |
402
|
|
|
} else if ( isValidInt( v, -MAX, MAX, 2, p ) ) { |
403
|
|
|
TO_EXP_NEG = -( TO_EXP_POS = ( v < 0 ? -v : v ) | 0 ); |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
r[p] = [ TO_EXP_NEG, TO_EXP_POS ]; |
407
|
|
|
|
408
|
|
|
// RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or |
409
|
|
|
// [integer -MAX to -1 inclusive, integer 1 to MAX inclusive]. |
410
|
|
|
// 'config() RANGE not an integer: {v}' |
411
|
|
|
// 'config() RANGE cannot be zero: {v}' |
412
|
|
|
// 'config() RANGE out of range: {v}' |
413
|
|
|
if ( has( p = 'RANGE' ) ) { |
414
|
|
|
|
415
|
|
|
if ( isArray(v) ) { |
416
|
|
|
if ( isValidInt( v[0], -MAX, -1, 2, p ) && isValidInt( v[1], 1, MAX, 2, p ) ) { |
417
|
|
|
MIN_EXP = v[0] | 0; |
418
|
|
|
MAX_EXP = v[1] | 0; |
419
|
|
|
} |
420
|
|
|
} else if ( isValidInt( v, -MAX, MAX, 2, p ) ) { |
421
|
|
|
if ( v | 0 ) MIN_EXP = -( MAX_EXP = ( v < 0 ? -v : v ) | 0 ); |
|
|
|
|
422
|
|
|
else if (ERRORS) raise( 2, p + ' cannot be zero', v ); |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
r[p] = [ MIN_EXP, MAX_EXP ]; |
426
|
|
|
|
427
|
|
|
// ERRORS {boolean|number} true, false, 1 or 0. |
428
|
|
|
// 'config() ERRORS not a boolean or binary digit: {v}' |
429
|
|
|
if ( has( p = 'ERRORS' ) ) { |
430
|
|
|
|
431
|
|
|
if ( v === !!v || v === 1 || v === 0 ) { |
432
|
|
|
id = 0; |
433
|
|
|
isValidInt = ( ERRORS = !!v ) ? intValidatorWithErrors : intValidatorNoErrors; |
434
|
|
|
} else if (ERRORS) { |
435
|
|
|
raise( 2, p + notBool, v ); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
r[p] = ERRORS; |
439
|
|
|
|
440
|
|
|
// CRYPTO {boolean|number} true, false, 1 or 0. |
441
|
|
|
// 'config() CRYPTO not a boolean or binary digit: {v}' |
442
|
|
|
// 'config() crypto unavailable: {crypto}' |
443
|
|
|
if ( has( p = 'CRYPTO' ) ) { |
444
|
|
|
|
445
|
|
|
if ( v === true || v === false || v === 1 || v === 0 ) { |
446
|
|
|
if (v) { |
447
|
|
|
v = typeof crypto == 'undefined'; |
448
|
|
|
if ( !v && crypto && (crypto.getRandomValues || crypto.randomBytes)) { |
|
|
|
|
449
|
|
|
CRYPTO = true; |
450
|
|
|
} else if (ERRORS) { |
451
|
|
|
raise( 2, 'crypto unavailable', v ? void 0 : crypto ); |
|
|
|
|
452
|
|
|
} else { |
453
|
|
|
CRYPTO = false; |
454
|
|
|
} |
455
|
|
|
} else { |
456
|
|
|
CRYPTO = false; |
457
|
|
|
} |
458
|
|
|
} else if (ERRORS) { |
459
|
|
|
raise( 2, p + notBool, v ); |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
r[p] = CRYPTO; |
463
|
|
|
|
464
|
|
|
// MODULO_MODE {number} Integer, 0 to 9 inclusive. |
465
|
|
|
// 'config() MODULO_MODE not an integer: {v}' |
466
|
|
|
// 'config() MODULO_MODE out of range: {v}' |
467
|
|
|
if ( has( p = 'MODULO_MODE' ) && isValidInt( v, 0, 9, 2, p ) ) { |
|
|
|
|
468
|
|
|
MODULO_MODE = v | 0; |
469
|
|
|
} |
470
|
|
|
r[p] = MODULO_MODE; |
471
|
|
|
|
472
|
|
|
// POW_PRECISION {number} Integer, 0 to MAX inclusive. |
473
|
|
|
// 'config() POW_PRECISION not an integer: {v}' |
474
|
|
|
// 'config() POW_PRECISION out of range: {v}' |
475
|
|
|
if ( has( p = 'POW_PRECISION' ) && isValidInt( v, 0, MAX, 2, p ) ) { |
476
|
|
|
POW_PRECISION = v | 0; |
477
|
|
|
} |
478
|
|
|
r[p] = POW_PRECISION; |
479
|
|
|
|
480
|
|
|
// FORMAT {object} |
481
|
|
|
// 'config() FORMAT not an object: {v}' |
482
|
|
|
if ( has( p = 'FORMAT' ) ) { |
483
|
|
|
|
484
|
|
|
if ( typeof v == 'object' ) { |
485
|
|
|
FORMAT = v; |
486
|
|
|
} else if (ERRORS) { |
487
|
|
|
raise( 2, p + ' not an object', v ); |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
r[p] = FORMAT; |
491
|
|
|
|
492
|
|
|
return r; |
493
|
|
|
}; |
494
|
|
|
|
495
|
|
|
|
496
|
|
|
/* |
497
|
|
|
* Return a new BigNumber whose value is the maximum of the arguments. |
498
|
|
|
* |
499
|
|
|
* arguments {number|string|BigNumber} |
500
|
|
|
*/ |
501
|
|
|
BigNumber.max = function () { return maxOrMin( arguments, P.lt ); }; |
502
|
|
|
|
503
|
|
|
|
504
|
|
|
/* |
505
|
|
|
* Return a new BigNumber whose value is the minimum of the arguments. |
506
|
|
|
* |
507
|
|
|
* arguments {number|string|BigNumber} |
508
|
|
|
*/ |
509
|
|
|
BigNumber.min = function () { return maxOrMin( arguments, P.gt ); }; |
510
|
|
|
|
511
|
|
|
|
512
|
|
|
/* |
513
|
|
|
* Return a new BigNumber with a random value equal to or greater than 0 and less than 1, |
514
|
|
|
* and with dp, or DECIMAL_PLACES if dp is omitted, decimal places (or less if trailing |
515
|
|
|
* zeros are produced). |
516
|
|
|
* |
517
|
|
|
* [dp] {number} Decimal places. Integer, 0 to MAX inclusive. |
518
|
|
|
* |
519
|
|
|
* 'random() decimal places not an integer: {dp}' |
520
|
|
|
* 'random() decimal places out of range: {dp}' |
521
|
|
|
* 'random() crypto unavailable: {crypto}' |
522
|
|
|
*/ |
523
|
|
|
BigNumber.random = (function () { |
524
|
|
|
var pow2_53 = 0x20000000000000; |
525
|
|
|
|
526
|
|
|
// Return a 53 bit integer n, where 0 <= n < 9007199254740992. |
527
|
|
|
// Check if Math.random() produces more than 32 bits of randomness. |
528
|
|
|
// If it does, assume at least 53 bits are produced, otherwise assume at least 30 bits. |
529
|
|
|
// 0x40000000 is 2^30, 0x800000 is 2^23, 0x1fffff is 2^21 - 1. |
530
|
|
|
var random53bitInt = (Math.random() * pow2_53) & 0x1fffff |
|
|
|
|
531
|
|
|
? function () { return mathfloor( Math.random() * pow2_53 ); } |
532
|
|
|
: function () { return ((Math.random() * 0x40000000 | 0) * 0x800000) + |
533
|
|
|
(Math.random() * 0x800000 | 0); }; |
534
|
|
|
|
535
|
|
|
return function (dp) { |
536
|
|
|
var a, b, e, k, v, |
537
|
|
|
i = 0, |
538
|
|
|
c = [], |
539
|
|
|
rand = new BigNumber(ONE); |
540
|
|
|
|
541
|
|
|
dp = dp == null || !isValidInt( dp, 0, MAX, 14 ) ? DECIMAL_PLACES : dp | 0; |
542
|
|
|
k = mathceil( dp / LOG_BASE ); |
543
|
|
|
|
544
|
|
|
if (CRYPTO) { |
545
|
|
|
|
546
|
|
|
// Browsers supporting crypto.getRandomValues. |
547
|
|
|
if (crypto.getRandomValues) { |
548
|
|
|
|
549
|
|
|
a = crypto.getRandomValues( new Uint32Array( k *= 2 ) ); |
550
|
|
|
|
551
|
|
|
for ( ; i < k; ) { |
552
|
|
|
|
553
|
|
|
// 53 bits: |
554
|
|
|
// ((Math.pow(2, 32) - 1) * Math.pow(2, 21)).toString(2) |
555
|
|
|
// 11111 11111111 11111111 11111111 11100000 00000000 00000000 |
556
|
|
|
// ((Math.pow(2, 32) - 1) >>> 11).toString(2) |
557
|
|
|
// 11111 11111111 11111111 |
558
|
|
|
// 0x20000 is 2^21. |
559
|
|
|
v = a[i] * 0x20000 + (a[i + 1] >>> 11); |
560
|
|
|
|
561
|
|
|
// Rejection sampling: |
562
|
|
|
// 0 <= v < 9007199254740992 |
563
|
|
|
// Probability that v >= 9e15, is |
564
|
|
|
// 7199254740992 / 9007199254740992 ~= 0.0008, i.e. 1 in 1251 |
565
|
|
|
if ( v >= 9e15 ) { |
566
|
|
|
b = crypto.getRandomValues( new Uint32Array(2) ); |
567
|
|
|
a[i] = b[0]; |
568
|
|
|
a[i + 1] = b[1]; |
569
|
|
|
} else { |
570
|
|
|
|
571
|
|
|
// 0 <= v <= 8999999999999999 |
572
|
|
|
// 0 <= (v % 1e14) <= 99999999999999 |
573
|
|
|
c.push( v % 1e14 ); |
574
|
|
|
i += 2; |
575
|
|
|
} |
576
|
|
|
} |
577
|
|
|
i = k / 2; |
578
|
|
|
|
579
|
|
|
// Node.js supporting crypto.randomBytes. |
580
|
|
|
} else if (crypto.randomBytes) { |
581
|
|
|
|
582
|
|
|
// buffer |
583
|
|
|
a = crypto.randomBytes( k *= 7 ); |
584
|
|
|
|
585
|
|
|
for ( ; i < k; ) { |
586
|
|
|
|
587
|
|
|
// 0x1000000000000 is 2^48, 0x10000000000 is 2^40 |
588
|
|
|
// 0x100000000 is 2^32, 0x1000000 is 2^24 |
589
|
|
|
// 11111 11111111 11111111 11111111 11111111 11111111 11111111 |
590
|
|
|
// 0 <= v < 9007199254740992 |
591
|
|
|
v = ( ( a[i] & 31 ) * 0x1000000000000 ) + ( a[i + 1] * 0x10000000000 ) + |
592
|
|
|
( a[i + 2] * 0x100000000 ) + ( a[i + 3] * 0x1000000 ) + |
593
|
|
|
( a[i + 4] << 16 ) + ( a[i + 5] << 8 ) + a[i + 6]; |
594
|
|
|
|
595
|
|
|
if ( v >= 9e15 ) { |
596
|
|
|
crypto.randomBytes(7).copy( a, i ); |
597
|
|
|
} else { |
598
|
|
|
|
599
|
|
|
// 0 <= (v % 1e14) <= 99999999999999 |
600
|
|
|
c.push( v % 1e14 ); |
601
|
|
|
i += 7; |
602
|
|
|
} |
603
|
|
|
} |
604
|
|
|
i = k / 7; |
605
|
|
|
} else { |
606
|
|
|
CRYPTO = false; |
607
|
|
|
if (ERRORS) raise( 14, 'crypto unavailable', crypto ); |
608
|
|
|
} |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
// Use Math.random. |
612
|
|
|
if (!CRYPTO) { |
613
|
|
|
|
614
|
|
|
for ( ; i < k; ) { |
615
|
|
|
v = random53bitInt(); |
616
|
|
|
if ( v < 9e15 ) c[i++] = v % 1e14; |
617
|
|
|
} |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
k = c[--i]; |
621
|
|
|
dp %= LOG_BASE; |
622
|
|
|
|
623
|
|
|
// Convert trailing digits to zeros according to dp. |
624
|
|
|
if ( k && dp ) { |
625
|
|
|
v = POWS_TEN[LOG_BASE - dp]; |
626
|
|
|
c[i] = mathfloor( k / v ) * v; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
// Remove trailing elements which are zero. |
630
|
|
|
for ( ; c[i] === 0; c.pop(), i-- ); |
|
|
|
|
631
|
|
|
|
632
|
|
|
// Zero? |
633
|
|
|
if ( i < 0 ) { |
634
|
|
|
c = [ e = 0 ]; |
635
|
|
|
} else { |
636
|
|
|
|
637
|
|
|
// Remove leading elements which are zero and adjust exponent accordingly. |
638
|
|
|
for ( e = -1 ; c[0] === 0; c.splice(0, 1), e -= LOG_BASE); |
|
|
|
|
639
|
|
|
|
640
|
|
|
// Count the digits of the first element of c to determine leading zeros, and... |
641
|
|
|
for ( i = 1, v = c[0]; v >= 10; v /= 10, i++); |
|
|
|
|
642
|
|
|
|
643
|
|
|
// adjust the exponent accordingly. |
644
|
|
|
if ( i < LOG_BASE ) e -= LOG_BASE - i; |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
rand.e = e; |
648
|
|
|
rand.c = c; |
649
|
|
|
return rand; |
650
|
|
|
}; |
651
|
|
|
})(); |
652
|
|
|
|
653
|
|
|
|
654
|
|
|
// PRIVATE FUNCTIONS |
655
|
|
|
|
656
|
|
|
|
657
|
|
|
// Convert a numeric string of baseIn to a numeric string of baseOut. |
658
|
|
|
function convertBase( str, baseOut, baseIn, sign ) { |
659
|
|
|
var d, e, k, r, x, xc, y, |
660
|
|
|
i = str.indexOf( '.' ), |
661
|
|
|
dp = DECIMAL_PLACES, |
662
|
|
|
rm = ROUNDING_MODE; |
663
|
|
|
|
664
|
|
|
if ( baseIn < 37 ) str = str.toLowerCase(); |
665
|
|
|
|
666
|
|
|
// Non-integer. |
667
|
|
|
if ( i >= 0 ) { |
668
|
|
|
k = POW_PRECISION; |
669
|
|
|
|
670
|
|
|
// Unlimited precision. |
671
|
|
|
POW_PRECISION = 0; |
672
|
|
|
str = str.replace( '.', '' ); |
673
|
|
|
y = new BigNumber(baseIn); |
674
|
|
|
x = y.pow( str.length - i ); |
675
|
|
|
POW_PRECISION = k; |
676
|
|
|
|
677
|
|
|
// Convert str as if an integer, then restore the fraction part by dividing the |
678
|
|
|
// result by its base raised to a power. |
679
|
|
|
y.c = toBaseOut( toFixedPoint( coeffToString( x.c ), x.e ), 10, baseOut ); |
680
|
|
|
y.e = y.c.length; |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
// Convert the number as integer. |
684
|
|
|
xc = toBaseOut( str, baseIn, baseOut ); |
685
|
|
|
e = k = xc.length; |
686
|
|
|
|
687
|
|
|
// Remove trailing zeros. |
688
|
|
|
for ( ; xc[--k] == 0; xc.pop() ); |
|
|
|
|
689
|
|
|
if ( !xc[0] ) return '0'; |
690
|
|
|
|
691
|
|
|
if ( i < 0 ) { |
692
|
|
|
--e; |
693
|
|
|
} else { |
694
|
|
|
x.c = xc; |
|
|
|
|
695
|
|
|
x.e = e; |
696
|
|
|
|
697
|
|
|
// sign is needed for correct rounding. |
698
|
|
|
x.s = sign; |
699
|
|
|
x = div( x, y, dp, rm, baseOut ); |
|
|
|
|
700
|
|
|
xc = x.c; |
701
|
|
|
r = x.r; |
702
|
|
|
e = x.e; |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
d = e + dp + 1; |
706
|
|
|
|
707
|
|
|
// The rounding digit, i.e. the digit to the right of the digit that may be rounded up. |
708
|
|
|
i = xc[d]; |
709
|
|
|
k = baseOut / 2; |
710
|
|
|
r = r || d < 0 || xc[d + 1] != null; |
711
|
|
|
|
712
|
|
View Code Duplication |
r = rm < 4 ? ( i != null || r ) && ( rm == 0 || rm == ( x.s < 0 ? 3 : 2 ) ) |
713
|
|
|
: i > k || i == k &&( rm == 4 || r || rm == 6 && xc[d - 1] & 1 || |
714
|
|
|
rm == ( x.s < 0 ? 8 : 7 ) ); |
715
|
|
|
|
716
|
|
|
if ( d < 1 || !xc[0] ) { |
717
|
|
|
|
718
|
|
|
// 1^-dp or 0. |
719
|
|
|
str = r ? toFixedPoint( '1', -dp ) : '0'; |
720
|
|
|
} else { |
721
|
|
|
xc.length = d; |
722
|
|
|
|
723
|
|
|
if (r) { |
724
|
|
|
|
725
|
|
|
// Rounding up may mean the previous digit has to be rounded up and so on. |
726
|
|
|
for ( --baseOut; ++xc[--d] > baseOut; ) { |
727
|
|
|
xc[d] = 0; |
728
|
|
|
|
729
|
|
|
if ( !d ) { |
730
|
|
|
++e; |
731
|
|
|
xc = [1].concat(xc); |
732
|
|
|
} |
733
|
|
|
} |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
// Determine trailing zeros. |
737
|
|
|
for ( k = xc.length; !xc[--k]; ); |
|
|
|
|
738
|
|
|
|
739
|
|
|
// E.g. [4, 11, 15] becomes 4bf. |
740
|
|
|
for ( i = 0, str = ''; i <= k; str += ALPHABET.charAt( xc[i++] ) ); |
|
|
|
|
741
|
|
|
str = toFixedPoint( str, e ); |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
// The caller will add the sign. |
745
|
|
|
return str; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
|
749
|
|
|
// Perform division in the specified base. Called by div and convertBase. |
750
|
|
|
div = (function () { |
751
|
|
|
|
752
|
|
|
// Assume non-zero x and k. |
753
|
|
|
function multiply( x, k, base ) { |
754
|
|
|
var m, temp, xlo, xhi, |
755
|
|
|
carry = 0, |
756
|
|
|
i = x.length, |
757
|
|
|
klo = k % SQRT_BASE, |
758
|
|
|
khi = k / SQRT_BASE | 0; |
759
|
|
|
|
760
|
|
|
for ( x = x.slice(); i--; ) { |
761
|
|
|
xlo = x[i] % SQRT_BASE; |
762
|
|
|
xhi = x[i] / SQRT_BASE | 0; |
763
|
|
|
m = khi * xlo + xhi * klo; |
764
|
|
|
temp = klo * xlo + ( ( m % SQRT_BASE ) * SQRT_BASE ) + carry; |
765
|
|
|
carry = ( temp / base | 0 ) + ( m / SQRT_BASE | 0 ) + khi * xhi; |
766
|
|
|
x[i] = temp % base; |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
if (carry) x = [carry].concat(x); |
770
|
|
|
|
771
|
|
|
return x; |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
function compare( a, b, aL, bL ) { |
775
|
|
|
var i, cmp; |
776
|
|
|
|
777
|
|
|
if ( aL != bL ) { |
778
|
|
|
cmp = aL > bL ? 1 : -1; |
779
|
|
|
} else { |
780
|
|
|
|
781
|
|
|
for ( i = cmp = 0; i < aL; i++ ) { |
782
|
|
|
|
783
|
|
|
if ( a[i] != b[i] ) { |
784
|
|
|
cmp = a[i] > b[i] ? 1 : -1; |
785
|
|
|
break; |
786
|
|
|
} |
787
|
|
|
} |
788
|
|
|
} |
789
|
|
|
return cmp; |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
function subtract( a, b, aL, base ) { |
793
|
|
|
var i = 0; |
794
|
|
|
|
795
|
|
|
// Subtract b from a. |
796
|
|
|
for ( ; aL--; ) { |
797
|
|
|
a[aL] -= i; |
798
|
|
|
i = a[aL] < b[aL] ? 1 : 0; |
799
|
|
|
a[aL] = i * base + a[aL] - b[aL]; |
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
// Remove leading zeros. |
803
|
|
|
for ( ; !a[0] && a.length > 1; a.splice(0, 1) ); |
|
|
|
|
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
// x: dividend, y: divisor. |
807
|
|
|
return function ( x, y, dp, rm, base ) { |
808
|
|
|
var cmp, e, i, more, n, prod, prodL, q, qc, rem, remL, rem0, xi, xL, yc0, |
809
|
|
|
yL, yz, |
810
|
|
|
s = x.s == y.s ? 1 : -1, |
811
|
|
|
xc = x.c, |
812
|
|
|
yc = y.c; |
813
|
|
|
|
814
|
|
|
// Either NaN, Infinity or 0? |
815
|
|
View Code Duplication |
if ( !xc || !xc[0] || !yc || !yc[0] ) { |
816
|
|
|
|
817
|
|
|
return new BigNumber( |
818
|
|
|
|
819
|
|
|
// Return NaN if either NaN, or both Infinity or 0. |
820
|
|
|
!x.s || !y.s || ( xc ? yc && xc[0] == yc[0] : !yc ) ? NaN : |
821
|
|
|
|
822
|
|
|
// Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0. |
823
|
|
|
xc && xc[0] == 0 || !yc ? s * 0 : s / 0 |
824
|
|
|
); |
825
|
|
|
} |
826
|
|
|
|
827
|
|
|
q = new BigNumber(s); |
828
|
|
|
qc = q.c = []; |
829
|
|
|
e = x.e - y.e; |
830
|
|
|
s = dp + e + 1; |
831
|
|
|
|
832
|
|
|
if ( !base ) { |
833
|
|
|
base = BASE; |
834
|
|
|
e = bitFloor( x.e / LOG_BASE ) - bitFloor( y.e / LOG_BASE ); |
835
|
|
|
s = s / LOG_BASE | 0; |
836
|
|
|
} |
837
|
|
|
|
838
|
|
|
// Result exponent may be one less then the current value of e. |
839
|
|
|
// The coefficients of the BigNumbers from convertBase may have trailing zeros. |
840
|
|
|
for ( i = 0; yc[i] == ( xc[i] || 0 ); i++ ); |
|
|
|
|
841
|
|
|
if ( yc[i] > ( xc[i] || 0 ) ) e--; |
842
|
|
|
|
843
|
|
|
if ( s < 0 ) { |
844
|
|
|
qc.push(1); |
845
|
|
|
more = true; |
846
|
|
|
} else { |
847
|
|
|
xL = xc.length; |
848
|
|
|
yL = yc.length; |
849
|
|
|
i = 0; |
850
|
|
|
s += 2; |
851
|
|
|
|
852
|
|
|
// Normalise xc and yc so highest order digit of yc is >= base / 2. |
853
|
|
|
|
854
|
|
|
n = mathfloor( base / ( yc[0] + 1 ) ); |
855
|
|
|
|
856
|
|
|
// Not necessary, but to handle odd bases where yc[0] == ( base / 2 ) - 1. |
857
|
|
|
// if ( n > 1 || n++ == 1 && yc[0] < base / 2 ) { |
858
|
|
|
if ( n > 1 ) { |
859
|
|
|
yc = multiply( yc, n, base ); |
860
|
|
|
xc = multiply( xc, n, base ); |
861
|
|
|
yL = yc.length; |
862
|
|
|
xL = xc.length; |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
xi = yL; |
866
|
|
|
rem = xc.slice( 0, yL ); |
867
|
|
|
remL = rem.length; |
868
|
|
|
|
869
|
|
|
// Add zeros to make remainder as long as divisor. |
870
|
|
|
for ( ; remL < yL; rem[remL++] = 0 ); |
|
|
|
|
871
|
|
|
yz = yc.slice(); |
872
|
|
|
yz = [0].concat(yz); |
873
|
|
|
yc0 = yc[0]; |
874
|
|
|
if ( yc[1] >= base / 2 ) yc0++; |
875
|
|
|
// Not necessary, but to prevent trial digit n > base, when using base 3. |
876
|
|
|
// else if ( base == 3 && yc0 == 1 ) yc0 = 1 + 1e-15; |
877
|
|
|
|
878
|
|
|
do { |
879
|
|
|
n = 0; |
880
|
|
|
|
881
|
|
|
// Compare divisor and remainder. |
882
|
|
|
cmp = compare( yc, rem, yL, remL ); |
883
|
|
|
|
884
|
|
|
// If divisor < remainder. |
885
|
|
|
if ( cmp < 0 ) { |
886
|
|
|
|
887
|
|
|
// Calculate trial digit, n. |
888
|
|
|
|
889
|
|
|
rem0 = rem[0]; |
890
|
|
|
if ( yL != remL ) rem0 = rem0 * base + ( rem[1] || 0 ); |
891
|
|
|
|
892
|
|
|
// n is how many times the divisor goes into the current remainder. |
893
|
|
|
n = mathfloor( rem0 / yc0 ); |
894
|
|
|
|
895
|
|
|
// Algorithm: |
896
|
|
|
// 1. product = divisor * trial digit (n) |
897
|
|
|
// 2. if product > remainder: product -= divisor, n-- |
898
|
|
|
// 3. remainder -= product |
899
|
|
|
// 4. if product was < remainder at 2: |
900
|
|
|
// 5. compare new remainder and divisor |
901
|
|
|
// 6. If remainder > divisor: remainder -= divisor, n++ |
902
|
|
|
|
903
|
|
|
if ( n > 1 ) { |
904
|
|
|
|
905
|
|
|
// n may be > base only when base is 3. |
906
|
|
|
if (n >= base) n = base - 1; |
907
|
|
|
|
908
|
|
|
// product = divisor * trial digit. |
909
|
|
|
prod = multiply( yc, n, base ); |
910
|
|
|
prodL = prod.length; |
911
|
|
|
remL = rem.length; |
912
|
|
|
|
913
|
|
|
// Compare product and remainder. |
914
|
|
|
// If product > remainder. |
915
|
|
|
// Trial digit n too high. |
916
|
|
|
// n is 1 too high about 5% of the time, and is not known to have |
917
|
|
|
// ever been more than 1 too high. |
918
|
|
|
while ( compare( prod, rem, prodL, remL ) == 1 ) { |
919
|
|
|
n--; |
920
|
|
|
|
921
|
|
|
// Subtract divisor from product. |
922
|
|
|
subtract( prod, yL < prodL ? yz : yc, prodL, base ); |
923
|
|
|
prodL = prod.length; |
924
|
|
|
cmp = 1; |
925
|
|
|
} |
926
|
|
|
} else { |
927
|
|
|
|
928
|
|
|
// n is 0 or 1, cmp is -1. |
929
|
|
|
// If n is 0, there is no need to compare yc and rem again below, |
930
|
|
|
// so change cmp to 1 to avoid it. |
931
|
|
|
// If n is 1, leave cmp as -1, so yc and rem are compared again. |
932
|
|
|
if ( n == 0 ) { |
933
|
|
|
|
934
|
|
|
// divisor < remainder, so n must be at least 1. |
935
|
|
|
cmp = n = 1; |
936
|
|
|
} |
937
|
|
|
|
938
|
|
|
// product = divisor |
939
|
|
|
prod = yc.slice(); |
940
|
|
|
prodL = prod.length; |
941
|
|
|
} |
942
|
|
|
|
943
|
|
|
if ( prodL < remL ) prod = [0].concat(prod); |
944
|
|
|
|
945
|
|
|
// Subtract product from remainder. |
946
|
|
|
subtract( rem, prod, remL, base ); |
947
|
|
|
remL = rem.length; |
948
|
|
|
|
949
|
|
|
// If product was < remainder. |
950
|
|
|
if ( cmp == -1 ) { |
951
|
|
|
|
952
|
|
|
// Compare divisor and new remainder. |
953
|
|
|
// If divisor < new remainder, subtract divisor from remainder. |
954
|
|
|
// Trial digit n too low. |
955
|
|
|
// n is 1 too low about 5% of the time, and very rarely 2 too low. |
956
|
|
|
while ( compare( yc, rem, yL, remL ) < 1 ) { |
957
|
|
|
n++; |
958
|
|
|
|
959
|
|
|
// Subtract divisor from remainder. |
960
|
|
|
subtract( rem, yL < remL ? yz : yc, remL, base ); |
961
|
|
|
remL = rem.length; |
962
|
|
|
} |
963
|
|
|
} |
964
|
|
|
} else if ( cmp === 0 ) { |
965
|
|
|
n++; |
966
|
|
|
rem = [0]; |
967
|
|
|
} // else cmp === 1 and n will be 0 |
968
|
|
|
|
969
|
|
|
// Add the next digit, n, to the result array. |
970
|
|
|
qc[i++] = n; |
971
|
|
|
|
972
|
|
|
// Update the remainder. |
973
|
|
|
if ( rem[0] ) { |
974
|
|
|
rem[remL++] = xc[xi] || 0; |
975
|
|
|
} else { |
976
|
|
|
rem = [ xc[xi] ]; |
977
|
|
|
remL = 1; |
978
|
|
|
} |
979
|
|
|
} while ( ( xi++ < xL || rem[0] != null ) && s-- ); |
980
|
|
|
|
981
|
|
|
more = rem[0] != null; |
982
|
|
|
|
983
|
|
|
// Leading zero? |
984
|
|
|
if ( !qc[0] ) qc.splice(0, 1); |
985
|
|
|
} |
986
|
|
|
|
987
|
|
|
if ( base == BASE ) { |
988
|
|
|
|
989
|
|
|
// To calculate q.e, first get the number of digits of qc[0]. |
990
|
|
|
for ( i = 1, s = qc[0]; s >= 10; s /= 10, i++ ); |
|
|
|
|
991
|
|
|
round( q, dp + ( q.e = i + e * LOG_BASE - 1 ) + 1, rm, more ); |
992
|
|
|
|
993
|
|
|
// Caller is convertBase. |
994
|
|
|
} else { |
995
|
|
|
q.e = e; |
996
|
|
|
q.r = +more; |
997
|
|
|
} |
998
|
|
|
|
999
|
|
|
return q; |
1000
|
|
|
}; |
1001
|
|
|
})(); |
1002
|
|
|
|
1003
|
|
|
|
1004
|
|
|
/* |
1005
|
|
|
* Return a string representing the value of BigNumber n in fixed-point or exponential |
1006
|
|
|
* notation rounded to the specified decimal places or significant digits. |
1007
|
|
|
* |
1008
|
|
|
* n is a BigNumber. |
1009
|
|
|
* i is the index of the last digit required (i.e. the digit that may be rounded up). |
1010
|
|
|
* rm is the rounding mode. |
1011
|
|
|
* caller is caller id: toExponential 19, toFixed 20, toFormat 21, toPrecision 24. |
1012
|
|
|
*/ |
1013
|
|
|
function format( n, i, rm, caller ) { |
1014
|
|
|
var c0, e, ne, len, str; |
1015
|
|
|
|
1016
|
|
|
rm = rm != null && isValidInt( rm, 0, 8, caller, roundingMode ) |
1017
|
|
|
? rm | 0 : ROUNDING_MODE; |
1018
|
|
|
|
1019
|
|
|
if ( !n.c ) return n.toString(); |
1020
|
|
|
c0 = n.c[0]; |
1021
|
|
|
ne = n.e; |
1022
|
|
|
|
1023
|
|
|
if ( i == null ) { |
1024
|
|
|
str = coeffToString( n.c ); |
1025
|
|
|
str = caller == 19 || caller == 24 && ne <= TO_EXP_NEG |
1026
|
|
|
? toExponential( str, ne ) |
1027
|
|
|
: toFixedPoint( str, ne ); |
1028
|
|
|
} else { |
1029
|
|
|
n = round( new BigNumber(n), i, rm ); |
1030
|
|
|
|
1031
|
|
|
// n.e may have changed if the value was rounded up. |
1032
|
|
|
e = n.e; |
1033
|
|
|
|
1034
|
|
|
str = coeffToString( n.c ); |
1035
|
|
|
len = str.length; |
1036
|
|
|
|
1037
|
|
|
// toPrecision returns exponential notation if the number of significant digits |
1038
|
|
|
// specified is less than the number of digits necessary to represent the integer |
1039
|
|
|
// part of the value in fixed-point notation. |
1040
|
|
|
|
1041
|
|
|
// Exponential notation. |
1042
|
|
|
if ( caller == 19 || caller == 24 && ( i <= e || e <= TO_EXP_NEG ) ) { |
1043
|
|
|
|
1044
|
|
|
// Append zeros? |
1045
|
|
|
for ( ; len < i; str += '0', len++ ); |
|
|
|
|
1046
|
|
|
str = toExponential( str, e ); |
1047
|
|
|
|
1048
|
|
|
// Fixed-point notation. |
1049
|
|
|
} else { |
1050
|
|
|
i -= ne; |
1051
|
|
|
str = toFixedPoint( str, e ); |
1052
|
|
|
|
1053
|
|
|
// Append zeros? |
1054
|
|
|
if ( e + 1 > len ) { |
1055
|
|
|
if ( --i > 0 ) for ( str += '.'; i--; str += '0' ); |
|
|
|
|
1056
|
|
|
} else { |
1057
|
|
|
i += e - len; |
1058
|
|
|
if ( i > 0 ) { |
1059
|
|
|
if ( e + 1 == len ) str += '.'; |
1060
|
|
|
for ( ; i--; str += '0' ); |
|
|
|
|
1061
|
|
|
} |
1062
|
|
|
} |
1063
|
|
|
} |
1064
|
|
|
} |
1065
|
|
|
|
1066
|
|
|
return n.s < 0 && c0 ? '-' + str : str; |
1067
|
|
|
} |
1068
|
|
|
|
1069
|
|
|
|
1070
|
|
|
// Handle BigNumber.max and BigNumber.min. |
1071
|
|
|
function maxOrMin( args, method ) { |
1072
|
|
|
var m, n, |
1073
|
|
|
i = 0; |
1074
|
|
|
|
1075
|
|
|
if ( isArray( args[0] ) ) args = args[0]; |
1076
|
|
|
m = new BigNumber( args[0] ); |
1077
|
|
|
|
1078
|
|
|
for ( ; ++i < args.length; ) { |
1079
|
|
|
n = new BigNumber( args[i] ); |
1080
|
|
|
|
1081
|
|
|
// If any number is NaN, return NaN. |
1082
|
|
|
if ( !n.s ) { |
1083
|
|
|
m = n; |
1084
|
|
|
break; |
1085
|
|
|
} else if ( method.call( m, n ) ) { |
1086
|
|
|
m = n; |
1087
|
|
|
} |
1088
|
|
|
} |
1089
|
|
|
|
1090
|
|
|
return m; |
1091
|
|
|
} |
1092
|
|
|
|
1093
|
|
|
|
1094
|
|
|
/* |
1095
|
|
|
* Return true if n is an integer in range, otherwise throw. |
1096
|
|
|
* Use for argument validation when ERRORS is true. |
1097
|
|
|
*/ |
1098
|
|
|
function intValidatorWithErrors( n, min, max, caller, name ) { |
1099
|
|
|
if ( n < min || n > max || n != truncate(n) ) { |
1100
|
|
|
raise( caller, ( name || 'decimal places' ) + |
1101
|
|
|
( n < min || n > max ? ' out of range' : ' not an integer' ), n ); |
1102
|
|
|
} |
1103
|
|
|
|
1104
|
|
|
return true; |
1105
|
|
|
} |
1106
|
|
|
|
1107
|
|
|
|
1108
|
|
|
/* |
1109
|
|
|
* Strip trailing zeros, calculate base 10 exponent and check against MIN_EXP and MAX_EXP. |
1110
|
|
|
* Called by minus, plus and times. |
1111
|
|
|
*/ |
1112
|
|
|
function normalise( n, c, e ) { |
1113
|
|
|
var i = 1, |
1114
|
|
|
j = c.length; |
1115
|
|
|
|
1116
|
|
|
// Remove trailing zeros. |
1117
|
|
|
for ( ; !c[--j]; c.pop() ); |
|
|
|
|
1118
|
|
|
|
1119
|
|
|
// Calculate the base 10 exponent. First get the number of digits of c[0]. |
1120
|
|
|
for ( j = c[0]; j >= 10; j /= 10, i++ ); |
|
|
|
|
1121
|
|
|
|
1122
|
|
|
// Overflow? |
1123
|
|
|
if ( ( e = i + e * LOG_BASE - 1 ) > MAX_EXP ) { |
1124
|
|
|
|
1125
|
|
|
// Infinity. |
1126
|
|
|
n.c = n.e = null; |
1127
|
|
|
|
1128
|
|
|
// Underflow? |
1129
|
|
|
} else if ( e < MIN_EXP ) { |
1130
|
|
|
|
1131
|
|
|
// Zero. |
1132
|
|
|
n.c = [ n.e = 0 ]; |
1133
|
|
|
} else { |
1134
|
|
|
n.e = e; |
1135
|
|
|
n.c = c; |
1136
|
|
|
} |
1137
|
|
|
|
1138
|
|
|
return n; |
1139
|
|
|
} |
1140
|
|
|
|
1141
|
|
|
|
1142
|
|
|
// Handle values that fail the validity test in BigNumber. |
1143
|
|
|
parseNumeric = (function () { |
1144
|
|
|
var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i, |
1145
|
|
|
dotAfter = /^([^.]+)\.$/, |
1146
|
|
|
dotBefore = /^\.([^.]+)$/, |
1147
|
|
|
isInfinityOrNaN = /^-?(Infinity|NaN)$/, |
1148
|
|
|
whitespaceOrPlus = /^\s*\+(?=[\w.])|^\s+|\s+$/g; |
1149
|
|
|
|
1150
|
|
|
return function ( x, str, num, b ) { |
1151
|
|
|
var base, |
1152
|
|
|
s = num ? str : str.replace( whitespaceOrPlus, '' ); |
1153
|
|
|
|
1154
|
|
|
// No exception on ±Infinity or NaN. |
1155
|
|
|
if ( isInfinityOrNaN.test(s) ) { |
1156
|
|
|
x.s = isNaN(s) ? null : s < 0 ? -1 : 1; |
1157
|
|
|
} else { |
1158
|
|
|
if ( !num ) { |
1159
|
|
|
|
1160
|
|
|
// basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i |
1161
|
|
|
s = s.replace( basePrefix, function ( m, p1, p2 ) { |
1162
|
|
|
base = ( p2 = p2.toLowerCase() ) == 'x' ? 16 : p2 == 'b' ? 2 : 8; |
1163
|
|
|
return !b || b == base ? p1 : m; |
1164
|
|
|
}); |
1165
|
|
|
|
1166
|
|
|
if (b) { |
1167
|
|
|
base = b; |
1168
|
|
|
|
1169
|
|
|
// E.g. '1.' to '1', '.1' to '0.1' |
1170
|
|
|
s = s.replace( dotAfter, '$1' ).replace( dotBefore, '0.$1' ); |
1171
|
|
|
} |
1172
|
|
|
|
1173
|
|
|
if ( str != s ) return new BigNumber( s, base ); |
|
|
|
|
1174
|
|
|
} |
1175
|
|
|
|
1176
|
|
|
// 'new BigNumber() not a number: {n}' |
1177
|
|
|
// 'new BigNumber() not a base {b} number: {n}' |
1178
|
|
|
if (ERRORS) raise( id, 'not a' + ( b ? ' base ' + b : '' ) + ' number', str ); |
1179
|
|
|
x.s = null; |
1180
|
|
|
} |
1181
|
|
|
|
1182
|
|
|
x.c = x.e = null; |
1183
|
|
|
id = 0; |
1184
|
|
|
} |
1185
|
|
|
})(); |
1186
|
|
|
|
1187
|
|
|
|
1188
|
|
|
// Throw a BigNumber Error. |
1189
|
|
|
function raise( caller, msg, val ) { |
1190
|
|
|
var error = new Error( [ |
1191
|
|
|
'new BigNumber', // 0 |
1192
|
|
|
'cmp', // 1 |
1193
|
|
|
'config', // 2 |
1194
|
|
|
'div', // 3 |
1195
|
|
|
'divToInt', // 4 |
1196
|
|
|
'eq', // 5 |
1197
|
|
|
'gt', // 6 |
1198
|
|
|
'gte', // 7 |
1199
|
|
|
'lt', // 8 |
1200
|
|
|
'lte', // 9 |
1201
|
|
|
'minus', // 10 |
1202
|
|
|
'mod', // 11 |
1203
|
|
|
'plus', // 12 |
1204
|
|
|
'precision', // 13 |
1205
|
|
|
'random', // 14 |
1206
|
|
|
'round', // 15 |
1207
|
|
|
'shift', // 16 |
1208
|
|
|
'times', // 17 |
1209
|
|
|
'toDigits', // 18 |
1210
|
|
|
'toExponential', // 19 |
1211
|
|
|
'toFixed', // 20 |
1212
|
|
|
'toFormat', // 21 |
1213
|
|
|
'toFraction', // 22 |
1214
|
|
|
'pow', // 23 |
1215
|
|
|
'toPrecision', // 24 |
1216
|
|
|
'toString', // 25 |
1217
|
|
|
'BigNumber' // 26 |
1218
|
|
|
][caller] + '() ' + msg + ': ' + val ); |
1219
|
|
|
|
1220
|
|
|
error.name = 'BigNumber Error'; |
1221
|
|
|
id = 0; |
1222
|
|
|
throw error; |
1223
|
|
|
} |
1224
|
|
|
|
1225
|
|
|
|
1226
|
|
|
/* |
1227
|
|
|
* Round x to sd significant digits using rounding mode rm. Check for over/under-flow. |
1228
|
|
|
* If r is truthy, it is known that there are more digits after the rounding digit. |
1229
|
|
|
*/ |
1230
|
|
|
function round( x, sd, rm, r ) { |
1231
|
|
|
var d, i, j, k, n, ni, rd, |
1232
|
|
|
xc = x.c, |
1233
|
|
|
pows10 = POWS_TEN; |
1234
|
|
|
|
1235
|
|
|
// if x is not Infinity or NaN... |
1236
|
|
|
if (xc) { |
1237
|
|
|
|
1238
|
|
|
// rd is the rounding digit, i.e. the digit after the digit that may be rounded up. |
1239
|
|
|
// n is a base 1e14 number, the value of the element of array x.c containing rd. |
1240
|
|
|
// ni is the index of n within x.c. |
1241
|
|
|
// d is the number of digits of n. |
1242
|
|
|
// i is the index of rd within n including leading zeros. |
1243
|
|
|
// j is the actual index of rd within n (if < 0, rd is a leading zero). |
1244
|
|
|
out: { |
1245
|
|
|
|
1246
|
|
|
// Get the number of digits of the first element of xc. |
1247
|
|
|
for ( d = 1, k = xc[0]; k >= 10; k /= 10, d++ ); |
|
|
|
|
1248
|
|
|
i = sd - d; |
1249
|
|
|
|
1250
|
|
|
// If the rounding digit is in the first element of xc... |
1251
|
|
|
if ( i < 0 ) { |
1252
|
|
|
i += LOG_BASE; |
1253
|
|
|
j = sd; |
1254
|
|
|
n = xc[ ni = 0 ]; |
1255
|
|
|
|
1256
|
|
|
// Get the rounding digit at index j of n. |
1257
|
|
|
rd = n / pows10[ d - j - 1 ] % 10 | 0; |
1258
|
|
|
} else { |
1259
|
|
|
ni = mathceil( ( i + 1 ) / LOG_BASE ); |
1260
|
|
|
|
1261
|
|
|
if ( ni >= xc.length ) { |
1262
|
|
|
|
1263
|
|
|
if (r) { |
1264
|
|
|
|
1265
|
|
|
// Needed by sqrt. |
1266
|
|
|
for ( ; xc.length <= ni; xc.push(0) ); |
|
|
|
|
1267
|
|
|
n = rd = 0; |
1268
|
|
|
d = 1; |
1269
|
|
|
i %= LOG_BASE; |
1270
|
|
|
j = i - LOG_BASE + 1; |
1271
|
|
|
} else { |
1272
|
|
|
break out; |
1273
|
|
|
} |
1274
|
|
|
} else { |
1275
|
|
|
n = k = xc[ni]; |
1276
|
|
|
|
1277
|
|
|
// Get the number of digits of n. |
1278
|
|
|
for ( d = 1; k >= 10; k /= 10, d++ ); |
|
|
|
|
1279
|
|
|
|
1280
|
|
|
// Get the index of rd within n. |
1281
|
|
|
i %= LOG_BASE; |
1282
|
|
|
|
1283
|
|
|
// Get the index of rd within n, adjusted for leading zeros. |
1284
|
|
|
// The number of leading zeros of n is given by LOG_BASE - d. |
1285
|
|
|
j = i - LOG_BASE + d; |
1286
|
|
|
|
1287
|
|
|
// Get the rounding digit at index j of n. |
1288
|
|
|
rd = j < 0 ? 0 : n / pows10[ d - j - 1 ] % 10 | 0; |
1289
|
|
|
} |
1290
|
|
|
} |
1291
|
|
|
|
1292
|
|
|
r = r || sd < 0 || |
1293
|
|
|
|
1294
|
|
|
// Are there any non-zero digits after the rounding digit? |
1295
|
|
|
// The expression n % pows10[ d - j - 1 ] returns all digits of n to the right |
1296
|
|
|
// of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714. |
1297
|
|
|
xc[ni + 1] != null || ( j < 0 ? n : n % pows10[ d - j - 1 ] ); |
1298
|
|
|
|
1299
|
|
View Code Duplication |
r = rm < 4 |
1300
|
|
|
? ( rd || r ) && ( rm == 0 || rm == ( x.s < 0 ? 3 : 2 ) ) |
1301
|
|
|
: rd > 5 || rd == 5 && ( rm == 4 || r || rm == 6 && |
1302
|
|
|
|
1303
|
|
|
// Check whether the digit to the left of the rounding digit is odd. |
1304
|
|
|
( ( i > 0 ? j > 0 ? n / pows10[ d - j ] : 0 : xc[ni - 1] ) % 10 ) & 1 || |
1305
|
|
|
rm == ( x.s < 0 ? 8 : 7 ) ); |
1306
|
|
|
|
1307
|
|
|
if ( sd < 1 || !xc[0] ) { |
1308
|
|
|
xc.length = 0; |
1309
|
|
|
|
1310
|
|
|
if (r) { |
1311
|
|
|
|
1312
|
|
|
// Convert sd to decimal places. |
1313
|
|
|
sd -= x.e + 1; |
1314
|
|
|
|
1315
|
|
|
// 1, 0.1, 0.01, 0.001, 0.0001 etc. |
1316
|
|
|
xc[0] = pows10[ ( LOG_BASE - sd % LOG_BASE ) % LOG_BASE ]; |
1317
|
|
|
x.e = -sd || 0; |
1318
|
|
|
} else { |
1319
|
|
|
|
1320
|
|
|
// Zero. |
1321
|
|
|
xc[0] = x.e = 0; |
1322
|
|
|
} |
1323
|
|
|
|
1324
|
|
|
return x; |
1325
|
|
|
} |
1326
|
|
|
|
1327
|
|
|
// Remove excess digits. |
1328
|
|
|
if ( i == 0 ) { |
1329
|
|
|
xc.length = ni; |
1330
|
|
|
k = 1; |
1331
|
|
|
ni--; |
1332
|
|
|
} else { |
1333
|
|
|
xc.length = ni + 1; |
1334
|
|
|
k = pows10[ LOG_BASE - i ]; |
1335
|
|
|
|
1336
|
|
|
// E.g. 56700 becomes 56000 if 7 is the rounding digit. |
1337
|
|
|
// j > 0 means i > number of leading zeros of n. |
1338
|
|
|
xc[ni] = j > 0 ? mathfloor( n / pows10[ d - j ] % pows10[j] ) * k : 0; |
1339
|
|
|
} |
1340
|
|
|
|
1341
|
|
|
// Round up? |
1342
|
|
|
if (r) { |
1343
|
|
|
|
1344
|
|
|
for ( ; ; ) { |
1345
|
|
|
|
1346
|
|
|
// If the digit to be rounded up is in the first element of xc... |
1347
|
|
|
if ( ni == 0 ) { |
1348
|
|
|
|
1349
|
|
|
// i will be the length of xc[0] before k is added. |
1350
|
|
|
for ( i = 1, j = xc[0]; j >= 10; j /= 10, i++ ); |
|
|
|
|
1351
|
|
|
j = xc[0] += k; |
1352
|
|
|
for ( k = 1; j >= 10; j /= 10, k++ ); |
|
|
|
|
1353
|
|
|
|
1354
|
|
|
// if i != k the length has increased. |
1355
|
|
|
if ( i != k ) { |
1356
|
|
|
x.e++; |
1357
|
|
|
if ( xc[0] == BASE ) xc[0] = 1; |
1358
|
|
|
} |
1359
|
|
|
|
1360
|
|
|
break; |
1361
|
|
|
} else { |
1362
|
|
|
xc[ni] += k; |
1363
|
|
|
if ( xc[ni] != BASE ) break; |
1364
|
|
|
xc[ni--] = 0; |
1365
|
|
|
k = 1; |
1366
|
|
|
} |
1367
|
|
|
} |
1368
|
|
|
} |
1369
|
|
|
|
1370
|
|
|
// Remove trailing zeros. |
1371
|
|
|
for ( i = xc.length; xc[--i] === 0; xc.pop() ); |
|
|
|
|
1372
|
|
|
} |
1373
|
|
|
|
1374
|
|
|
// Overflow? Infinity. |
1375
|
|
|
if ( x.e > MAX_EXP ) { |
1376
|
|
|
x.c = x.e = null; |
1377
|
|
|
|
1378
|
|
|
// Underflow? Zero. |
1379
|
|
|
} else if ( x.e < MIN_EXP ) { |
1380
|
|
|
x.c = [ x.e = 0 ]; |
1381
|
|
|
} |
1382
|
|
|
} |
1383
|
|
|
|
1384
|
|
|
return x; |
1385
|
|
|
} |
1386
|
|
|
|
1387
|
|
|
|
1388
|
|
|
// PROTOTYPE/INSTANCE METHODS |
1389
|
|
|
|
1390
|
|
|
|
1391
|
|
|
/* |
1392
|
|
|
* Return a new BigNumber whose value is the absolute value of this BigNumber. |
1393
|
|
|
*/ |
1394
|
|
|
P.absoluteValue = P.abs = function () { |
1395
|
|
|
var x = new BigNumber(this); |
1396
|
|
|
if ( x.s < 0 ) x.s = 1; |
1397
|
|
|
return x; |
1398
|
|
|
}; |
1399
|
|
|
|
1400
|
|
|
|
1401
|
|
|
/* |
1402
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber rounded to a whole |
1403
|
|
|
* number in the direction of Infinity. |
1404
|
|
|
*/ |
1405
|
|
|
P.ceil = function () { |
1406
|
|
|
return round( new BigNumber(this), this.e + 1, 2 ); |
1407
|
|
|
}; |
1408
|
|
|
|
1409
|
|
|
|
1410
|
|
|
/* |
1411
|
|
|
* Return |
1412
|
|
|
* 1 if the value of this BigNumber is greater than the value of BigNumber(y, b), |
1413
|
|
|
* -1 if the value of this BigNumber is less than the value of BigNumber(y, b), |
1414
|
|
|
* 0 if they have the same value, |
1415
|
|
|
* or null if the value of either is NaN. |
1416
|
|
|
*/ |
1417
|
|
|
P.comparedTo = P.cmp = function ( y, b ) { |
1418
|
|
|
id = 1; |
1419
|
|
|
return compare( this, new BigNumber( y, b ) ); |
1420
|
|
|
}; |
1421
|
|
|
|
1422
|
|
|
|
1423
|
|
|
/* |
1424
|
|
|
* Return the number of decimal places of the value of this BigNumber, or null if the value |
1425
|
|
|
* of this BigNumber is ±Infinity or NaN. |
1426
|
|
|
*/ |
1427
|
|
|
P.decimalPlaces = P.dp = function () { |
1428
|
|
|
var n, v, |
1429
|
|
|
c = this.c; |
1430
|
|
|
|
1431
|
|
|
if ( !c ) return null; |
1432
|
|
|
n = ( ( v = c.length - 1 ) - bitFloor( this.e / LOG_BASE ) ) * LOG_BASE; |
1433
|
|
|
|
1434
|
|
|
// Subtract the number of trailing zeros of the last number. |
1435
|
|
|
if ( v = c[v] ) for ( ; v % 10 == 0; v /= 10, n-- ); |
|
|
|
|
1436
|
|
|
if ( n < 0 ) n = 0; |
1437
|
|
|
|
1438
|
|
|
return n; |
1439
|
|
|
}; |
1440
|
|
|
|
1441
|
|
|
|
1442
|
|
|
/* |
1443
|
|
|
* n / 0 = I |
1444
|
|
|
* n / N = N |
1445
|
|
|
* n / I = 0 |
1446
|
|
|
* 0 / n = 0 |
1447
|
|
|
* 0 / 0 = N |
1448
|
|
|
* 0 / N = N |
1449
|
|
|
* 0 / I = 0 |
1450
|
|
|
* N / n = N |
1451
|
|
|
* N / 0 = N |
1452
|
|
|
* N / N = N |
1453
|
|
|
* N / I = N |
1454
|
|
|
* I / n = I |
1455
|
|
|
* I / 0 = I |
1456
|
|
|
* I / N = N |
1457
|
|
|
* I / I = N |
1458
|
|
|
* |
1459
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber divided by the value of |
1460
|
|
|
* BigNumber(y, b), rounded according to DECIMAL_PLACES and ROUNDING_MODE. |
1461
|
|
|
*/ |
1462
|
|
|
P.dividedBy = P.div = function ( y, b ) { |
1463
|
|
|
id = 3; |
1464
|
|
|
return div( this, new BigNumber( y, b ), DECIMAL_PLACES, ROUNDING_MODE ); |
1465
|
|
|
}; |
1466
|
|
|
|
1467
|
|
|
|
1468
|
|
|
/* |
1469
|
|
|
* Return a new BigNumber whose value is the integer part of dividing the value of this |
1470
|
|
|
* BigNumber by the value of BigNumber(y, b). |
1471
|
|
|
*/ |
1472
|
|
|
P.dividedToIntegerBy = P.divToInt = function ( y, b ) { |
1473
|
|
|
id = 4; |
1474
|
|
|
return div( this, new BigNumber( y, b ), 0, 1 ); |
1475
|
|
|
}; |
1476
|
|
|
|
1477
|
|
|
|
1478
|
|
|
/* |
1479
|
|
|
* Return true if the value of this BigNumber is equal to the value of BigNumber(y, b), |
1480
|
|
|
* otherwise returns false. |
1481
|
|
|
*/ |
1482
|
|
|
P.equals = P.eq = function ( y, b ) { |
1483
|
|
|
id = 5; |
1484
|
|
|
return compare( this, new BigNumber( y, b ) ) === 0; |
1485
|
|
|
}; |
1486
|
|
|
|
1487
|
|
|
|
1488
|
|
|
/* |
1489
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber rounded to a whole |
1490
|
|
|
* number in the direction of -Infinity. |
1491
|
|
|
*/ |
1492
|
|
|
P.floor = function () { |
1493
|
|
|
return round( new BigNumber(this), this.e + 1, 3 ); |
1494
|
|
|
}; |
1495
|
|
|
|
1496
|
|
|
|
1497
|
|
|
/* |
1498
|
|
|
* Return true if the value of this BigNumber is greater than the value of BigNumber(y, b), |
1499
|
|
|
* otherwise returns false. |
1500
|
|
|
*/ |
1501
|
|
|
P.greaterThan = P.gt = function ( y, b ) { |
1502
|
|
|
id = 6; |
1503
|
|
|
return compare( this, new BigNumber( y, b ) ) > 0; |
1504
|
|
|
}; |
1505
|
|
|
|
1506
|
|
|
|
1507
|
|
|
/* |
1508
|
|
|
* Return true if the value of this BigNumber is greater than or equal to the value of |
1509
|
|
|
* BigNumber(y, b), otherwise returns false. |
1510
|
|
|
*/ |
1511
|
|
|
P.greaterThanOrEqualTo = P.gte = function ( y, b ) { |
1512
|
|
|
id = 7; |
1513
|
|
|
return ( b = compare( this, new BigNumber( y, b ) ) ) === 1 || b === 0; |
1514
|
|
|
|
1515
|
|
|
}; |
1516
|
|
|
|
1517
|
|
|
|
1518
|
|
|
/* |
1519
|
|
|
* Return true if the value of this BigNumber is a finite number, otherwise returns false. |
1520
|
|
|
*/ |
1521
|
|
|
P.isFinite = function () { |
1522
|
|
|
return !!this.c; |
1523
|
|
|
}; |
1524
|
|
|
|
1525
|
|
|
|
1526
|
|
|
/* |
1527
|
|
|
* Return true if the value of this BigNumber is an integer, otherwise return false. |
1528
|
|
|
*/ |
1529
|
|
|
P.isInteger = P.isInt = function () { |
1530
|
|
|
return !!this.c && bitFloor( this.e / LOG_BASE ) > this.c.length - 2; |
1531
|
|
|
}; |
1532
|
|
|
|
1533
|
|
|
|
1534
|
|
|
/* |
1535
|
|
|
* Return true if the value of this BigNumber is NaN, otherwise returns false. |
1536
|
|
|
*/ |
1537
|
|
|
P.isNaN = function () { |
1538
|
|
|
return !this.s; |
1539
|
|
|
}; |
1540
|
|
|
|
1541
|
|
|
|
1542
|
|
|
/* |
1543
|
|
|
* Return true if the value of this BigNumber is negative, otherwise returns false. |
1544
|
|
|
*/ |
1545
|
|
|
P.isNegative = P.isNeg = function () { |
1546
|
|
|
return this.s < 0; |
1547
|
|
|
}; |
1548
|
|
|
|
1549
|
|
|
|
1550
|
|
|
/* |
1551
|
|
|
* Return true if the value of this BigNumber is 0 or -0, otherwise returns false. |
1552
|
|
|
*/ |
1553
|
|
|
P.isZero = function () { |
1554
|
|
|
return !!this.c && this.c[0] == 0; |
1555
|
|
|
}; |
1556
|
|
|
|
1557
|
|
|
|
1558
|
|
|
/* |
1559
|
|
|
* Return true if the value of this BigNumber is less than the value of BigNumber(y, b), |
1560
|
|
|
* otherwise returns false. |
1561
|
|
|
*/ |
1562
|
|
|
P.lessThan = P.lt = function ( y, b ) { |
1563
|
|
|
id = 8; |
1564
|
|
|
return compare( this, new BigNumber( y, b ) ) < 0; |
1565
|
|
|
}; |
1566
|
|
|
|
1567
|
|
|
|
1568
|
|
|
/* |
1569
|
|
|
* Return true if the value of this BigNumber is less than or equal to the value of |
1570
|
|
|
* BigNumber(y, b), otherwise returns false. |
1571
|
|
|
*/ |
1572
|
|
|
P.lessThanOrEqualTo = P.lte = function ( y, b ) { |
1573
|
|
|
id = 9; |
1574
|
|
|
return ( b = compare( this, new BigNumber( y, b ) ) ) === -1 || b === 0; |
1575
|
|
|
}; |
1576
|
|
|
|
1577
|
|
|
|
1578
|
|
|
/* |
1579
|
|
|
* n - 0 = n |
1580
|
|
|
* n - N = N |
1581
|
|
|
* n - I = -I |
1582
|
|
|
* 0 - n = -n |
1583
|
|
|
* 0 - 0 = 0 |
1584
|
|
|
* 0 - N = N |
1585
|
|
|
* 0 - I = -I |
1586
|
|
|
* N - n = N |
1587
|
|
|
* N - 0 = N |
1588
|
|
|
* N - N = N |
1589
|
|
|
* N - I = N |
1590
|
|
|
* I - n = I |
1591
|
|
|
* I - 0 = I |
1592
|
|
|
* I - N = N |
1593
|
|
|
* I - I = N |
1594
|
|
|
* |
1595
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber minus the value of |
1596
|
|
|
* BigNumber(y, b). |
1597
|
|
|
*/ |
1598
|
|
|
P.minus = P.sub = function ( y, b ) { |
1599
|
|
|
var i, j, t, xLTy, |
1600
|
|
|
x = this, |
1601
|
|
|
a = x.s; |
1602
|
|
|
|
1603
|
|
|
id = 10; |
1604
|
|
|
y = new BigNumber( y, b ); |
1605
|
|
|
b = y.s; |
1606
|
|
|
|
1607
|
|
|
// Either NaN? |
1608
|
|
|
if ( !a || !b ) return new BigNumber(NaN); |
1609
|
|
|
|
1610
|
|
|
// Signs differ? |
1611
|
|
|
if ( a != b ) { |
1612
|
|
|
y.s = -b; |
1613
|
|
|
return x.plus(y); |
1614
|
|
|
} |
1615
|
|
|
|
1616
|
|
|
var xe = x.e / LOG_BASE, |
1617
|
|
|
ye = y.e / LOG_BASE, |
1618
|
|
|
xc = x.c, |
1619
|
|
|
yc = y.c; |
1620
|
|
|
|
1621
|
|
|
if ( !xe || !ye ) { |
1622
|
|
|
|
1623
|
|
|
// Either Infinity? |
1624
|
|
|
if ( !xc || !yc ) return xc ? ( y.s = -b, y ) : new BigNumber( yc ? x : NaN ); |
1625
|
|
|
|
1626
|
|
|
// Either zero? |
1627
|
|
|
if ( !xc[0] || !yc[0] ) { |
1628
|
|
|
|
1629
|
|
|
// Return y if y is non-zero, x if x is non-zero, or zero if both are zero. |
1630
|
|
|
return yc[0] ? ( y.s = -b, y ) : new BigNumber( xc[0] ? x : |
1631
|
|
|
|
1632
|
|
|
// IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity |
1633
|
|
|
ROUNDING_MODE == 3 ? -0 : 0 ); |
1634
|
|
|
} |
1635
|
|
|
} |
1636
|
|
|
|
1637
|
|
|
xe = bitFloor(xe); |
1638
|
|
|
ye = bitFloor(ye); |
1639
|
|
|
xc = xc.slice(); |
1640
|
|
|
|
1641
|
|
|
// Determine which is the bigger number. |
1642
|
|
|
if ( a = xe - ye ) { |
1643
|
|
|
|
1644
|
|
|
if ( xLTy = a < 0 ) { |
1645
|
|
|
a = -a; |
1646
|
|
|
t = xc; |
1647
|
|
|
} else { |
1648
|
|
|
ye = xe; |
1649
|
|
|
t = yc; |
1650
|
|
|
} |
1651
|
|
|
|
1652
|
|
|
t.reverse(); |
1653
|
|
|
|
1654
|
|
|
// Prepend zeros to equalise exponents. |
1655
|
|
|
for ( b = a; b--; t.push(0) ); |
|
|
|
|
1656
|
|
|
t.reverse(); |
1657
|
|
|
} else { |
1658
|
|
|
|
1659
|
|
|
// Exponents equal. Check digit by digit. |
1660
|
|
|
j = ( xLTy = ( a = xc.length ) < ( b = yc.length ) ) ? a : b; |
1661
|
|
|
|
1662
|
|
|
for ( a = b = 0; b < j; b++ ) { |
1663
|
|
|
|
1664
|
|
|
if ( xc[b] != yc[b] ) { |
1665
|
|
|
xLTy = xc[b] < yc[b]; |
1666
|
|
|
break; |
1667
|
|
|
} |
1668
|
|
|
} |
1669
|
|
|
} |
1670
|
|
|
|
1671
|
|
|
// x < y? Point xc to the array of the bigger number. |
1672
|
|
|
if (xLTy) t = xc, xc = yc, yc = t, y.s = -y.s; |
1673
|
|
|
|
1674
|
|
|
b = ( j = yc.length ) - ( i = xc.length ); |
1675
|
|
|
|
1676
|
|
|
// Append zeros to xc if shorter. |
1677
|
|
|
// No need to add zeros to yc if shorter as subtract only needs to start at yc.length. |
1678
|
|
|
if ( b > 0 ) for ( ; b--; xc[i++] = 0 ); |
|
|
|
|
1679
|
|
|
b = BASE - 1; |
1680
|
|
|
|
1681
|
|
|
// Subtract yc from xc. |
1682
|
|
|
for ( ; j > a; ) { |
1683
|
|
|
|
1684
|
|
|
if ( xc[--j] < yc[j] ) { |
1685
|
|
|
for ( i = j; i && !xc[--i]; xc[i] = b ); |
|
|
|
|
1686
|
|
|
--xc[i]; |
1687
|
|
|
xc[j] += BASE; |
1688
|
|
|
} |
1689
|
|
|
|
1690
|
|
|
xc[j] -= yc[j]; |
1691
|
|
|
} |
1692
|
|
|
|
1693
|
|
|
// Remove leading zeros and adjust exponent accordingly. |
1694
|
|
|
for ( ; xc[0] == 0; xc.splice(0, 1), --ye ); |
|
|
|
|
1695
|
|
|
|
1696
|
|
|
// Zero? |
1697
|
|
|
if ( !xc[0] ) { |
1698
|
|
|
|
1699
|
|
|
// Following IEEE 754 (2008) 6.3, |
1700
|
|
|
// n - n = +0 but n - n = -0 when rounding towards -Infinity. |
1701
|
|
|
y.s = ROUNDING_MODE == 3 ? -1 : 1; |
1702
|
|
|
y.c = [ y.e = 0 ]; |
1703
|
|
|
return y; |
1704
|
|
|
} |
1705
|
|
|
|
1706
|
|
|
// No need to check for Infinity as +x - +y != Infinity && -x - -y != Infinity |
1707
|
|
|
// for finite x and y. |
1708
|
|
|
return normalise( y, xc, ye ); |
1709
|
|
|
}; |
1710
|
|
|
|
1711
|
|
|
|
1712
|
|
|
/* |
1713
|
|
|
* n % 0 = N |
1714
|
|
|
* n % N = N |
1715
|
|
|
* n % I = n |
1716
|
|
|
* 0 % n = 0 |
1717
|
|
|
* -0 % n = -0 |
1718
|
|
|
* 0 % 0 = N |
1719
|
|
|
* 0 % N = N |
1720
|
|
|
* 0 % I = 0 |
1721
|
|
|
* N % n = N |
1722
|
|
|
* N % 0 = N |
1723
|
|
|
* N % N = N |
1724
|
|
|
* N % I = N |
1725
|
|
|
* I % n = N |
1726
|
|
|
* I % 0 = N |
1727
|
|
|
* I % N = N |
1728
|
|
|
* I % I = N |
1729
|
|
|
* |
1730
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber modulo the value of |
1731
|
|
|
* BigNumber(y, b). The result depends on the value of MODULO_MODE. |
1732
|
|
|
*/ |
1733
|
|
|
P.modulo = P.mod = function ( y, b ) { |
1734
|
|
|
var q, s, |
1735
|
|
|
x = this; |
1736
|
|
|
|
1737
|
|
|
id = 11; |
1738
|
|
|
y = new BigNumber( y, b ); |
1739
|
|
|
|
1740
|
|
|
// Return NaN if x is Infinity or NaN, or y is NaN or zero. |
1741
|
|
|
if ( !x.c || !y.s || y.c && !y.c[0] ) { |
1742
|
|
|
return new BigNumber(NaN); |
1743
|
|
|
|
1744
|
|
|
// Return x if y is Infinity or x is zero. |
1745
|
|
|
} else if ( !y.c || x.c && !x.c[0] ) { |
1746
|
|
|
return new BigNumber(x); |
1747
|
|
|
} |
1748
|
|
|
|
1749
|
|
|
if ( MODULO_MODE == 9 ) { |
1750
|
|
|
|
1751
|
|
|
// Euclidian division: q = sign(y) * floor(x / abs(y)) |
1752
|
|
|
// r = x - qy where 0 <= r < abs(y) |
1753
|
|
|
s = y.s; |
1754
|
|
|
y.s = 1; |
1755
|
|
|
q = div( x, y, 0, 3 ); |
1756
|
|
|
y.s = s; |
1757
|
|
|
q.s *= s; |
1758
|
|
|
} else { |
1759
|
|
|
q = div( x, y, 0, MODULO_MODE ); |
1760
|
|
|
} |
1761
|
|
|
|
1762
|
|
|
return x.minus( q.times(y) ); |
1763
|
|
|
}; |
1764
|
|
|
|
1765
|
|
|
|
1766
|
|
|
/* |
1767
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber negated, |
1768
|
|
|
* i.e. multiplied by -1. |
1769
|
|
|
*/ |
1770
|
|
|
P.negated = P.neg = function () { |
1771
|
|
|
var x = new BigNumber(this); |
1772
|
|
|
x.s = -x.s || null; |
1773
|
|
|
return x; |
1774
|
|
|
}; |
1775
|
|
|
|
1776
|
|
|
|
1777
|
|
|
/* |
1778
|
|
|
* n + 0 = n |
1779
|
|
|
* n + N = N |
1780
|
|
|
* n + I = I |
1781
|
|
|
* 0 + n = n |
1782
|
|
|
* 0 + 0 = 0 |
1783
|
|
|
* 0 + N = N |
1784
|
|
|
* 0 + I = I |
1785
|
|
|
* N + n = N |
1786
|
|
|
* N + 0 = N |
1787
|
|
|
* N + N = N |
1788
|
|
|
* N + I = N |
1789
|
|
|
* I + n = I |
1790
|
|
|
* I + 0 = I |
1791
|
|
|
* I + N = N |
1792
|
|
|
* I + I = I |
1793
|
|
|
* |
1794
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber plus the value of |
1795
|
|
|
* BigNumber(y, b). |
1796
|
|
|
*/ |
1797
|
|
|
P.plus = P.add = function ( y, b ) { |
1798
|
|
|
var t, |
1799
|
|
|
x = this, |
1800
|
|
|
a = x.s; |
1801
|
|
|
|
1802
|
|
|
id = 12; |
1803
|
|
|
y = new BigNumber( y, b ); |
1804
|
|
|
b = y.s; |
1805
|
|
|
|
1806
|
|
|
// Either NaN? |
1807
|
|
|
if ( !a || !b ) return new BigNumber(NaN); |
1808
|
|
|
|
1809
|
|
|
// Signs differ? |
1810
|
|
|
if ( a != b ) { |
1811
|
|
|
y.s = -b; |
1812
|
|
|
return x.minus(y); |
1813
|
|
|
} |
1814
|
|
|
|
1815
|
|
|
var xe = x.e / LOG_BASE, |
1816
|
|
|
ye = y.e / LOG_BASE, |
1817
|
|
|
xc = x.c, |
1818
|
|
|
yc = y.c; |
1819
|
|
|
|
1820
|
|
|
if ( !xe || !ye ) { |
1821
|
|
|
|
1822
|
|
|
// Return ±Infinity if either ±Infinity. |
1823
|
|
|
if ( !xc || !yc ) return new BigNumber( a / 0 ); |
1824
|
|
|
|
1825
|
|
|
// Either zero? |
1826
|
|
|
// Return y if y is non-zero, x if x is non-zero, or zero if both are zero. |
1827
|
|
|
if ( !xc[0] || !yc[0] ) return yc[0] ? y : new BigNumber( xc[0] ? x : a * 0 ); |
1828
|
|
|
} |
1829
|
|
|
|
1830
|
|
|
xe = bitFloor(xe); |
1831
|
|
|
ye = bitFloor(ye); |
1832
|
|
|
xc = xc.slice(); |
1833
|
|
|
|
1834
|
|
|
// Prepend zeros to equalise exponents. Faster to use reverse then do unshifts. |
1835
|
|
|
if ( a = xe - ye ) { |
1836
|
|
|
if ( a > 0 ) { |
1837
|
|
|
ye = xe; |
1838
|
|
|
t = yc; |
1839
|
|
|
} else { |
1840
|
|
|
a = -a; |
1841
|
|
|
t = xc; |
1842
|
|
|
} |
1843
|
|
|
|
1844
|
|
|
t.reverse(); |
1845
|
|
|
for ( ; a--; t.push(0) ); |
|
|
|
|
1846
|
|
|
t.reverse(); |
1847
|
|
|
} |
1848
|
|
|
|
1849
|
|
|
a = xc.length; |
1850
|
|
|
b = yc.length; |
1851
|
|
|
|
1852
|
|
|
// Point xc to the longer array, and b to the shorter length. |
1853
|
|
|
if ( a - b < 0 ) t = yc, yc = xc, xc = t, b = a; |
1854
|
|
|
|
1855
|
|
|
// Only start adding at yc.length - 1 as the further digits of xc can be ignored. |
1856
|
|
|
for ( a = 0; b; ) { |
1857
|
|
|
a = ( xc[--b] = xc[b] + yc[b] + a ) / BASE | 0; |
1858
|
|
|
xc[b] = BASE === xc[b] ? 0 : xc[b] % BASE; |
1859
|
|
|
} |
1860
|
|
|
|
1861
|
|
|
if (a) { |
1862
|
|
|
xc = [a].concat(xc); |
1863
|
|
|
++ye; |
1864
|
|
|
} |
1865
|
|
|
|
1866
|
|
|
// No need to check for zero, as +x + +y != 0 && -x + -y != 0 |
1867
|
|
|
// ye = MAX_EXP + 1 possible |
1868
|
|
|
return normalise( y, xc, ye ); |
1869
|
|
|
}; |
1870
|
|
|
|
1871
|
|
|
|
1872
|
|
|
/* |
1873
|
|
|
* Return the number of significant digits of the value of this BigNumber. |
1874
|
|
|
* |
1875
|
|
|
* [z] {boolean|number} Whether to count integer-part trailing zeros: true, false, 1 or 0. |
1876
|
|
|
*/ |
1877
|
|
|
P.precision = P.sd = function (z) { |
1878
|
|
|
var n, v, |
1879
|
|
|
x = this, |
1880
|
|
|
c = x.c; |
1881
|
|
|
|
1882
|
|
|
// 'precision() argument not a boolean or binary digit: {z}' |
1883
|
|
|
if ( z != null && z !== !!z && z !== 1 && z !== 0 ) { |
1884
|
|
|
if (ERRORS) raise( 13, 'argument' + notBool, z ); |
1885
|
|
|
if ( z != !!z ) z = null; |
1886
|
|
|
} |
1887
|
|
|
|
1888
|
|
|
if ( !c ) return null; |
1889
|
|
|
v = c.length - 1; |
1890
|
|
|
n = v * LOG_BASE + 1; |
1891
|
|
|
|
1892
|
|
|
if ( v = c[v] ) { |
1893
|
|
|
|
1894
|
|
|
// Subtract the number of trailing zeros of the last element. |
1895
|
|
|
for ( ; v % 10 == 0; v /= 10, n-- ); |
|
|
|
|
1896
|
|
|
|
1897
|
|
|
// Add the number of digits of the first element. |
1898
|
|
|
for ( v = c[0]; v >= 10; v /= 10, n++ ); |
|
|
|
|
1899
|
|
|
} |
1900
|
|
|
|
1901
|
|
|
if ( z && x.e + 1 > n ) n = x.e + 1; |
1902
|
|
|
|
1903
|
|
|
return n; |
1904
|
|
|
}; |
1905
|
|
|
|
1906
|
|
|
|
1907
|
|
|
/* |
1908
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber rounded to a maximum of |
1909
|
|
|
* dp decimal places using rounding mode rm, or to 0 and ROUNDING_MODE respectively if |
1910
|
|
|
* omitted. |
1911
|
|
|
* |
1912
|
|
|
* [dp] {number} Decimal places. Integer, 0 to MAX inclusive. |
1913
|
|
|
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. |
1914
|
|
|
* |
1915
|
|
|
* 'round() decimal places out of range: {dp}' |
1916
|
|
|
* 'round() decimal places not an integer: {dp}' |
1917
|
|
|
* 'round() rounding mode not an integer: {rm}' |
1918
|
|
|
* 'round() rounding mode out of range: {rm}' |
1919
|
|
|
*/ |
1920
|
|
|
P.round = function ( dp, rm ) { |
1921
|
|
|
var n = new BigNumber(this); |
1922
|
|
|
|
1923
|
|
|
if ( dp == null || isValidInt( dp, 0, MAX, 15 ) ) { |
1924
|
|
|
round( n, ~~dp + this.e + 1, rm == null || |
1925
|
|
|
!isValidInt( rm, 0, 8, 15, roundingMode ) ? ROUNDING_MODE : rm | 0 ); |
1926
|
|
|
} |
1927
|
|
|
|
1928
|
|
|
return n; |
1929
|
|
|
}; |
1930
|
|
|
|
1931
|
|
|
|
1932
|
|
|
/* |
1933
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber shifted by k places |
1934
|
|
|
* (powers of 10). Shift to the right if n > 0, and to the left if n < 0. |
1935
|
|
|
* |
1936
|
|
|
* k {number} Integer, -MAX_SAFE_INTEGER to MAX_SAFE_INTEGER inclusive. |
1937
|
|
|
* |
1938
|
|
|
* If k is out of range and ERRORS is false, the result will be ±0 if k < 0, or ±Infinity |
1939
|
|
|
* otherwise. |
1940
|
|
|
* |
1941
|
|
|
* 'shift() argument not an integer: {k}' |
1942
|
|
|
* 'shift() argument out of range: {k}' |
1943
|
|
|
*/ |
1944
|
|
|
P.shift = function (k) { |
1945
|
|
|
var n = this; |
1946
|
|
|
return isValidInt( k, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER, 16, 'argument' ) |
1947
|
|
|
|
1948
|
|
|
// k < 1e+21, or truncate(k) will produce exponential notation. |
1949
|
|
|
? n.times( '1e' + truncate(k) ) |
1950
|
|
|
: new BigNumber( n.c && n.c[0] && ( k < -MAX_SAFE_INTEGER || k > MAX_SAFE_INTEGER ) |
1951
|
|
|
? n.s * ( k < 0 ? 0 : 1 / 0 ) |
1952
|
|
|
: n ); |
1953
|
|
|
}; |
1954
|
|
|
|
1955
|
|
|
|
1956
|
|
|
/* |
1957
|
|
|
* sqrt(-n) = N |
1958
|
|
|
* sqrt( N) = N |
1959
|
|
|
* sqrt(-I) = N |
1960
|
|
|
* sqrt( I) = I |
1961
|
|
|
* sqrt( 0) = 0 |
1962
|
|
|
* sqrt(-0) = -0 |
1963
|
|
|
* |
1964
|
|
|
* Return a new BigNumber whose value is the square root of the value of this BigNumber, |
1965
|
|
|
* rounded according to DECIMAL_PLACES and ROUNDING_MODE. |
1966
|
|
|
*/ |
1967
|
|
|
P.squareRoot = P.sqrt = function () { |
1968
|
|
|
var m, n, r, rep, t, |
1969
|
|
|
x = this, |
1970
|
|
|
c = x.c, |
1971
|
|
|
s = x.s, |
1972
|
|
|
e = x.e, |
1973
|
|
|
dp = DECIMAL_PLACES + 4, |
1974
|
|
|
half = new BigNumber('0.5'); |
1975
|
|
|
|
1976
|
|
|
// Negative/NaN/Infinity/zero? |
1977
|
|
|
if ( s !== 1 || !c || !c[0] ) { |
1978
|
|
|
return new BigNumber( !s || s < 0 && ( !c || c[0] ) ? NaN : c ? x : 1 / 0 ); |
1979
|
|
|
} |
1980
|
|
|
|
1981
|
|
|
// Initial estimate. |
1982
|
|
|
s = Math.sqrt( +x ); |
1983
|
|
|
|
1984
|
|
|
// Math.sqrt underflow/overflow? |
1985
|
|
|
// Pass x to Math.sqrt as integer, then adjust the exponent of the result. |
1986
|
|
|
if ( s == 0 || s == 1 / 0 ) { |
1987
|
|
|
n = coeffToString(c); |
1988
|
|
|
if ( ( n.length + e ) % 2 == 0 ) n += '0'; |
1989
|
|
|
s = Math.sqrt(n); |
1990
|
|
|
e = bitFloor( ( e + 1 ) / 2 ) - ( e < 0 || e % 2 ); |
1991
|
|
|
|
1992
|
|
|
if ( s == 1 / 0 ) { |
1993
|
|
|
n = '1e' + e; |
1994
|
|
|
} else { |
1995
|
|
|
n = s.toExponential(); |
1996
|
|
|
n = n.slice( 0, n.indexOf('e') + 1 ) + e; |
1997
|
|
|
} |
1998
|
|
|
|
1999
|
|
|
r = new BigNumber(n); |
2000
|
|
|
} else { |
2001
|
|
|
r = new BigNumber( s + '' ); |
2002
|
|
|
} |
2003
|
|
|
|
2004
|
|
|
// Check for zero. |
2005
|
|
|
// r could be zero if MIN_EXP is changed after the this value was created. |
2006
|
|
|
// This would cause a division by zero (x/t) and hence Infinity below, which would cause |
2007
|
|
|
// coeffToString to throw. |
2008
|
|
|
if ( r.c[0] ) { |
2009
|
|
|
e = r.e; |
2010
|
|
|
s = e + dp; |
2011
|
|
|
if ( s < 3 ) s = 0; |
2012
|
|
|
|
2013
|
|
|
// Newton-Raphson iteration. |
2014
|
|
|
for ( ; ; ) { |
2015
|
|
|
t = r; |
2016
|
|
|
r = half.times( t.plus( div( x, t, dp, 1 ) ) ); |
2017
|
|
|
|
2018
|
|
|
if ( coeffToString( t.c ).slice( 0, s ) === ( n = |
2019
|
|
|
coeffToString( r.c ) ).slice( 0, s ) ) { |
2020
|
|
|
|
2021
|
|
|
// The exponent of r may here be one less than the final result exponent, |
2022
|
|
|
// e.g 0.0009999 (e-4) --> 0.001 (e-3), so adjust s so the rounding digits |
2023
|
|
|
// are indexed correctly. |
2024
|
|
|
if ( r.e < e ) --s; |
2025
|
|
|
n = n.slice( s - 3, s + 1 ); |
2026
|
|
|
|
2027
|
|
|
// The 4th rounding digit may be in error by -1 so if the 4 rounding digits |
2028
|
|
|
// are 9999 or 4999 (i.e. approaching a rounding boundary) continue the |
2029
|
|
|
// iteration. |
2030
|
|
|
if ( n == '9999' || !rep && n == '4999' ) { |
2031
|
|
|
|
2032
|
|
|
// On the first iteration only, check to see if rounding up gives the |
2033
|
|
|
// exact result as the nines may infinitely repeat. |
2034
|
|
|
if ( !rep ) { |
2035
|
|
|
round( t, t.e + DECIMAL_PLACES + 2, 0 ); |
2036
|
|
|
|
2037
|
|
|
if ( t.times(t).eq(x) ) { |
2038
|
|
|
r = t; |
2039
|
|
|
break; |
2040
|
|
|
} |
2041
|
|
|
} |
2042
|
|
|
|
2043
|
|
|
dp += 4; |
2044
|
|
|
s += 4; |
2045
|
|
|
rep = 1; |
2046
|
|
|
} else { |
2047
|
|
|
|
2048
|
|
|
// If rounding digits are null, 0{0,4} or 50{0,3}, check for exact |
2049
|
|
|
// result. If not, then there are further digits and m will be truthy. |
2050
|
|
|
if ( !+n || !+n.slice(1) && n.charAt(0) == '5' ) { |
2051
|
|
|
|
2052
|
|
|
// Truncate to the first rounding digit. |
2053
|
|
|
round( r, r.e + DECIMAL_PLACES + 2, 1 ); |
2054
|
|
|
m = !r.times(r).eq(x); |
2055
|
|
|
} |
2056
|
|
|
|
2057
|
|
|
break; |
2058
|
|
|
} |
2059
|
|
|
} |
2060
|
|
|
} |
2061
|
|
|
} |
2062
|
|
|
|
2063
|
|
|
return round( r, r.e + DECIMAL_PLACES + 1, ROUNDING_MODE, m ); |
|
|
|
|
2064
|
|
|
}; |
2065
|
|
|
|
2066
|
|
|
|
2067
|
|
|
/* |
2068
|
|
|
* n * 0 = 0 |
2069
|
|
|
* n * N = N |
2070
|
|
|
* n * I = I |
2071
|
|
|
* 0 * n = 0 |
2072
|
|
|
* 0 * 0 = 0 |
2073
|
|
|
* 0 * N = N |
2074
|
|
|
* 0 * I = N |
2075
|
|
|
* N * n = N |
2076
|
|
|
* N * 0 = N |
2077
|
|
|
* N * N = N |
2078
|
|
|
* N * I = N |
2079
|
|
|
* I * n = I |
2080
|
|
|
* I * 0 = N |
2081
|
|
|
* I * N = N |
2082
|
|
|
* I * I = I |
2083
|
|
|
* |
2084
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber times the value of |
2085
|
|
|
* BigNumber(y, b). |
2086
|
|
|
*/ |
2087
|
|
|
P.times = P.mul = function ( y, b ) { |
2088
|
|
|
var c, e, i, j, k, m, xcL, xlo, xhi, ycL, ylo, yhi, zc, |
2089
|
|
|
base, sqrtBase, |
2090
|
|
|
x = this, |
2091
|
|
|
xc = x.c, |
2092
|
|
|
yc = ( id = 17, y = new BigNumber( y, b ) ).c; |
2093
|
|
|
|
2094
|
|
|
// Either NaN, ±Infinity or ±0? |
2095
|
|
View Code Duplication |
if ( !xc || !yc || !xc[0] || !yc[0] ) { |
2096
|
|
|
|
2097
|
|
|
// Return NaN if either is NaN, or one is 0 and the other is Infinity. |
2098
|
|
|
if ( !x.s || !y.s || xc && !xc[0] && !yc || yc && !yc[0] && !xc ) { |
2099
|
|
|
y.c = y.e = y.s = null; |
2100
|
|
|
} else { |
2101
|
|
|
y.s *= x.s; |
2102
|
|
|
|
2103
|
|
|
// Return ±Infinity if either is ±Infinity. |
2104
|
|
|
if ( !xc || !yc ) { |
2105
|
|
|
y.c = y.e = null; |
2106
|
|
|
|
2107
|
|
|
// Return ±0 if either is ±0. |
2108
|
|
|
} else { |
2109
|
|
|
y.c = [0]; |
2110
|
|
|
y.e = 0; |
2111
|
|
|
} |
2112
|
|
|
} |
2113
|
|
|
|
2114
|
|
|
return y; |
2115
|
|
|
} |
2116
|
|
|
|
2117
|
|
|
e = bitFloor( x.e / LOG_BASE ) + bitFloor( y.e / LOG_BASE ); |
2118
|
|
|
y.s *= x.s; |
2119
|
|
|
xcL = xc.length; |
2120
|
|
|
ycL = yc.length; |
2121
|
|
|
|
2122
|
|
|
// Ensure xc points to longer array and xcL to its length. |
2123
|
|
|
if ( xcL < ycL ) zc = xc, xc = yc, yc = zc, i = xcL, xcL = ycL, ycL = i; |
2124
|
|
|
|
2125
|
|
|
// Initialise the result array with zeros. |
2126
|
|
|
for ( i = xcL + ycL, zc = []; i--; zc.push(0) ); |
|
|
|
|
2127
|
|
|
|
2128
|
|
|
base = BASE; |
2129
|
|
|
sqrtBase = SQRT_BASE; |
2130
|
|
|
|
2131
|
|
|
for ( i = ycL; --i >= 0; ) { |
2132
|
|
|
c = 0; |
2133
|
|
|
ylo = yc[i] % sqrtBase; |
2134
|
|
|
yhi = yc[i] / sqrtBase | 0; |
2135
|
|
|
|
2136
|
|
|
for ( k = xcL, j = i + k; j > i; ) { |
2137
|
|
|
xlo = xc[--k] % sqrtBase; |
2138
|
|
|
xhi = xc[k] / sqrtBase | 0; |
2139
|
|
|
m = yhi * xlo + xhi * ylo; |
2140
|
|
|
xlo = ylo * xlo + ( ( m % sqrtBase ) * sqrtBase ) + zc[j] + c; |
2141
|
|
|
c = ( xlo / base | 0 ) + ( m / sqrtBase | 0 ) + yhi * xhi; |
2142
|
|
|
zc[j--] = xlo % base; |
2143
|
|
|
} |
2144
|
|
|
|
2145
|
|
|
zc[j] = c; |
2146
|
|
|
} |
2147
|
|
|
|
2148
|
|
|
if (c) { |
2149
|
|
|
++e; |
2150
|
|
|
} else { |
2151
|
|
|
zc.splice(0, 1); |
2152
|
|
|
} |
2153
|
|
|
|
2154
|
|
|
return normalise( y, zc, e ); |
2155
|
|
|
}; |
2156
|
|
|
|
2157
|
|
|
|
2158
|
|
|
/* |
2159
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber rounded to a maximum of |
2160
|
|
|
* sd significant digits using rounding mode rm, or ROUNDING_MODE if rm is omitted. |
2161
|
|
|
* |
2162
|
|
|
* [sd] {number} Significant digits. Integer, 1 to MAX inclusive. |
2163
|
|
|
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. |
2164
|
|
|
* |
2165
|
|
|
* 'toDigits() precision out of range: {sd}' |
2166
|
|
|
* 'toDigits() precision not an integer: {sd}' |
2167
|
|
|
* 'toDigits() rounding mode not an integer: {rm}' |
2168
|
|
|
* 'toDigits() rounding mode out of range: {rm}' |
2169
|
|
|
*/ |
2170
|
|
|
P.toDigits = function ( sd, rm ) { |
2171
|
|
|
var n = new BigNumber(this); |
2172
|
|
|
sd = sd == null || !isValidInt( sd, 1, MAX, 18, 'precision' ) ? null : sd | 0; |
2173
|
|
|
rm = rm == null || !isValidInt( rm, 0, 8, 18, roundingMode ) ? ROUNDING_MODE : rm | 0; |
2174
|
|
|
return sd ? round( n, sd, rm ) : n; |
2175
|
|
|
}; |
2176
|
|
|
|
2177
|
|
|
|
2178
|
|
|
/* |
2179
|
|
|
* Return a string representing the value of this BigNumber in exponential notation and |
2180
|
|
|
* rounded using ROUNDING_MODE to dp fixed decimal places. |
2181
|
|
|
* |
2182
|
|
|
* [dp] {number} Decimal places. Integer, 0 to MAX inclusive. |
2183
|
|
|
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. |
2184
|
|
|
* |
2185
|
|
|
* 'toExponential() decimal places not an integer: {dp}' |
2186
|
|
|
* 'toExponential() decimal places out of range: {dp}' |
2187
|
|
|
* 'toExponential() rounding mode not an integer: {rm}' |
2188
|
|
|
* 'toExponential() rounding mode out of range: {rm}' |
2189
|
|
|
*/ |
2190
|
|
|
P.toExponential = function ( dp, rm ) { |
2191
|
|
|
return format( this, |
2192
|
|
|
dp != null && isValidInt( dp, 0, MAX, 19 ) ? ~~dp + 1 : null, rm, 19 ); |
2193
|
|
|
}; |
2194
|
|
|
|
2195
|
|
|
|
2196
|
|
|
/* |
2197
|
|
|
* Return a string representing the value of this BigNumber in fixed-point notation rounding |
2198
|
|
|
* to dp fixed decimal places using rounding mode rm, or ROUNDING_MODE if rm is omitted. |
2199
|
|
|
* |
2200
|
|
|
* Note: as with JavaScript's number type, (-0).toFixed(0) is '0', |
2201
|
|
|
* but e.g. (-0.00001).toFixed(0) is '-0'. |
2202
|
|
|
* |
2203
|
|
|
* [dp] {number} Decimal places. Integer, 0 to MAX inclusive. |
2204
|
|
|
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. |
2205
|
|
|
* |
2206
|
|
|
* 'toFixed() decimal places not an integer: {dp}' |
2207
|
|
|
* 'toFixed() decimal places out of range: {dp}' |
2208
|
|
|
* 'toFixed() rounding mode not an integer: {rm}' |
2209
|
|
|
* 'toFixed() rounding mode out of range: {rm}' |
2210
|
|
|
*/ |
2211
|
|
|
P.toFixed = function ( dp, rm ) { |
2212
|
|
|
return format( this, dp != null && isValidInt( dp, 0, MAX, 20 ) |
2213
|
|
|
? ~~dp + this.e + 1 : null, rm, 20 ); |
2214
|
|
|
}; |
2215
|
|
|
|
2216
|
|
|
|
2217
|
|
|
/* |
2218
|
|
|
* Return a string representing the value of this BigNumber in fixed-point notation rounded |
2219
|
|
|
* using rm or ROUNDING_MODE to dp decimal places, and formatted according to the properties |
2220
|
|
|
* of the FORMAT object (see BigNumber.config). |
2221
|
|
|
* |
2222
|
|
|
* FORMAT = { |
2223
|
|
|
* decimalSeparator : '.', |
2224
|
|
|
* groupSeparator : ',', |
2225
|
|
|
* groupSize : 3, |
2226
|
|
|
* secondaryGroupSize : 0, |
2227
|
|
|
* fractionGroupSeparator : '\xA0', // non-breaking space |
2228
|
|
|
* fractionGroupSize : 0 |
2229
|
|
|
* }; |
2230
|
|
|
* |
2231
|
|
|
* [dp] {number} Decimal places. Integer, 0 to MAX inclusive. |
2232
|
|
|
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. |
2233
|
|
|
* |
2234
|
|
|
* 'toFormat() decimal places not an integer: {dp}' |
2235
|
|
|
* 'toFormat() decimal places out of range: {dp}' |
2236
|
|
|
* 'toFormat() rounding mode not an integer: {rm}' |
2237
|
|
|
* 'toFormat() rounding mode out of range: {rm}' |
2238
|
|
|
*/ |
2239
|
|
|
P.toFormat = function ( dp, rm ) { |
2240
|
|
|
var str = format( this, dp != null && isValidInt( dp, 0, MAX, 21 ) |
2241
|
|
|
? ~~dp + this.e + 1 : null, rm, 21 ); |
2242
|
|
|
|
2243
|
|
|
if ( this.c ) { |
2244
|
|
|
var i, |
2245
|
|
|
arr = str.split('.'), |
2246
|
|
|
g1 = +FORMAT.groupSize, |
2247
|
|
|
g2 = +FORMAT.secondaryGroupSize, |
2248
|
|
|
groupSeparator = FORMAT.groupSeparator, |
2249
|
|
|
intPart = arr[0], |
2250
|
|
|
fractionPart = arr[1], |
2251
|
|
|
isNeg = this.s < 0, |
2252
|
|
|
intDigits = isNeg ? intPart.slice(1) : intPart, |
2253
|
|
|
len = intDigits.length; |
2254
|
|
|
|
2255
|
|
|
if (g2) i = g1, g1 = g2, g2 = i, len -= i; |
2256
|
|
|
|
2257
|
|
|
if ( g1 > 0 && len > 0 ) { |
2258
|
|
|
i = len % g1 || g1; |
2259
|
|
|
intPart = intDigits.substr( 0, i ); |
2260
|
|
|
|
2261
|
|
|
for ( ; i < len; i += g1 ) { |
2262
|
|
|
intPart += groupSeparator + intDigits.substr( i, g1 ); |
2263
|
|
|
} |
2264
|
|
|
|
2265
|
|
|
if ( g2 > 0 ) intPart += groupSeparator + intDigits.slice(i); |
2266
|
|
|
if (isNeg) intPart = '-' + intPart; |
2267
|
|
|
} |
2268
|
|
|
|
2269
|
|
|
str = fractionPart |
2270
|
|
|
? intPart + FORMAT.decimalSeparator + ( ( g2 = +FORMAT.fractionGroupSize ) |
2271
|
|
|
? fractionPart.replace( new RegExp( '\\d{' + g2 + '}\\B', 'g' ), |
2272
|
|
|
'$&' + FORMAT.fractionGroupSeparator ) |
2273
|
|
|
: fractionPart ) |
2274
|
|
|
: intPart; |
2275
|
|
|
} |
2276
|
|
|
|
2277
|
|
|
return str; |
2278
|
|
|
}; |
2279
|
|
|
|
2280
|
|
|
|
2281
|
|
|
/* |
2282
|
|
|
* Return a string array representing the value of this BigNumber as a simple fraction with |
2283
|
|
|
* an integer numerator and an integer denominator. The denominator will be a positive |
2284
|
|
|
* non-zero value less than or equal to the specified maximum denominator. If a maximum |
2285
|
|
|
* denominator is not specified, the denominator will be the lowest value necessary to |
2286
|
|
|
* represent the number exactly. |
2287
|
|
|
* |
2288
|
|
|
* [md] {number|string|BigNumber} Integer >= 1 and < Infinity. The maximum denominator. |
2289
|
|
|
* |
2290
|
|
|
* 'toFraction() max denominator not an integer: {md}' |
2291
|
|
|
* 'toFraction() max denominator out of range: {md}' |
2292
|
|
|
*/ |
2293
|
|
|
P.toFraction = function (md) { |
2294
|
|
|
var arr, d0, d2, e, exp, n, n0, q, s, |
2295
|
|
|
k = ERRORS, |
2296
|
|
|
x = this, |
2297
|
|
|
xc = x.c, |
2298
|
|
|
d = new BigNumber(ONE), |
2299
|
|
|
n1 = d0 = new BigNumber(ONE), |
2300
|
|
|
d1 = n0 = new BigNumber(ONE); |
2301
|
|
|
|
2302
|
|
|
if ( md != null ) { |
2303
|
|
|
ERRORS = false; |
2304
|
|
|
n = new BigNumber(md); |
2305
|
|
|
ERRORS = k; |
2306
|
|
|
|
2307
|
|
|
if ( !( k = n.isInt() ) || n.lt(ONE) ) { |
2308
|
|
|
|
2309
|
|
|
if (ERRORS) { |
2310
|
|
|
raise( 22, |
2311
|
|
|
'max denominator ' + ( k ? 'out of range' : 'not an integer' ), md ); |
2312
|
|
|
} |
2313
|
|
|
|
2314
|
|
|
// ERRORS is false: |
2315
|
|
|
// If md is a finite non-integer >= 1, round it to an integer and use it. |
2316
|
|
|
md = !k && n.c && round( n, n.e + 1, 1 ).gte(ONE) ? n : null; |
2317
|
|
|
} |
2318
|
|
|
} |
2319
|
|
|
|
2320
|
|
|
if ( !xc ) return x.toString(); |
2321
|
|
|
s = coeffToString(xc); |
2322
|
|
|
|
2323
|
|
|
// Determine initial denominator. |
2324
|
|
|
// d is a power of 10 and the minimum max denominator that specifies the value exactly. |
2325
|
|
|
e = d.e = s.length - x.e - 1; |
2326
|
|
|
d.c[0] = POWS_TEN[ ( exp = e % LOG_BASE ) < 0 ? LOG_BASE + exp : exp ]; |
2327
|
|
|
md = !md || n.cmp(d) > 0 ? ( e > 0 ? d : n1 ) : n; |
|
|
|
|
2328
|
|
|
|
2329
|
|
|
exp = MAX_EXP; |
2330
|
|
|
MAX_EXP = 1 / 0; |
2331
|
|
|
n = new BigNumber(s); |
2332
|
|
|
|
2333
|
|
|
// n0 = d1 = 0 |
2334
|
|
|
n0.c[0] = 0; |
2335
|
|
|
|
2336
|
|
|
for ( ; ; ) { |
2337
|
|
|
q = div( n, d, 0, 1 ); |
2338
|
|
|
d2 = d0.plus( q.times(d1) ); |
2339
|
|
|
if ( d2.cmp(md) == 1 ) break; |
2340
|
|
|
d0 = d1; |
2341
|
|
|
d1 = d2; |
2342
|
|
|
n1 = n0.plus( q.times( d2 = n1 ) ); |
2343
|
|
|
n0 = d2; |
2344
|
|
|
d = n.minus( q.times( d2 = d ) ); |
2345
|
|
|
n = d2; |
2346
|
|
|
} |
2347
|
|
|
|
2348
|
|
|
d2 = div( md.minus(d0), d1, 0, 1 ); |
2349
|
|
|
n0 = n0.plus( d2.times(n1) ); |
2350
|
|
|
d0 = d0.plus( d2.times(d1) ); |
2351
|
|
|
n0.s = n1.s = x.s; |
2352
|
|
|
e *= 2; |
2353
|
|
|
|
2354
|
|
|
// Determine which fraction is closer to x, n0/d0 or n1/d1 |
2355
|
|
|
arr = div( n1, d1, e, ROUNDING_MODE ).minus(x).abs().cmp( |
2356
|
|
|
div( n0, d0, e, ROUNDING_MODE ).minus(x).abs() ) < 1 |
2357
|
|
|
? [ n1.toString(), d1.toString() ] |
2358
|
|
|
: [ n0.toString(), d0.toString() ]; |
2359
|
|
|
|
2360
|
|
|
MAX_EXP = exp; |
2361
|
|
|
return arr; |
2362
|
|
|
}; |
2363
|
|
|
|
2364
|
|
|
|
2365
|
|
|
/* |
2366
|
|
|
* Return the value of this BigNumber converted to a number primitive. |
2367
|
|
|
*/ |
2368
|
|
|
P.toNumber = function () { |
2369
|
|
|
return +this; |
2370
|
|
|
}; |
2371
|
|
|
|
2372
|
|
|
|
2373
|
|
|
/* |
2374
|
|
|
* Return a BigNumber whose value is the value of this BigNumber raised to the power n. |
2375
|
|
|
* If m is present, return the result modulo m. |
2376
|
|
|
* If n is negative round according to DECIMAL_PLACES and ROUNDING_MODE. |
2377
|
|
|
* If POW_PRECISION is non-zero and m is not present, round to POW_PRECISION using |
2378
|
|
|
* ROUNDING_MODE. |
2379
|
|
|
* |
2380
|
|
|
* The modular power operation works efficiently when x, n, and m are positive integers, |
2381
|
|
|
* otherwise it is equivalent to calculating x.toPower(n).modulo(m) (with POW_PRECISION 0). |
2382
|
|
|
* |
2383
|
|
|
* n {number} Integer, -MAX_SAFE_INTEGER to MAX_SAFE_INTEGER inclusive. |
2384
|
|
|
* [m] {number|string|BigNumber} The modulus. |
2385
|
|
|
* |
2386
|
|
|
* 'pow() exponent not an integer: {n}' |
2387
|
|
|
* 'pow() exponent out of range: {n}' |
2388
|
|
|
* |
2389
|
|
|
* Performs 54 loop iterations for n of 9007199254740991. |
2390
|
|
|
*/ |
2391
|
|
|
P.toPower = P.pow = function ( n, m ) { |
2392
|
|
|
var k, y, z, |
2393
|
|
|
i = mathfloor( n < 0 ? -n : +n ), |
2394
|
|
|
x = this; |
2395
|
|
|
|
2396
|
|
|
if ( m != null ) { |
2397
|
|
|
id = 23; |
2398
|
|
|
m = new BigNumber(m); |
2399
|
|
|
} |
2400
|
|
|
|
2401
|
|
|
// Pass ±Infinity to Math.pow if exponent is out of range. |
2402
|
|
|
if ( !isValidInt( n, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER, 23, 'exponent' ) && |
2403
|
|
|
( !isFinite(n) || i > MAX_SAFE_INTEGER && ( n /= 0 ) || |
2404
|
|
|
parseFloat(n) != n && !( n = NaN ) ) || n == 0 ) { |
|
|
|
|
2405
|
|
|
k = Math.pow( +x, n ); |
2406
|
|
|
return new BigNumber( m ? k % m : k ); |
2407
|
|
|
} |
2408
|
|
|
|
2409
|
|
|
if (m) { |
2410
|
|
|
if ( n > 1 && x.gt(ONE) && x.isInt() && m.gt(ONE) && m.isInt() ) { |
2411
|
|
|
x = x.mod(m); |
2412
|
|
|
} else { |
2413
|
|
|
z = m; |
2414
|
|
|
|
2415
|
|
|
// Nullify m so only a single mod operation is performed at the end. |
2416
|
|
|
m = null; |
2417
|
|
|
} |
2418
|
|
|
} else if (POW_PRECISION) { |
2419
|
|
|
|
2420
|
|
|
// Truncating each coefficient array to a length of k after each multiplication |
2421
|
|
|
// equates to truncating significant digits to POW_PRECISION + [28, 41], |
2422
|
|
|
// i.e. there will be a minimum of 28 guard digits retained. |
2423
|
|
|
// (Using + 1.5 would give [9, 21] guard digits.) |
2424
|
|
|
k = mathceil( POW_PRECISION / LOG_BASE + 2 ); |
2425
|
|
|
} |
2426
|
|
|
|
2427
|
|
|
y = new BigNumber(ONE); |
2428
|
|
|
|
2429
|
|
|
for ( ; ; ) { |
2430
|
|
|
if ( i % 2 ) { |
2431
|
|
|
y = y.times(x); |
2432
|
|
|
if ( !y.c ) break; |
2433
|
|
|
if (k) { |
2434
|
|
|
if ( y.c.length > k ) y.c.length = k; |
2435
|
|
|
} else if (m) { |
2436
|
|
|
y = y.mod(m); |
2437
|
|
|
} |
2438
|
|
|
} |
2439
|
|
|
|
2440
|
|
|
i = mathfloor( i / 2 ); |
2441
|
|
|
if ( !i ) break; |
2442
|
|
|
x = x.times(x); |
2443
|
|
|
if (k) { |
2444
|
|
|
if ( x.c && x.c.length > k ) x.c.length = k; |
2445
|
|
|
} else if (m) { |
2446
|
|
|
x = x.mod(m); |
2447
|
|
|
} |
2448
|
|
|
} |
2449
|
|
|
|
2450
|
|
|
if (m) return y; |
2451
|
|
|
if ( n < 0 ) y = ONE.div(y); |
2452
|
|
|
|
2453
|
|
|
return z ? y.mod(z) : k ? round( y, POW_PRECISION, ROUNDING_MODE ) : y; |
2454
|
|
|
}; |
2455
|
|
|
|
2456
|
|
|
|
2457
|
|
|
/* |
2458
|
|
|
* Return a string representing the value of this BigNumber rounded to sd significant digits |
2459
|
|
|
* using rounding mode rm or ROUNDING_MODE. If sd is less than the number of digits |
2460
|
|
|
* necessary to represent the integer part of the value in fixed-point notation, then use |
2461
|
|
|
* exponential notation. |
2462
|
|
|
* |
2463
|
|
|
* [sd] {number} Significant digits. Integer, 1 to MAX inclusive. |
2464
|
|
|
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. |
2465
|
|
|
* |
2466
|
|
|
* 'toPrecision() precision not an integer: {sd}' |
2467
|
|
|
* 'toPrecision() precision out of range: {sd}' |
2468
|
|
|
* 'toPrecision() rounding mode not an integer: {rm}' |
2469
|
|
|
* 'toPrecision() rounding mode out of range: {rm}' |
2470
|
|
|
*/ |
2471
|
|
|
P.toPrecision = function ( sd, rm ) { |
2472
|
|
|
return format( this, sd != null && isValidInt( sd, 1, MAX, 24, 'precision' ) |
2473
|
|
|
? sd | 0 : null, rm, 24 ); |
2474
|
|
|
}; |
2475
|
|
|
|
2476
|
|
|
|
2477
|
|
|
/* |
2478
|
|
|
* Return a string representing the value of this BigNumber in base b, or base 10 if b is |
2479
|
|
|
* omitted. If a base is specified, including base 10, round according to DECIMAL_PLACES and |
2480
|
|
|
* ROUNDING_MODE. If a base is not specified, and this BigNumber has a positive exponent |
2481
|
|
|
* that is equal to or greater than TO_EXP_POS, or a negative exponent equal to or less than |
2482
|
|
|
* TO_EXP_NEG, return exponential notation. |
2483
|
|
|
* |
2484
|
|
|
* [b] {number} Integer, 2 to 64 inclusive. |
2485
|
|
|
* |
2486
|
|
|
* 'toString() base not an integer: {b}' |
2487
|
|
|
* 'toString() base out of range: {b}' |
2488
|
|
|
*/ |
2489
|
|
|
P.toString = function (b) { |
2490
|
|
|
var str, |
2491
|
|
|
n = this, |
2492
|
|
|
s = n.s, |
2493
|
|
|
e = n.e; |
2494
|
|
|
|
2495
|
|
|
// Infinity or NaN? |
2496
|
|
|
if ( e === null ) { |
2497
|
|
|
|
2498
|
|
|
if (s) { |
2499
|
|
|
str = 'Infinity'; |
2500
|
|
|
if ( s < 0 ) str = '-' + str; |
2501
|
|
|
} else { |
2502
|
|
|
str = 'NaN'; |
2503
|
|
|
} |
2504
|
|
|
} else { |
2505
|
|
|
str = coeffToString( n.c ); |
2506
|
|
|
|
2507
|
|
|
if ( b == null || !isValidInt( b, 2, 64, 25, 'base' ) ) { |
2508
|
|
|
str = e <= TO_EXP_NEG || e >= TO_EXP_POS |
2509
|
|
|
? toExponential( str, e ) |
2510
|
|
|
: toFixedPoint( str, e ); |
2511
|
|
|
} else { |
2512
|
|
|
str = convertBase( toFixedPoint( str, e ), b | 0, 10, s ); |
2513
|
|
|
} |
2514
|
|
|
|
2515
|
|
|
if ( s < 0 && n.c[0] ) str = '-' + str; |
2516
|
|
|
} |
2517
|
|
|
|
2518
|
|
|
return str; |
2519
|
|
|
}; |
2520
|
|
|
|
2521
|
|
|
|
2522
|
|
|
/* |
2523
|
|
|
* Return a new BigNumber whose value is the value of this BigNumber truncated to a whole |
2524
|
|
|
* number. |
2525
|
|
|
*/ |
2526
|
|
|
P.truncated = P.trunc = function () { |
2527
|
|
|
return round( new BigNumber(this), this.e + 1, 1 ); |
2528
|
|
|
}; |
2529
|
|
|
|
2530
|
|
|
|
2531
|
|
|
/* |
2532
|
|
|
* Return as toString, but do not accept a base argument, and include the minus sign for |
2533
|
|
|
* negative zero. |
2534
|
|
|
*/ |
2535
|
|
|
P.valueOf = P.toJSON = function () { |
2536
|
|
|
var str, |
2537
|
|
|
n = this, |
2538
|
|
|
e = n.e; |
2539
|
|
|
|
2540
|
|
|
if ( e === null ) return n.toString(); |
2541
|
|
|
|
2542
|
|
|
str = coeffToString( n.c ); |
2543
|
|
|
|
2544
|
|
|
str = e <= TO_EXP_NEG || e >= TO_EXP_POS |
2545
|
|
|
? toExponential( str, e ) |
2546
|
|
|
: toFixedPoint( str, e ); |
2547
|
|
|
|
2548
|
|
|
return n.s < 0 ? '-' + str : str; |
2549
|
|
|
}; |
2550
|
|
|
|
2551
|
|
|
|
2552
|
|
|
P.isBigNumber = true; |
2553
|
|
|
|
2554
|
|
|
if ( config != null ) BigNumber.config(config); |
2555
|
|
|
|
2556
|
|
|
return BigNumber; |
2557
|
|
|
} |
2558
|
|
|
|
2559
|
|
|
|
2560
|
|
|
// PRIVATE HELPER FUNCTIONS |
2561
|
|
|
|
2562
|
|
|
|
2563
|
|
|
function bitFloor(n) { |
2564
|
|
|
var i = n | 0; |
2565
|
|
|
return n > 0 || n === i ? i : i - 1; |
2566
|
|
|
} |
2567
|
|
|
|
2568
|
|
|
|
2569
|
|
|
// Return a coefficient array as a string of base 10 digits. |
2570
|
|
|
function coeffToString(a) { |
2571
|
|
|
var s, z, |
2572
|
|
|
i = 1, |
2573
|
|
|
j = a.length, |
2574
|
|
|
r = a[0] + ''; |
2575
|
|
|
|
2576
|
|
|
for ( ; i < j; ) { |
2577
|
|
|
s = a[i++] + ''; |
2578
|
|
|
z = LOG_BASE - s.length; |
2579
|
|
|
for ( ; z--; s = '0' + s ); |
|
|
|
|
2580
|
|
|
r += s; |
2581
|
|
|
} |
2582
|
|
|
|
2583
|
|
|
// Determine trailing zeros. |
2584
|
|
|
for ( j = r.length; r.charCodeAt(--j) === 48; ); |
|
|
|
|
2585
|
|
|
return r.slice( 0, j + 1 || 1 ); |
2586
|
|
|
} |
2587
|
|
|
|
2588
|
|
|
|
2589
|
|
|
// Compare the value of BigNumbers x and y. |
2590
|
|
|
function compare( x, y ) { |
2591
|
|
|
var a, b, |
2592
|
|
|
xc = x.c, |
2593
|
|
|
yc = y.c, |
2594
|
|
|
i = x.s, |
2595
|
|
|
j = y.s, |
2596
|
|
|
k = x.e, |
2597
|
|
|
l = y.e; |
2598
|
|
|
|
2599
|
|
|
// Either NaN? |
2600
|
|
|
if ( !i || !j ) return null; |
2601
|
|
|
|
2602
|
|
|
a = xc && !xc[0]; |
2603
|
|
|
b = yc && !yc[0]; |
2604
|
|
|
|
2605
|
|
|
// Either zero? |
2606
|
|
|
if ( a || b ) return a ? b ? 0 : -j : i; |
2607
|
|
|
|
2608
|
|
|
// Signs differ? |
2609
|
|
|
if ( i != j ) return i; |
2610
|
|
|
|
2611
|
|
|
a = i < 0; |
2612
|
|
|
b = k == l; |
2613
|
|
|
|
2614
|
|
|
// Either Infinity? |
2615
|
|
|
if ( !xc || !yc ) return b ? 0 : !xc ^ a ? 1 : -1; |
2616
|
|
|
|
2617
|
|
|
// Compare exponents. |
2618
|
|
|
if ( !b ) return k > l ^ a ? 1 : -1; |
2619
|
|
|
|
2620
|
|
|
j = ( k = xc.length ) < ( l = yc.length ) ? k : l; |
2621
|
|
|
|
2622
|
|
|
// Compare digit by digit. |
2623
|
|
|
for ( i = 0; i < j; i++ ) if ( xc[i] != yc[i] ) return xc[i] > yc[i] ^ a ? 1 : -1; |
2624
|
|
|
|
2625
|
|
|
// Compare lengths. |
2626
|
|
|
return k == l ? 0 : k > l ^ a ? 1 : -1; |
2627
|
|
|
} |
2628
|
|
|
|
2629
|
|
|
|
2630
|
|
|
/* |
2631
|
|
|
* Return true if n is a valid number in range, otherwise false. |
2632
|
|
|
* Use for argument validation when ERRORS is false. |
2633
|
|
|
* Note: parseInt('1e+1') == 1 but parseFloat('1e+1') == 10. |
2634
|
|
|
*/ |
2635
|
|
|
function intValidatorNoErrors( n, min, max ) { |
2636
|
|
|
return ( n = truncate(n) ) >= min && n <= max; |
2637
|
|
|
} |
2638
|
|
|
|
2639
|
|
|
|
2640
|
|
|
function isArray(obj) { |
2641
|
|
|
return Object.prototype.toString.call(obj) == '[object Array]'; |
2642
|
|
|
} |
2643
|
|
|
|
2644
|
|
|
|
2645
|
|
|
/* |
2646
|
|
|
* Convert string of baseIn to an array of numbers of baseOut. |
2647
|
|
|
* Eg. convertBase('255', 10, 16) returns [15, 15]. |
2648
|
|
|
* Eg. convertBase('ff', 16, 10) returns [2, 5, 5]. |
2649
|
|
|
*/ |
2650
|
|
|
function toBaseOut( str, baseIn, baseOut ) { |
2651
|
|
|
var j, |
2652
|
|
|
arr = [0], |
2653
|
|
|
arrL, |
2654
|
|
|
i = 0, |
2655
|
|
|
len = str.length; |
2656
|
|
|
|
2657
|
|
|
for ( ; i < len; ) { |
2658
|
|
|
for ( arrL = arr.length; arrL--; arr[arrL] *= baseIn ); |
|
|
|
|
2659
|
|
|
arr[ j = 0 ] += ALPHABET.indexOf( str.charAt( i++ ) ); |
2660
|
|
|
|
2661
|
|
|
for ( ; j < arr.length; j++ ) { |
2662
|
|
|
|
2663
|
|
|
if ( arr[j] > baseOut - 1 ) { |
2664
|
|
|
if ( arr[j + 1] == null ) arr[j + 1] = 0; |
2665
|
|
|
arr[j + 1] += arr[j] / baseOut | 0; |
2666
|
|
|
arr[j] %= baseOut; |
2667
|
|
|
} |
2668
|
|
|
} |
2669
|
|
|
} |
2670
|
|
|
|
2671
|
|
|
return arr.reverse(); |
2672
|
|
|
} |
2673
|
|
|
|
2674
|
|
|
|
2675
|
|
|
function toExponential( str, e ) { |
2676
|
|
|
return ( str.length > 1 ? str.charAt(0) + '.' + str.slice(1) : str ) + |
2677
|
|
|
( e < 0 ? 'e' : 'e+' ) + e; |
2678
|
|
|
} |
2679
|
|
|
|
2680
|
|
|
|
2681
|
|
|
function toFixedPoint( str, e ) { |
2682
|
|
|
var len, z; |
2683
|
|
|
|
2684
|
|
|
// Negative exponent? |
2685
|
|
|
if ( e < 0 ) { |
2686
|
|
|
|
2687
|
|
|
// Prepend zeros. |
2688
|
|
|
for ( z = '0.'; ++e; z += '0' ); |
|
|
|
|
2689
|
|
|
str = z + str; |
2690
|
|
|
|
2691
|
|
|
// Positive exponent |
2692
|
|
|
} else { |
2693
|
|
|
len = str.length; |
2694
|
|
|
|
2695
|
|
|
// Append zeros. |
2696
|
|
|
if ( ++e > len ) { |
2697
|
|
|
for ( z = '0', e -= len; --e; z += '0' ); |
|
|
|
|
2698
|
|
|
str += z; |
2699
|
|
|
} else if ( e < len ) { |
2700
|
|
|
str = str.slice( 0, e ) + '.' + str.slice(e); |
2701
|
|
|
} |
2702
|
|
|
} |
2703
|
|
|
|
2704
|
|
|
return str; |
2705
|
|
|
} |
2706
|
|
|
|
2707
|
|
|
|
2708
|
|
|
function truncate(n) { |
2709
|
|
|
n = parseFloat(n); |
2710
|
|
|
return n < 0 ? mathceil(n) : mathfloor(n); |
2711
|
|
|
} |
2712
|
|
|
|
2713
|
|
|
|
2714
|
|
|
// EXPORT |
2715
|
|
|
|
2716
|
|
|
|
2717
|
|
|
BigNumber = constructorFactory(); |
2718
|
|
|
BigNumber['default'] = BigNumber.BigNumber = BigNumber; |
2719
|
|
|
|
2720
|
|
|
|
2721
|
|
|
// AMD. |
2722
|
|
|
if ( typeof define == 'function' && define.amd ) { |
2723
|
|
|
define( function () { return BigNumber; } ); |
2724
|
|
|
|
2725
|
|
|
// Node.js and other environments that support module.exports. |
2726
|
|
|
} else if ( typeof module != 'undefined' && module.exports ) { |
2727
|
|
|
module.exports = BigNumber; |
2728
|
|
|
|
2729
|
|
|
// Browser. |
2730
|
|
|
} else { |
2731
|
|
|
if ( !globalObj ) globalObj = typeof self != 'undefined' ? self : Function('return this')(); |
|
|
|
|
2732
|
|
|
globalObj.BigNumber = BigNumber; |
2733
|
|
|
} |
2734
|
|
|
})(this); |
2735
|
|
|
|