@0xsquid/ui 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -2912,9 +2912,2967 @@ const PasteButton = ({ onClick }) => {
2912
2912
  return (jsxRuntime.jsx(Button, { size: "md", variant: "tertiary", onClick: onClick, className: "!tw-h-[30px] !tw-w-fit tw-min-w-0 !tw-px-2", children: jsxRuntime.jsx(CaptionText, { children: "Paste" }) }));
2913
2913
  };
2914
2914
 
2915
+ /*
2916
+ * bignumber.js v9.1.2
2917
+ * A JavaScript library for arbitrary-precision arithmetic.
2918
+ * https://github.com/MikeMcl/bignumber.js
2919
+ * Copyright (c) 2022 Michael Mclaughlin <M8ch88l@gmail.com>
2920
+ * MIT Licensed.
2921
+ *
2922
+ * BigNumber.prototype methods | BigNumber methods
2923
+ * |
2924
+ * absoluteValue abs | clone
2925
+ * comparedTo | config set
2926
+ * decimalPlaces dp | DECIMAL_PLACES
2927
+ * dividedBy div | ROUNDING_MODE
2928
+ * dividedToIntegerBy idiv | EXPONENTIAL_AT
2929
+ * exponentiatedBy pow | RANGE
2930
+ * integerValue | CRYPTO
2931
+ * isEqualTo eq | MODULO_MODE
2932
+ * isFinite | POW_PRECISION
2933
+ * isGreaterThan gt | FORMAT
2934
+ * isGreaterThanOrEqualTo gte | ALPHABET
2935
+ * isInteger | isBigNumber
2936
+ * isLessThan lt | maximum max
2937
+ * isLessThanOrEqualTo lte | minimum min
2938
+ * isNaN | random
2939
+ * isNegative | sum
2940
+ * isPositive |
2941
+ * isZero |
2942
+ * minus |
2943
+ * modulo mod |
2944
+ * multipliedBy times |
2945
+ * negated |
2946
+ * plus |
2947
+ * precision sd |
2948
+ * shiftedBy |
2949
+ * squareRoot sqrt |
2950
+ * toExponential |
2951
+ * toFixed |
2952
+ * toFormat |
2953
+ * toFraction |
2954
+ * toJSON |
2955
+ * toNumber |
2956
+ * toPrecision |
2957
+ * toString |
2958
+ * valueOf |
2959
+ *
2960
+ */
2961
+
2962
+
2963
+ var
2964
+ isNumeric = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,
2965
+ mathceil = Math.ceil,
2966
+ mathfloor = Math.floor,
2967
+
2968
+ bignumberError = '[BigNumber Error] ',
2969
+ tooManyDigits = bignumberError + 'Number primitive has more than 15 significant digits: ',
2970
+
2971
+ BASE = 1e14,
2972
+ LOG_BASE = 14,
2973
+ MAX_SAFE_INTEGER = 0x1fffffffffffff, // 2^53 - 1
2974
+ // MAX_INT32 = 0x7fffffff, // 2^31 - 1
2975
+ POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13],
2976
+ SQRT_BASE = 1e7,
2977
+
2978
+ // EDITABLE
2979
+ // The limit on the value of DECIMAL_PLACES, TO_EXP_NEG, TO_EXP_POS, MIN_EXP, MAX_EXP, and
2980
+ // the arguments to toExponential, toFixed, toFormat, and toPrecision.
2981
+ MAX = 1E9; // 0 to MAX_INT32
2982
+
2983
+
2984
+ /*
2985
+ * Create and return a BigNumber constructor.
2986
+ */
2987
+ function clone(configObject) {
2988
+ var div, convertBase, parseNumeric,
2989
+ P = BigNumber.prototype = { constructor: BigNumber, toString: null, valueOf: null },
2990
+ ONE = new BigNumber(1),
2991
+
2992
+
2993
+ //----------------------------- EDITABLE CONFIG DEFAULTS -------------------------------
2994
+
2995
+
2996
+ // The default values below must be integers within the inclusive ranges stated.
2997
+ // The values can also be changed at run-time using BigNumber.set.
2998
+
2999
+ // The maximum number of decimal places for operations involving division.
3000
+ DECIMAL_PLACES = 20, // 0 to MAX
3001
+
3002
+ // The rounding mode used when rounding to the above decimal places, and when using
3003
+ // toExponential, toFixed, toFormat and toPrecision, and round (default value).
3004
+ // UP 0 Away from zero.
3005
+ // DOWN 1 Towards zero.
3006
+ // CEIL 2 Towards +Infinity.
3007
+ // FLOOR 3 Towards -Infinity.
3008
+ // HALF_UP 4 Towards nearest neighbour. If equidistant, up.
3009
+ // HALF_DOWN 5 Towards nearest neighbour. If equidistant, down.
3010
+ // HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour.
3011
+ // HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity.
3012
+ // HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.
3013
+ ROUNDING_MODE = 4, // 0 to 8
3014
+
3015
+ // EXPONENTIAL_AT : [TO_EXP_NEG , TO_EXP_POS]
3016
+
3017
+ // The exponent value at and beneath which toString returns exponential notation.
3018
+ // Number type: -7
3019
+ TO_EXP_NEG = -7, // 0 to -MAX
3020
+
3021
+ // The exponent value at and above which toString returns exponential notation.
3022
+ // Number type: 21
3023
+ TO_EXP_POS = 21, // 0 to MAX
3024
+
3025
+ // RANGE : [MIN_EXP, MAX_EXP]
3026
+
3027
+ // The minimum exponent value, beneath which underflow to zero occurs.
3028
+ // Number type: -324 (5e-324)
3029
+ MIN_EXP = -1e7, // -1 to -MAX
3030
+
3031
+ // The maximum exponent value, above which overflow to Infinity occurs.
3032
+ // Number type: 308 (1.7976931348623157e+308)
3033
+ // For MAX_EXP > 1e7, e.g. new BigNumber('1e100000000').plus(1) may be slow.
3034
+ MAX_EXP = 1e7, // 1 to MAX
3035
+
3036
+ // Whether to use cryptographically-secure random number generation, if available.
3037
+ CRYPTO = false, // true or false
3038
+
3039
+ // The modulo mode used when calculating the modulus: a mod n.
3040
+ // The quotient (q = a / n) is calculated according to the corresponding rounding mode.
3041
+ // The remainder (r) is calculated as: r = a - n * q.
3042
+ //
3043
+ // UP 0 The remainder is positive if the dividend is negative, else is negative.
3044
+ // DOWN 1 The remainder has the same sign as the dividend.
3045
+ // This modulo mode is commonly known as 'truncated division' and is
3046
+ // equivalent to (a % n) in JavaScript.
3047
+ // FLOOR 3 The remainder has the same sign as the divisor (Python %).
3048
+ // HALF_EVEN 6 This modulo mode implements the IEEE 754 remainder function.
3049
+ // EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)).
3050
+ // The remainder is always positive.
3051
+ //
3052
+ // The truncated division, floored division, Euclidian division and IEEE 754 remainder
3053
+ // modes are commonly used for the modulus operation.
3054
+ // Although the other rounding modes can also be used, they may not give useful results.
3055
+ MODULO_MODE = 1, // 0 to 9
3056
+
3057
+ // The maximum number of significant digits of the result of the exponentiatedBy operation.
3058
+ // If POW_PRECISION is 0, there will be unlimited significant digits.
3059
+ POW_PRECISION = 0, // 0 to MAX
3060
+
3061
+ // The format specification used by the BigNumber.prototype.toFormat method.
3062
+ FORMAT = {
3063
+ prefix: '',
3064
+ groupSize: 3,
3065
+ secondaryGroupSize: 0,
3066
+ groupSeparator: ',',
3067
+ decimalSeparator: '.',
3068
+ fractionGroupSize: 0,
3069
+ fractionGroupSeparator: '\xA0', // non-breaking space
3070
+ suffix: ''
3071
+ },
3072
+
3073
+ // The alphabet used for base conversion. It must be at least 2 characters long, with no '+',
3074
+ // '-', '.', whitespace, or repeated character.
3075
+ // '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_'
3076
+ ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz',
3077
+ alphabetHasNormalDecimalDigits = true;
3078
+
3079
+
3080
+ //------------------------------------------------------------------------------------------
3081
+
3082
+
3083
+ // CONSTRUCTOR
3084
+
3085
+
3086
+ /*
3087
+ * The BigNumber constructor and exported function.
3088
+ * Create and return a new instance of a BigNumber object.
3089
+ *
3090
+ * v {number|string|BigNumber} A numeric value.
3091
+ * [b] {number} The base of v. Integer, 2 to ALPHABET.length inclusive.
3092
+ */
3093
+ function BigNumber(v, b) {
3094
+ var alphabet, c, caseChanged, e, i, isNum, len, str,
3095
+ x = this;
3096
+
3097
+ // Enable constructor call without `new`.
3098
+ if (!(x instanceof BigNumber)) return new BigNumber(v, b);
3099
+
3100
+ if (b == null) {
3101
+
3102
+ if (v && v._isBigNumber === true) {
3103
+ x.s = v.s;
3104
+
3105
+ if (!v.c || v.e > MAX_EXP) {
3106
+ x.c = x.e = null;
3107
+ } else if (v.e < MIN_EXP) {
3108
+ x.c = [x.e = 0];
3109
+ } else {
3110
+ x.e = v.e;
3111
+ x.c = v.c.slice();
3112
+ }
3113
+
3114
+ return;
3115
+ }
3116
+
3117
+ if ((isNum = typeof v == 'number') && v * 0 == 0) {
3118
+
3119
+ // Use `1 / n` to handle minus zero also.
3120
+ x.s = 1 / v < 0 ? (v = -v, -1) : 1;
3121
+
3122
+ // Fast path for integers, where n < 2147483648 (2**31).
3123
+ if (v === ~~v) {
3124
+ for (e = 0, i = v; i >= 10; i /= 10, e++);
3125
+
3126
+ if (e > MAX_EXP) {
3127
+ x.c = x.e = null;
3128
+ } else {
3129
+ x.e = e;
3130
+ x.c = [v];
3131
+ }
3132
+
3133
+ return;
3134
+ }
3135
+
3136
+ str = String(v);
3137
+ } else {
3138
+
3139
+ if (!isNumeric.test(str = String(v))) return parseNumeric(x, str, isNum);
3140
+
3141
+ x.s = str.charCodeAt(0) == 45 ? (str = str.slice(1), -1) : 1;
3142
+ }
3143
+
3144
+ // Decimal point?
3145
+ if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');
3146
+
3147
+ // Exponential form?
3148
+ if ((i = str.search(/e/i)) > 0) {
3149
+
3150
+ // Determine exponent.
3151
+ if (e < 0) e = i;
3152
+ e += +str.slice(i + 1);
3153
+ str = str.substring(0, i);
3154
+ } else if (e < 0) {
3155
+
3156
+ // Integer.
3157
+ e = str.length;
3158
+ }
3159
+
3160
+ } else {
3161
+
3162
+ // '[BigNumber Error] Base {not a primitive number|not an integer|out of range}: {b}'
3163
+ intCheck(b, 2, ALPHABET.length, 'Base');
3164
+
3165
+ // Allow exponential notation to be used with base 10 argument, while
3166
+ // also rounding to DECIMAL_PLACES as with other bases.
3167
+ if (b == 10 && alphabetHasNormalDecimalDigits) {
3168
+ x = new BigNumber(v);
3169
+ return round(x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE);
3170
+ }
3171
+
3172
+ str = String(v);
3173
+
3174
+ if (isNum = typeof v == 'number') {
3175
+
3176
+ // Avoid potential interpretation of Infinity and NaN as base 44+ values.
3177
+ if (v * 0 != 0) return parseNumeric(x, str, isNum, b);
3178
+
3179
+ x.s = 1 / v < 0 ? (str = str.slice(1), -1) : 1;
3180
+
3181
+ // '[BigNumber Error] Number primitive has more than 15 significant digits: {n}'
3182
+ if (BigNumber.DEBUG && str.replace(/^0\.0*|\./, '').length > 15) {
3183
+ throw Error
3184
+ (tooManyDigits + v);
3185
+ }
3186
+ } else {
3187
+ x.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1;
3188
+ }
3189
+
3190
+ alphabet = ALPHABET.slice(0, b);
3191
+ e = i = 0;
3192
+
3193
+ // Check that str is a valid base b number.
3194
+ // Don't use RegExp, so alphabet can contain special characters.
3195
+ for (len = str.length; i < len; i++) {
3196
+ if (alphabet.indexOf(c = str.charAt(i)) < 0) {
3197
+ if (c == '.') {
3198
+
3199
+ // If '.' is not the first character and it has not be found before.
3200
+ if (i > e) {
3201
+ e = len;
3202
+ continue;
3203
+ }
3204
+ } else if (!caseChanged) {
3205
+
3206
+ // Allow e.g. hexadecimal 'FF' as well as 'ff'.
3207
+ if (str == str.toUpperCase() && (str = str.toLowerCase()) ||
3208
+ str == str.toLowerCase() && (str = str.toUpperCase())) {
3209
+ caseChanged = true;
3210
+ i = -1;
3211
+ e = 0;
3212
+ continue;
3213
+ }
3214
+ }
3215
+
3216
+ return parseNumeric(x, String(v), isNum, b);
3217
+ }
3218
+ }
3219
+
3220
+ // Prevent later check for length on converted number.
3221
+ isNum = false;
3222
+ str = convertBase(str, b, 10, x.s);
3223
+
3224
+ // Decimal point?
3225
+ if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');
3226
+ else e = str.length;
3227
+ }
3228
+
3229
+ // Determine leading zeros.
3230
+ for (i = 0; str.charCodeAt(i) === 48; i++);
3231
+
3232
+ // Determine trailing zeros.
3233
+ for (len = str.length; str.charCodeAt(--len) === 48;);
3234
+
3235
+ if (str = str.slice(i, ++len)) {
3236
+ len -= i;
3237
+
3238
+ // '[BigNumber Error] Number primitive has more than 15 significant digits: {n}'
3239
+ if (isNum && BigNumber.DEBUG &&
3240
+ len > 15 && (v > MAX_SAFE_INTEGER || v !== mathfloor(v))) {
3241
+ throw Error
3242
+ (tooManyDigits + (x.s * v));
3243
+ }
3244
+
3245
+ // Overflow?
3246
+ if ((e = e - i - 1) > MAX_EXP) {
3247
+
3248
+ // Infinity.
3249
+ x.c = x.e = null;
3250
+
3251
+ // Underflow?
3252
+ } else if (e < MIN_EXP) {
3253
+
3254
+ // Zero.
3255
+ x.c = [x.e = 0];
3256
+ } else {
3257
+ x.e = e;
3258
+ x.c = [];
3259
+
3260
+ // Transform base
3261
+
3262
+ // e is the base 10 exponent.
3263
+ // i is where to slice str to get the first element of the coefficient array.
3264
+ i = (e + 1) % LOG_BASE;
3265
+ if (e < 0) i += LOG_BASE; // i < 1
3266
+
3267
+ if (i < len) {
3268
+ if (i) x.c.push(+str.slice(0, i));
3269
+
3270
+ for (len -= LOG_BASE; i < len;) {
3271
+ x.c.push(+str.slice(i, i += LOG_BASE));
3272
+ }
3273
+
3274
+ i = LOG_BASE - (str = str.slice(i)).length;
3275
+ } else {
3276
+ i -= len;
3277
+ }
3278
+
3279
+ for (; i--; str += '0');
3280
+ x.c.push(+str);
3281
+ }
3282
+ } else {
3283
+
3284
+ // Zero.
3285
+ x.c = [x.e = 0];
3286
+ }
3287
+ }
3288
+
3289
+
3290
+ // CONSTRUCTOR PROPERTIES
3291
+
3292
+
3293
+ BigNumber.clone = clone;
3294
+
3295
+ BigNumber.ROUND_UP = 0;
3296
+ BigNumber.ROUND_DOWN = 1;
3297
+ BigNumber.ROUND_CEIL = 2;
3298
+ BigNumber.ROUND_FLOOR = 3;
3299
+ BigNumber.ROUND_HALF_UP = 4;
3300
+ BigNumber.ROUND_HALF_DOWN = 5;
3301
+ BigNumber.ROUND_HALF_EVEN = 6;
3302
+ BigNumber.ROUND_HALF_CEIL = 7;
3303
+ BigNumber.ROUND_HALF_FLOOR = 8;
3304
+ BigNumber.EUCLID = 9;
3305
+
3306
+
3307
+ /*
3308
+ * Configure infrequently-changing library-wide settings.
3309
+ *
3310
+ * Accept an object with the following optional properties (if the value of a property is
3311
+ * a number, it must be an integer within the inclusive range stated):
3312
+ *
3313
+ * DECIMAL_PLACES {number} 0 to MAX
3314
+ * ROUNDING_MODE {number} 0 to 8
3315
+ * EXPONENTIAL_AT {number|number[]} -MAX to MAX or [-MAX to 0, 0 to MAX]
3316
+ * RANGE {number|number[]} -MAX to MAX (not zero) or [-MAX to -1, 1 to MAX]
3317
+ * CRYPTO {boolean} true or false
3318
+ * MODULO_MODE {number} 0 to 9
3319
+ * POW_PRECISION {number} 0 to MAX
3320
+ * ALPHABET {string} A string of two or more unique characters which does
3321
+ * not contain '.'.
3322
+ * FORMAT {object} An object with some of the following properties:
3323
+ * prefix {string}
3324
+ * groupSize {number}
3325
+ * secondaryGroupSize {number}
3326
+ * groupSeparator {string}
3327
+ * decimalSeparator {string}
3328
+ * fractionGroupSize {number}
3329
+ * fractionGroupSeparator {string}
3330
+ * suffix {string}
3331
+ *
3332
+ * (The values assigned to the above FORMAT object properties are not checked for validity.)
3333
+ *
3334
+ * E.g.
3335
+ * BigNumber.config({ DECIMAL_PLACES : 20, ROUNDING_MODE : 4 })
3336
+ *
3337
+ * Ignore properties/parameters set to null or undefined, except for ALPHABET.
3338
+ *
3339
+ * Return an object with the properties current values.
3340
+ */
3341
+ BigNumber.config = BigNumber.set = function (obj) {
3342
+ var p, v;
3343
+
3344
+ if (obj != null) {
3345
+
3346
+ if (typeof obj == 'object') {
3347
+
3348
+ // DECIMAL_PLACES {number} Integer, 0 to MAX inclusive.
3349
+ // '[BigNumber Error] DECIMAL_PLACES {not a primitive number|not an integer|out of range}: {v}'
3350
+ if (obj.hasOwnProperty(p = 'DECIMAL_PLACES')) {
3351
+ v = obj[p];
3352
+ intCheck(v, 0, MAX, p);
3353
+ DECIMAL_PLACES = v;
3354
+ }
3355
+
3356
+ // ROUNDING_MODE {number} Integer, 0 to 8 inclusive.
3357
+ // '[BigNumber Error] ROUNDING_MODE {not a primitive number|not an integer|out of range}: {v}'
3358
+ if (obj.hasOwnProperty(p = 'ROUNDING_MODE')) {
3359
+ v = obj[p];
3360
+ intCheck(v, 0, 8, p);
3361
+ ROUNDING_MODE = v;
3362
+ }
3363
+
3364
+ // EXPONENTIAL_AT {number|number[]}
3365
+ // Integer, -MAX to MAX inclusive or
3366
+ // [integer -MAX to 0 inclusive, 0 to MAX inclusive].
3367
+ // '[BigNumber Error] EXPONENTIAL_AT {not a primitive number|not an integer|out of range}: {v}'
3368
+ if (obj.hasOwnProperty(p = 'EXPONENTIAL_AT')) {
3369
+ v = obj[p];
3370
+ if (v && v.pop) {
3371
+ intCheck(v[0], -MAX, 0, p);
3372
+ intCheck(v[1], 0, MAX, p);
3373
+ TO_EXP_NEG = v[0];
3374
+ TO_EXP_POS = v[1];
3375
+ } else {
3376
+ intCheck(v, -MAX, MAX, p);
3377
+ TO_EXP_NEG = -(TO_EXP_POS = v < 0 ? -v : v);
3378
+ }
3379
+ }
3380
+
3381
+ // RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or
3382
+ // [integer -MAX to -1 inclusive, integer 1 to MAX inclusive].
3383
+ // '[BigNumber Error] RANGE {not a primitive number|not an integer|out of range|cannot be zero}: {v}'
3384
+ if (obj.hasOwnProperty(p = 'RANGE')) {
3385
+ v = obj[p];
3386
+ if (v && v.pop) {
3387
+ intCheck(v[0], -MAX, -1, p);
3388
+ intCheck(v[1], 1, MAX, p);
3389
+ MIN_EXP = v[0];
3390
+ MAX_EXP = v[1];
3391
+ } else {
3392
+ intCheck(v, -MAX, MAX, p);
3393
+ if (v) {
3394
+ MIN_EXP = -(MAX_EXP = v < 0 ? -v : v);
3395
+ } else {
3396
+ throw Error
3397
+ (bignumberError + p + ' cannot be zero: ' + v);
3398
+ }
3399
+ }
3400
+ }
3401
+
3402
+ // CRYPTO {boolean} true or false.
3403
+ // '[BigNumber Error] CRYPTO not true or false: {v}'
3404
+ // '[BigNumber Error] crypto unavailable'
3405
+ if (obj.hasOwnProperty(p = 'CRYPTO')) {
3406
+ v = obj[p];
3407
+ if (v === !!v) {
3408
+ if (v) {
3409
+ if (typeof crypto != 'undefined' && crypto &&
3410
+ (crypto.getRandomValues || crypto.randomBytes)) {
3411
+ CRYPTO = v;
3412
+ } else {
3413
+ CRYPTO = !v;
3414
+ throw Error
3415
+ (bignumberError + 'crypto unavailable');
3416
+ }
3417
+ } else {
3418
+ CRYPTO = v;
3419
+ }
3420
+ } else {
3421
+ throw Error
3422
+ (bignumberError + p + ' not true or false: ' + v);
3423
+ }
3424
+ }
3425
+
3426
+ // MODULO_MODE {number} Integer, 0 to 9 inclusive.
3427
+ // '[BigNumber Error] MODULO_MODE {not a primitive number|not an integer|out of range}: {v}'
3428
+ if (obj.hasOwnProperty(p = 'MODULO_MODE')) {
3429
+ v = obj[p];
3430
+ intCheck(v, 0, 9, p);
3431
+ MODULO_MODE = v;
3432
+ }
3433
+
3434
+ // POW_PRECISION {number} Integer, 0 to MAX inclusive.
3435
+ // '[BigNumber Error] POW_PRECISION {not a primitive number|not an integer|out of range}: {v}'
3436
+ if (obj.hasOwnProperty(p = 'POW_PRECISION')) {
3437
+ v = obj[p];
3438
+ intCheck(v, 0, MAX, p);
3439
+ POW_PRECISION = v;
3440
+ }
3441
+
3442
+ // FORMAT {object}
3443
+ // '[BigNumber Error] FORMAT not an object: {v}'
3444
+ if (obj.hasOwnProperty(p = 'FORMAT')) {
3445
+ v = obj[p];
3446
+ if (typeof v == 'object') FORMAT = v;
3447
+ else throw Error
3448
+ (bignumberError + p + ' not an object: ' + v);
3449
+ }
3450
+
3451
+ // ALPHABET {string}
3452
+ // '[BigNumber Error] ALPHABET invalid: {v}'
3453
+ if (obj.hasOwnProperty(p = 'ALPHABET')) {
3454
+ v = obj[p];
3455
+
3456
+ // Disallow if less than two characters,
3457
+ // or if it contains '+', '-', '.', whitespace, or a repeated character.
3458
+ if (typeof v == 'string' && !/^.?$|[+\-.\s]|(.).*\1/.test(v)) {
3459
+ alphabetHasNormalDecimalDigits = v.slice(0, 10) == '0123456789';
3460
+ ALPHABET = v;
3461
+ } else {
3462
+ throw Error
3463
+ (bignumberError + p + ' invalid: ' + v);
3464
+ }
3465
+ }
3466
+
3467
+ } else {
3468
+
3469
+ // '[BigNumber Error] Object expected: {v}'
3470
+ throw Error
3471
+ (bignumberError + 'Object expected: ' + obj);
3472
+ }
3473
+ }
3474
+
3475
+ return {
3476
+ DECIMAL_PLACES: DECIMAL_PLACES,
3477
+ ROUNDING_MODE: ROUNDING_MODE,
3478
+ EXPONENTIAL_AT: [TO_EXP_NEG, TO_EXP_POS],
3479
+ RANGE: [MIN_EXP, MAX_EXP],
3480
+ CRYPTO: CRYPTO,
3481
+ MODULO_MODE: MODULO_MODE,
3482
+ POW_PRECISION: POW_PRECISION,
3483
+ FORMAT: FORMAT,
3484
+ ALPHABET: ALPHABET
3485
+ };
3486
+ };
3487
+
3488
+
3489
+ /*
3490
+ * Return true if v is a BigNumber instance, otherwise return false.
3491
+ *
3492
+ * If BigNumber.DEBUG is true, throw if a BigNumber instance is not well-formed.
3493
+ *
3494
+ * v {any}
3495
+ *
3496
+ * '[BigNumber Error] Invalid BigNumber: {v}'
3497
+ */
3498
+ BigNumber.isBigNumber = function (v) {
3499
+ if (!v || v._isBigNumber !== true) return false;
3500
+ if (!BigNumber.DEBUG) return true;
3501
+
3502
+ var i, n,
3503
+ c = v.c,
3504
+ e = v.e,
3505
+ s = v.s;
3506
+
3507
+ out: if ({}.toString.call(c) == '[object Array]') {
3508
+
3509
+ if ((s === 1 || s === -1) && e >= -MAX && e <= MAX && e === mathfloor(e)) {
3510
+
3511
+ // If the first element is zero, the BigNumber value must be zero.
3512
+ if (c[0] === 0) {
3513
+ if (e === 0 && c.length === 1) return true;
3514
+ break out;
3515
+ }
3516
+
3517
+ // Calculate number of digits that c[0] should have, based on the exponent.
3518
+ i = (e + 1) % LOG_BASE;
3519
+ if (i < 1) i += LOG_BASE;
3520
+
3521
+ // Calculate number of digits of c[0].
3522
+ //if (Math.ceil(Math.log(c[0] + 1) / Math.LN10) == i) {
3523
+ if (String(c[0]).length == i) {
3524
+
3525
+ for (i = 0; i < c.length; i++) {
3526
+ n = c[i];
3527
+ if (n < 0 || n >= BASE || n !== mathfloor(n)) break out;
3528
+ }
3529
+
3530
+ // Last element cannot be zero, unless it is the only element.
3531
+ if (n !== 0) return true;
3532
+ }
3533
+ }
3534
+
3535
+ // Infinity/NaN
3536
+ } else if (c === null && e === null && (s === null || s === 1 || s === -1)) {
3537
+ return true;
3538
+ }
3539
+
3540
+ throw Error
3541
+ (bignumberError + 'Invalid BigNumber: ' + v);
3542
+ };
3543
+
3544
+
3545
+ /*
3546
+ * Return a new BigNumber whose value is the maximum of the arguments.
3547
+ *
3548
+ * arguments {number|string|BigNumber}
3549
+ */
3550
+ BigNumber.maximum = BigNumber.max = function () {
3551
+ return maxOrMin(arguments, -1);
3552
+ };
3553
+
3554
+
3555
+ /*
3556
+ * Return a new BigNumber whose value is the minimum of the arguments.
3557
+ *
3558
+ * arguments {number|string|BigNumber}
3559
+ */
3560
+ BigNumber.minimum = BigNumber.min = function () {
3561
+ return maxOrMin(arguments, 1);
3562
+ };
3563
+
3564
+
3565
+ /*
3566
+ * Return a new BigNumber with a random value equal to or greater than 0 and less than 1,
3567
+ * and with dp, or DECIMAL_PLACES if dp is omitted, decimal places (or less if trailing
3568
+ * zeros are produced).
3569
+ *
3570
+ * [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
3571
+ *
3572
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp}'
3573
+ * '[BigNumber Error] crypto unavailable'
3574
+ */
3575
+ BigNumber.random = (function () {
3576
+ var pow2_53 = 0x20000000000000;
3577
+
3578
+ // Return a 53 bit integer n, where 0 <= n < 9007199254740992.
3579
+ // Check if Math.random() produces more than 32 bits of randomness.
3580
+ // If it does, assume at least 53 bits are produced, otherwise assume at least 30 bits.
3581
+ // 0x40000000 is 2^30, 0x800000 is 2^23, 0x1fffff is 2^21 - 1.
3582
+ var random53bitInt = (Math.random() * pow2_53) & 0x1fffff
3583
+ ? function () { return mathfloor(Math.random() * pow2_53); }
3584
+ : function () { return ((Math.random() * 0x40000000 | 0) * 0x800000) +
3585
+ (Math.random() * 0x800000 | 0); };
3586
+
3587
+ return function (dp) {
3588
+ var a, b, e, k, v,
3589
+ i = 0,
3590
+ c = [],
3591
+ rand = new BigNumber(ONE);
3592
+
3593
+ if (dp == null) dp = DECIMAL_PLACES;
3594
+ else intCheck(dp, 0, MAX);
3595
+
3596
+ k = mathceil(dp / LOG_BASE);
3597
+
3598
+ if (CRYPTO) {
3599
+
3600
+ // Browsers supporting crypto.getRandomValues.
3601
+ if (crypto.getRandomValues) {
3602
+
3603
+ a = crypto.getRandomValues(new Uint32Array(k *= 2));
3604
+
3605
+ for (; i < k;) {
3606
+
3607
+ // 53 bits:
3608
+ // ((Math.pow(2, 32) - 1) * Math.pow(2, 21)).toString(2)
3609
+ // 11111 11111111 11111111 11111111 11100000 00000000 00000000
3610
+ // ((Math.pow(2, 32) - 1) >>> 11).toString(2)
3611
+ // 11111 11111111 11111111
3612
+ // 0x20000 is 2^21.
3613
+ v = a[i] * 0x20000 + (a[i + 1] >>> 11);
3614
+
3615
+ // Rejection sampling:
3616
+ // 0 <= v < 9007199254740992
3617
+ // Probability that v >= 9e15, is
3618
+ // 7199254740992 / 9007199254740992 ~= 0.0008, i.e. 1 in 1251
3619
+ if (v >= 9e15) {
3620
+ b = crypto.getRandomValues(new Uint32Array(2));
3621
+ a[i] = b[0];
3622
+ a[i + 1] = b[1];
3623
+ } else {
3624
+
3625
+ // 0 <= v <= 8999999999999999
3626
+ // 0 <= (v % 1e14) <= 99999999999999
3627
+ c.push(v % 1e14);
3628
+ i += 2;
3629
+ }
3630
+ }
3631
+ i = k / 2;
3632
+
3633
+ // Node.js supporting crypto.randomBytes.
3634
+ } else if (crypto.randomBytes) {
3635
+
3636
+ // buffer
3637
+ a = crypto.randomBytes(k *= 7);
3638
+
3639
+ for (; i < k;) {
3640
+
3641
+ // 0x1000000000000 is 2^48, 0x10000000000 is 2^40
3642
+ // 0x100000000 is 2^32, 0x1000000 is 2^24
3643
+ // 11111 11111111 11111111 11111111 11111111 11111111 11111111
3644
+ // 0 <= v < 9007199254740992
3645
+ v = ((a[i] & 31) * 0x1000000000000) + (a[i + 1] * 0x10000000000) +
3646
+ (a[i + 2] * 0x100000000) + (a[i + 3] * 0x1000000) +
3647
+ (a[i + 4] << 16) + (a[i + 5] << 8) + a[i + 6];
3648
+
3649
+ if (v >= 9e15) {
3650
+ crypto.randomBytes(7).copy(a, i);
3651
+ } else {
3652
+
3653
+ // 0 <= (v % 1e14) <= 99999999999999
3654
+ c.push(v % 1e14);
3655
+ i += 7;
3656
+ }
3657
+ }
3658
+ i = k / 7;
3659
+ } else {
3660
+ CRYPTO = false;
3661
+ throw Error
3662
+ (bignumberError + 'crypto unavailable');
3663
+ }
3664
+ }
3665
+
3666
+ // Use Math.random.
3667
+ if (!CRYPTO) {
3668
+
3669
+ for (; i < k;) {
3670
+ v = random53bitInt();
3671
+ if (v < 9e15) c[i++] = v % 1e14;
3672
+ }
3673
+ }
3674
+
3675
+ k = c[--i];
3676
+ dp %= LOG_BASE;
3677
+
3678
+ // Convert trailing digits to zeros according to dp.
3679
+ if (k && dp) {
3680
+ v = POWS_TEN[LOG_BASE - dp];
3681
+ c[i] = mathfloor(k / v) * v;
3682
+ }
3683
+
3684
+ // Remove trailing elements which are zero.
3685
+ for (; c[i] === 0; c.pop(), i--);
3686
+
3687
+ // Zero?
3688
+ if (i < 0) {
3689
+ c = [e = 0];
3690
+ } else {
3691
+
3692
+ // Remove leading elements which are zero and adjust exponent accordingly.
3693
+ for (e = -1 ; c[0] === 0; c.splice(0, 1), e -= LOG_BASE);
3694
+
3695
+ // Count the digits of the first element of c to determine leading zeros, and...
3696
+ for (i = 1, v = c[0]; v >= 10; v /= 10, i++);
3697
+
3698
+ // adjust the exponent accordingly.
3699
+ if (i < LOG_BASE) e -= LOG_BASE - i;
3700
+ }
3701
+
3702
+ rand.e = e;
3703
+ rand.c = c;
3704
+ return rand;
3705
+ };
3706
+ })();
3707
+
3708
+
3709
+ /*
3710
+ * Return a BigNumber whose value is the sum of the arguments.
3711
+ *
3712
+ * arguments {number|string|BigNumber}
3713
+ */
3714
+ BigNumber.sum = function () {
3715
+ var i = 1,
3716
+ args = arguments,
3717
+ sum = new BigNumber(args[0]);
3718
+ for (; i < args.length;) sum = sum.plus(args[i++]);
3719
+ return sum;
3720
+ };
3721
+
3722
+
3723
+ // PRIVATE FUNCTIONS
3724
+
3725
+
3726
+ // Called by BigNumber and BigNumber.prototype.toString.
3727
+ convertBase = (function () {
3728
+ var decimal = '0123456789';
3729
+
3730
+ /*
3731
+ * Convert string of baseIn to an array of numbers of baseOut.
3732
+ * Eg. toBaseOut('255', 10, 16) returns [15, 15].
3733
+ * Eg. toBaseOut('ff', 16, 10) returns [2, 5, 5].
3734
+ */
3735
+ function toBaseOut(str, baseIn, baseOut, alphabet) {
3736
+ var j,
3737
+ arr = [0],
3738
+ arrL,
3739
+ i = 0,
3740
+ len = str.length;
3741
+
3742
+ for (; i < len;) {
3743
+ for (arrL = arr.length; arrL--; arr[arrL] *= baseIn);
3744
+
3745
+ arr[0] += alphabet.indexOf(str.charAt(i++));
3746
+
3747
+ for (j = 0; j < arr.length; j++) {
3748
+
3749
+ if (arr[j] > baseOut - 1) {
3750
+ if (arr[j + 1] == null) arr[j + 1] = 0;
3751
+ arr[j + 1] += arr[j] / baseOut | 0;
3752
+ arr[j] %= baseOut;
3753
+ }
3754
+ }
3755
+ }
3756
+
3757
+ return arr.reverse();
3758
+ }
3759
+
3760
+ // Convert a numeric string of baseIn to a numeric string of baseOut.
3761
+ // If the caller is toString, we are converting from base 10 to baseOut.
3762
+ // If the caller is BigNumber, we are converting from baseIn to base 10.
3763
+ return function (str, baseIn, baseOut, sign, callerIsToString) {
3764
+ var alphabet, d, e, k, r, x, xc, y,
3765
+ i = str.indexOf('.'),
3766
+ dp = DECIMAL_PLACES,
3767
+ rm = ROUNDING_MODE;
3768
+
3769
+ // Non-integer.
3770
+ if (i >= 0) {
3771
+ k = POW_PRECISION;
3772
+
3773
+ // Unlimited precision.
3774
+ POW_PRECISION = 0;
3775
+ str = str.replace('.', '');
3776
+ y = new BigNumber(baseIn);
3777
+ x = y.pow(str.length - i);
3778
+ POW_PRECISION = k;
3779
+
3780
+ // Convert str as if an integer, then restore the fraction part by dividing the
3781
+ // result by its base raised to a power.
3782
+
3783
+ y.c = toBaseOut(toFixedPoint(coeffToString(x.c), x.e, '0'),
3784
+ 10, baseOut, decimal);
3785
+ y.e = y.c.length;
3786
+ }
3787
+
3788
+ // Convert the number as integer.
3789
+
3790
+ xc = toBaseOut(str, baseIn, baseOut, callerIsToString
3791
+ ? (alphabet = ALPHABET, decimal)
3792
+ : (alphabet = decimal, ALPHABET));
3793
+
3794
+ // xc now represents str as an integer and converted to baseOut. e is the exponent.
3795
+ e = k = xc.length;
3796
+
3797
+ // Remove trailing zeros.
3798
+ for (; xc[--k] == 0; xc.pop());
3799
+
3800
+ // Zero?
3801
+ if (!xc[0]) return alphabet.charAt(0);
3802
+
3803
+ // Does str represent an integer? If so, no need for the division.
3804
+ if (i < 0) {
3805
+ --e;
3806
+ } else {
3807
+ x.c = xc;
3808
+ x.e = e;
3809
+
3810
+ // The sign is needed for correct rounding.
3811
+ x.s = sign;
3812
+ x = div(x, y, dp, rm, baseOut);
3813
+ xc = x.c;
3814
+ r = x.r;
3815
+ e = x.e;
3816
+ }
3817
+
3818
+ // xc now represents str converted to baseOut.
3819
+
3820
+ // THe index of the rounding digit.
3821
+ d = e + dp + 1;
3822
+
3823
+ // The rounding digit: the digit to the right of the digit that may be rounded up.
3824
+ i = xc[d];
3825
+
3826
+ // Look at the rounding digits and mode to determine whether to round up.
3827
+
3828
+ k = baseOut / 2;
3829
+ r = r || d < 0 || xc[d + 1] != null;
3830
+
3831
+ r = rm < 4 ? (i != null || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2))
3832
+ : i > k || i == k &&(rm == 4 || r || rm == 6 && xc[d - 1] & 1 ||
3833
+ rm == (x.s < 0 ? 8 : 7));
3834
+
3835
+ // If the index of the rounding digit is not greater than zero, or xc represents
3836
+ // zero, then the result of the base conversion is zero or, if rounding up, a value
3837
+ // such as 0.00001.
3838
+ if (d < 1 || !xc[0]) {
3839
+
3840
+ // 1^-dp or 0
3841
+ str = r ? toFixedPoint(alphabet.charAt(1), -dp, alphabet.charAt(0)) : alphabet.charAt(0);
3842
+ } else {
3843
+
3844
+ // Truncate xc to the required number of decimal places.
3845
+ xc.length = d;
3846
+
3847
+ // Round up?
3848
+ if (r) {
3849
+
3850
+ // Rounding up may mean the previous digit has to be rounded up and so on.
3851
+ for (--baseOut; ++xc[--d] > baseOut;) {
3852
+ xc[d] = 0;
3853
+
3854
+ if (!d) {
3855
+ ++e;
3856
+ xc = [1].concat(xc);
3857
+ }
3858
+ }
3859
+ }
3860
+
3861
+ // Determine trailing zeros.
3862
+ for (k = xc.length; !xc[--k];);
3863
+
3864
+ // E.g. [4, 11, 15] becomes 4bf.
3865
+ for (i = 0, str = ''; i <= k; str += alphabet.charAt(xc[i++]));
3866
+
3867
+ // Add leading zeros, decimal point and trailing zeros as required.
3868
+ str = toFixedPoint(str, e, alphabet.charAt(0));
3869
+ }
3870
+
3871
+ // The caller will add the sign.
3872
+ return str;
3873
+ };
3874
+ })();
3875
+
3876
+
3877
+ // Perform division in the specified base. Called by div and convertBase.
3878
+ div = (function () {
3879
+
3880
+ // Assume non-zero x and k.
3881
+ function multiply(x, k, base) {
3882
+ var m, temp, xlo, xhi,
3883
+ carry = 0,
3884
+ i = x.length,
3885
+ klo = k % SQRT_BASE,
3886
+ khi = k / SQRT_BASE | 0;
3887
+
3888
+ for (x = x.slice(); i--;) {
3889
+ xlo = x[i] % SQRT_BASE;
3890
+ xhi = x[i] / SQRT_BASE | 0;
3891
+ m = khi * xlo + xhi * klo;
3892
+ temp = klo * xlo + ((m % SQRT_BASE) * SQRT_BASE) + carry;
3893
+ carry = (temp / base | 0) + (m / SQRT_BASE | 0) + khi * xhi;
3894
+ x[i] = temp % base;
3895
+ }
3896
+
3897
+ if (carry) x = [carry].concat(x);
3898
+
3899
+ return x;
3900
+ }
3901
+
3902
+ function compare(a, b, aL, bL) {
3903
+ var i, cmp;
3904
+
3905
+ if (aL != bL) {
3906
+ cmp = aL > bL ? 1 : -1;
3907
+ } else {
3908
+
3909
+ for (i = cmp = 0; i < aL; i++) {
3910
+
3911
+ if (a[i] != b[i]) {
3912
+ cmp = a[i] > b[i] ? 1 : -1;
3913
+ break;
3914
+ }
3915
+ }
3916
+ }
3917
+
3918
+ return cmp;
3919
+ }
3920
+
3921
+ function subtract(a, b, aL, base) {
3922
+ var i = 0;
3923
+
3924
+ // Subtract b from a.
3925
+ for (; aL--;) {
3926
+ a[aL] -= i;
3927
+ i = a[aL] < b[aL] ? 1 : 0;
3928
+ a[aL] = i * base + a[aL] - b[aL];
3929
+ }
3930
+
3931
+ // Remove leading zeros.
3932
+ for (; !a[0] && a.length > 1; a.splice(0, 1));
3933
+ }
3934
+
3935
+ // x: dividend, y: divisor.
3936
+ return function (x, y, dp, rm, base) {
3937
+ var cmp, e, i, more, n, prod, prodL, q, qc, rem, remL, rem0, xi, xL, yc0,
3938
+ yL, yz,
3939
+ s = x.s == y.s ? 1 : -1,
3940
+ xc = x.c,
3941
+ yc = y.c;
3942
+
3943
+ // Either NaN, Infinity or 0?
3944
+ if (!xc || !xc[0] || !yc || !yc[0]) {
3945
+
3946
+ return new BigNumber(
3947
+
3948
+ // Return NaN if either NaN, or both Infinity or 0.
3949
+ !x.s || !y.s || (xc ? yc && xc[0] == yc[0] : !yc) ? NaN :
3950
+
3951
+ // Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0.
3952
+ xc && xc[0] == 0 || !yc ? s * 0 : s / 0
3953
+ );
3954
+ }
3955
+
3956
+ q = new BigNumber(s);
3957
+ qc = q.c = [];
3958
+ e = x.e - y.e;
3959
+ s = dp + e + 1;
3960
+
3961
+ if (!base) {
3962
+ base = BASE;
3963
+ e = bitFloor(x.e / LOG_BASE) - bitFloor(y.e / LOG_BASE);
3964
+ s = s / LOG_BASE | 0;
3965
+ }
3966
+
3967
+ // Result exponent may be one less then the current value of e.
3968
+ // The coefficients of the BigNumbers from convertBase may have trailing zeros.
3969
+ for (i = 0; yc[i] == (xc[i] || 0); i++);
3970
+
3971
+ if (yc[i] > (xc[i] || 0)) e--;
3972
+
3973
+ if (s < 0) {
3974
+ qc.push(1);
3975
+ more = true;
3976
+ } else {
3977
+ xL = xc.length;
3978
+ yL = yc.length;
3979
+ i = 0;
3980
+ s += 2;
3981
+
3982
+ // Normalise xc and yc so highest order digit of yc is >= base / 2.
3983
+
3984
+ n = mathfloor(base / (yc[0] + 1));
3985
+
3986
+ // Not necessary, but to handle odd bases where yc[0] == (base / 2) - 1.
3987
+ // if (n > 1 || n++ == 1 && yc[0] < base / 2) {
3988
+ if (n > 1) {
3989
+ yc = multiply(yc, n, base);
3990
+ xc = multiply(xc, n, base);
3991
+ yL = yc.length;
3992
+ xL = xc.length;
3993
+ }
3994
+
3995
+ xi = yL;
3996
+ rem = xc.slice(0, yL);
3997
+ remL = rem.length;
3998
+
3999
+ // Add zeros to make remainder as long as divisor.
4000
+ for (; remL < yL; rem[remL++] = 0);
4001
+ yz = yc.slice();
4002
+ yz = [0].concat(yz);
4003
+ yc0 = yc[0];
4004
+ if (yc[1] >= base / 2) yc0++;
4005
+ // Not necessary, but to prevent trial digit n > base, when using base 3.
4006
+ // else if (base == 3 && yc0 == 1) yc0 = 1 + 1e-15;
4007
+
4008
+ do {
4009
+ n = 0;
4010
+
4011
+ // Compare divisor and remainder.
4012
+ cmp = compare(yc, rem, yL, remL);
4013
+
4014
+ // If divisor < remainder.
4015
+ if (cmp < 0) {
4016
+
4017
+ // Calculate trial digit, n.
4018
+
4019
+ rem0 = rem[0];
4020
+ if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
4021
+
4022
+ // n is how many times the divisor goes into the current remainder.
4023
+ n = mathfloor(rem0 / yc0);
4024
+
4025
+ // Algorithm:
4026
+ // product = divisor multiplied by trial digit (n).
4027
+ // Compare product and remainder.
4028
+ // If product is greater than remainder:
4029
+ // Subtract divisor from product, decrement trial digit.
4030
+ // Subtract product from remainder.
4031
+ // If product was less than remainder at the last compare:
4032
+ // Compare new remainder and divisor.
4033
+ // If remainder is greater than divisor:
4034
+ // Subtract divisor from remainder, increment trial digit.
4035
+
4036
+ if (n > 1) {
4037
+
4038
+ // n may be > base only when base is 3.
4039
+ if (n >= base) n = base - 1;
4040
+
4041
+ // product = divisor * trial digit.
4042
+ prod = multiply(yc, n, base);
4043
+ prodL = prod.length;
4044
+ remL = rem.length;
4045
+
4046
+ // Compare product and remainder.
4047
+ // If product > remainder then trial digit n too high.
4048
+ // n is 1 too high about 5% of the time, and is not known to have
4049
+ // ever been more than 1 too high.
4050
+ while (compare(prod, rem, prodL, remL) == 1) {
4051
+ n--;
4052
+
4053
+ // Subtract divisor from product.
4054
+ subtract(prod, yL < prodL ? yz : yc, prodL, base);
4055
+ prodL = prod.length;
4056
+ cmp = 1;
4057
+ }
4058
+ } else {
4059
+
4060
+ // n is 0 or 1, cmp is -1.
4061
+ // If n is 0, there is no need to compare yc and rem again below,
4062
+ // so change cmp to 1 to avoid it.
4063
+ // If n is 1, leave cmp as -1, so yc and rem are compared again.
4064
+ if (n == 0) {
4065
+
4066
+ // divisor < remainder, so n must be at least 1.
4067
+ cmp = n = 1;
4068
+ }
4069
+
4070
+ // product = divisor
4071
+ prod = yc.slice();
4072
+ prodL = prod.length;
4073
+ }
4074
+
4075
+ if (prodL < remL) prod = [0].concat(prod);
4076
+
4077
+ // Subtract product from remainder.
4078
+ subtract(rem, prod, remL, base);
4079
+ remL = rem.length;
4080
+
4081
+ // If product was < remainder.
4082
+ if (cmp == -1) {
4083
+
4084
+ // Compare divisor and new remainder.
4085
+ // If divisor < new remainder, subtract divisor from remainder.
4086
+ // Trial digit n too low.
4087
+ // n is 1 too low about 5% of the time, and very rarely 2 too low.
4088
+ while (compare(yc, rem, yL, remL) < 1) {
4089
+ n++;
4090
+
4091
+ // Subtract divisor from remainder.
4092
+ subtract(rem, yL < remL ? yz : yc, remL, base);
4093
+ remL = rem.length;
4094
+ }
4095
+ }
4096
+ } else if (cmp === 0) {
4097
+ n++;
4098
+ rem = [0];
4099
+ } // else cmp === 1 and n will be 0
4100
+
4101
+ // Add the next digit, n, to the result array.
4102
+ qc[i++] = n;
4103
+
4104
+ // Update the remainder.
4105
+ if (rem[0]) {
4106
+ rem[remL++] = xc[xi] || 0;
4107
+ } else {
4108
+ rem = [xc[xi]];
4109
+ remL = 1;
4110
+ }
4111
+ } while ((xi++ < xL || rem[0] != null) && s--);
4112
+
4113
+ more = rem[0] != null;
4114
+
4115
+ // Leading zero?
4116
+ if (!qc[0]) qc.splice(0, 1);
4117
+ }
4118
+
4119
+ if (base == BASE) {
4120
+
4121
+ // To calculate q.e, first get the number of digits of qc[0].
4122
+ for (i = 1, s = qc[0]; s >= 10; s /= 10, i++);
4123
+
4124
+ round(q, dp + (q.e = i + e * LOG_BASE - 1) + 1, rm, more);
4125
+
4126
+ // Caller is convertBase.
4127
+ } else {
4128
+ q.e = e;
4129
+ q.r = +more;
4130
+ }
4131
+
4132
+ return q;
4133
+ };
4134
+ })();
4135
+
4136
+
4137
+ /*
4138
+ * Return a string representing the value of BigNumber n in fixed-point or exponential
4139
+ * notation rounded to the specified decimal places or significant digits.
4140
+ *
4141
+ * n: a BigNumber.
4142
+ * i: the index of the last digit required (i.e. the digit that may be rounded up).
4143
+ * rm: the rounding mode.
4144
+ * id: 1 (toExponential) or 2 (toPrecision).
4145
+ */
4146
+ function format(n, i, rm, id) {
4147
+ var c0, e, ne, len, str;
4148
+
4149
+ if (rm == null) rm = ROUNDING_MODE;
4150
+ else intCheck(rm, 0, 8);
4151
+
4152
+ if (!n.c) return n.toString();
4153
+
4154
+ c0 = n.c[0];
4155
+ ne = n.e;
4156
+
4157
+ if (i == null) {
4158
+ str = coeffToString(n.c);
4159
+ str = id == 1 || id == 2 && (ne <= TO_EXP_NEG || ne >= TO_EXP_POS)
4160
+ ? toExponential(str, ne)
4161
+ : toFixedPoint(str, ne, '0');
4162
+ } else {
4163
+ n = round(new BigNumber(n), i, rm);
4164
+
4165
+ // n.e may have changed if the value was rounded up.
4166
+ e = n.e;
4167
+
4168
+ str = coeffToString(n.c);
4169
+ len = str.length;
4170
+
4171
+ // toPrecision returns exponential notation if the number of significant digits
4172
+ // specified is less than the number of digits necessary to represent the integer
4173
+ // part of the value in fixed-point notation.
4174
+
4175
+ // Exponential notation.
4176
+ if (id == 1 || id == 2 && (i <= e || e <= TO_EXP_NEG)) {
4177
+
4178
+ // Append zeros?
4179
+ for (; len < i; str += '0', len++);
4180
+ str = toExponential(str, e);
4181
+
4182
+ // Fixed-point notation.
4183
+ } else {
4184
+ i -= ne;
4185
+ str = toFixedPoint(str, e, '0');
4186
+
4187
+ // Append zeros?
4188
+ if (e + 1 > len) {
4189
+ if (--i > 0) for (str += '.'; i--; str += '0');
4190
+ } else {
4191
+ i += e - len;
4192
+ if (i > 0) {
4193
+ if (e + 1 == len) str += '.';
4194
+ for (; i--; str += '0');
4195
+ }
4196
+ }
4197
+ }
4198
+ }
4199
+
4200
+ return n.s < 0 && c0 ? '-' + str : str;
4201
+ }
4202
+
4203
+
4204
+ // Handle BigNumber.max and BigNumber.min.
4205
+ // If any number is NaN, return NaN.
4206
+ function maxOrMin(args, n) {
4207
+ var k, y,
4208
+ i = 1,
4209
+ x = new BigNumber(args[0]);
4210
+
4211
+ for (; i < args.length; i++) {
4212
+ y = new BigNumber(args[i]);
4213
+ if (!y.s || (k = compare(x, y)) === n || k === 0 && x.s === n) {
4214
+ x = y;
4215
+ }
4216
+ }
4217
+
4218
+ return x;
4219
+ }
4220
+
4221
+
4222
+ /*
4223
+ * Strip trailing zeros, calculate base 10 exponent and check against MIN_EXP and MAX_EXP.
4224
+ * Called by minus, plus and times.
4225
+ */
4226
+ function normalise(n, c, e) {
4227
+ var i = 1,
4228
+ j = c.length;
4229
+
4230
+ // Remove trailing zeros.
4231
+ for (; !c[--j]; c.pop());
4232
+
4233
+ // Calculate the base 10 exponent. First get the number of digits of c[0].
4234
+ for (j = c[0]; j >= 10; j /= 10, i++);
4235
+
4236
+ // Overflow?
4237
+ if ((e = i + e * LOG_BASE - 1) > MAX_EXP) {
4238
+
4239
+ // Infinity.
4240
+ n.c = n.e = null;
4241
+
4242
+ // Underflow?
4243
+ } else if (e < MIN_EXP) {
4244
+
4245
+ // Zero.
4246
+ n.c = [n.e = 0];
4247
+ } else {
4248
+ n.e = e;
4249
+ n.c = c;
4250
+ }
4251
+
4252
+ return n;
4253
+ }
4254
+
4255
+
4256
+ // Handle values that fail the validity test in BigNumber.
4257
+ parseNumeric = (function () {
4258
+ var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i,
4259
+ dotAfter = /^([^.]+)\.$/,
4260
+ dotBefore = /^\.([^.]+)$/,
4261
+ isInfinityOrNaN = /^-?(Infinity|NaN)$/,
4262
+ whitespaceOrPlus = /^\s*\+(?=[\w.])|^\s+|\s+$/g;
4263
+
4264
+ return function (x, str, isNum, b) {
4265
+ var base,
4266
+ s = isNum ? str : str.replace(whitespaceOrPlus, '');
4267
+
4268
+ // No exception on ±Infinity or NaN.
4269
+ if (isInfinityOrNaN.test(s)) {
4270
+ x.s = isNaN(s) ? null : s < 0 ? -1 : 1;
4271
+ } else {
4272
+ if (!isNum) {
4273
+
4274
+ // basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i
4275
+ s = s.replace(basePrefix, function (m, p1, p2) {
4276
+ base = (p2 = p2.toLowerCase()) == 'x' ? 16 : p2 == 'b' ? 2 : 8;
4277
+ return !b || b == base ? p1 : m;
4278
+ });
4279
+
4280
+ if (b) {
4281
+ base = b;
4282
+
4283
+ // E.g. '1.' to '1', '.1' to '0.1'
4284
+ s = s.replace(dotAfter, '$1').replace(dotBefore, '0.$1');
4285
+ }
4286
+
4287
+ if (str != s) return new BigNumber(s, base);
4288
+ }
4289
+
4290
+ // '[BigNumber Error] Not a number: {n}'
4291
+ // '[BigNumber Error] Not a base {b} number: {n}'
4292
+ if (BigNumber.DEBUG) {
4293
+ throw Error
4294
+ (bignumberError + 'Not a' + (b ? ' base ' + b : '') + ' number: ' + str);
4295
+ }
4296
+
4297
+ // NaN
4298
+ x.s = null;
4299
+ }
4300
+
4301
+ x.c = x.e = null;
4302
+ }
4303
+ })();
4304
+
4305
+
4306
+ /*
4307
+ * Round x to sd significant digits using rounding mode rm. Check for over/under-flow.
4308
+ * If r is truthy, it is known that there are more digits after the rounding digit.
4309
+ */
4310
+ function round(x, sd, rm, r) {
4311
+ var d, i, j, k, n, ni, rd,
4312
+ xc = x.c,
4313
+ pows10 = POWS_TEN;
4314
+
4315
+ // if x is not Infinity or NaN...
4316
+ if (xc) {
4317
+
4318
+ // rd is the rounding digit, i.e. the digit after the digit that may be rounded up.
4319
+ // n is a base 1e14 number, the value of the element of array x.c containing rd.
4320
+ // ni is the index of n within x.c.
4321
+ // d is the number of digits of n.
4322
+ // i is the index of rd within n including leading zeros.
4323
+ // j is the actual index of rd within n (if < 0, rd is a leading zero).
4324
+ out: {
4325
+
4326
+ // Get the number of digits of the first element of xc.
4327
+ for (d = 1, k = xc[0]; k >= 10; k /= 10, d++);
4328
+ i = sd - d;
4329
+
4330
+ // If the rounding digit is in the first element of xc...
4331
+ if (i < 0) {
4332
+ i += LOG_BASE;
4333
+ j = sd;
4334
+ n = xc[ni = 0];
4335
+
4336
+ // Get the rounding digit at index j of n.
4337
+ rd = mathfloor(n / pows10[d - j - 1] % 10);
4338
+ } else {
4339
+ ni = mathceil((i + 1) / LOG_BASE);
4340
+
4341
+ if (ni >= xc.length) {
4342
+
4343
+ if (r) {
4344
+
4345
+ // Needed by sqrt.
4346
+ for (; xc.length <= ni; xc.push(0));
4347
+ n = rd = 0;
4348
+ d = 1;
4349
+ i %= LOG_BASE;
4350
+ j = i - LOG_BASE + 1;
4351
+ } else {
4352
+ break out;
4353
+ }
4354
+ } else {
4355
+ n = k = xc[ni];
4356
+
4357
+ // Get the number of digits of n.
4358
+ for (d = 1; k >= 10; k /= 10, d++);
4359
+
4360
+ // Get the index of rd within n.
4361
+ i %= LOG_BASE;
4362
+
4363
+ // Get the index of rd within n, adjusted for leading zeros.
4364
+ // The number of leading zeros of n is given by LOG_BASE - d.
4365
+ j = i - LOG_BASE + d;
4366
+
4367
+ // Get the rounding digit at index j of n.
4368
+ rd = j < 0 ? 0 : mathfloor(n / pows10[d - j - 1] % 10);
4369
+ }
4370
+ }
4371
+
4372
+ r = r || sd < 0 ||
4373
+
4374
+ // Are there any non-zero digits after the rounding digit?
4375
+ // The expression n % pows10[d - j - 1] returns all digits of n to the right
4376
+ // of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714.
4377
+ xc[ni + 1] != null || (j < 0 ? n : n % pows10[d - j - 1]);
4378
+
4379
+ r = rm < 4
4380
+ ? (rd || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2))
4381
+ : rd > 5 || rd == 5 && (rm == 4 || r || rm == 6 &&
4382
+
4383
+ // Check whether the digit to the left of the rounding digit is odd.
4384
+ ((i > 0 ? j > 0 ? n / pows10[d - j] : 0 : xc[ni - 1]) % 10) & 1 ||
4385
+ rm == (x.s < 0 ? 8 : 7));
4386
+
4387
+ if (sd < 1 || !xc[0]) {
4388
+ xc.length = 0;
4389
+
4390
+ if (r) {
4391
+
4392
+ // Convert sd to decimal places.
4393
+ sd -= x.e + 1;
4394
+
4395
+ // 1, 0.1, 0.01, 0.001, 0.0001 etc.
4396
+ xc[0] = pows10[(LOG_BASE - sd % LOG_BASE) % LOG_BASE];
4397
+ x.e = -sd || 0;
4398
+ } else {
4399
+
4400
+ // Zero.
4401
+ xc[0] = x.e = 0;
4402
+ }
4403
+
4404
+ return x;
4405
+ }
4406
+
4407
+ // Remove excess digits.
4408
+ if (i == 0) {
4409
+ xc.length = ni;
4410
+ k = 1;
4411
+ ni--;
4412
+ } else {
4413
+ xc.length = ni + 1;
4414
+ k = pows10[LOG_BASE - i];
4415
+
4416
+ // E.g. 56700 becomes 56000 if 7 is the rounding digit.
4417
+ // j > 0 means i > number of leading zeros of n.
4418
+ xc[ni] = j > 0 ? mathfloor(n / pows10[d - j] % pows10[j]) * k : 0;
4419
+ }
4420
+
4421
+ // Round up?
4422
+ if (r) {
4423
+
4424
+ for (; ;) {
4425
+
4426
+ // If the digit to be rounded up is in the first element of xc...
4427
+ if (ni == 0) {
4428
+
4429
+ // i will be the length of xc[0] before k is added.
4430
+ for (i = 1, j = xc[0]; j >= 10; j /= 10, i++);
4431
+ j = xc[0] += k;
4432
+ for (k = 1; j >= 10; j /= 10, k++);
4433
+
4434
+ // if i != k the length has increased.
4435
+ if (i != k) {
4436
+ x.e++;
4437
+ if (xc[0] == BASE) xc[0] = 1;
4438
+ }
4439
+
4440
+ break;
4441
+ } else {
4442
+ xc[ni] += k;
4443
+ if (xc[ni] != BASE) break;
4444
+ xc[ni--] = 0;
4445
+ k = 1;
4446
+ }
4447
+ }
4448
+ }
4449
+
4450
+ // Remove trailing zeros.
4451
+ for (i = xc.length; xc[--i] === 0; xc.pop());
4452
+ }
4453
+
4454
+ // Overflow? Infinity.
4455
+ if (x.e > MAX_EXP) {
4456
+ x.c = x.e = null;
4457
+
4458
+ // Underflow? Zero.
4459
+ } else if (x.e < MIN_EXP) {
4460
+ x.c = [x.e = 0];
4461
+ }
4462
+ }
4463
+
4464
+ return x;
4465
+ }
4466
+
4467
+
4468
+ function valueOf(n) {
4469
+ var str,
4470
+ e = n.e;
4471
+
4472
+ if (e === null) return n.toString();
4473
+
4474
+ str = coeffToString(n.c);
4475
+
4476
+ str = e <= TO_EXP_NEG || e >= TO_EXP_POS
4477
+ ? toExponential(str, e)
4478
+ : toFixedPoint(str, e, '0');
4479
+
4480
+ return n.s < 0 ? '-' + str : str;
4481
+ }
4482
+
4483
+
4484
+ // PROTOTYPE/INSTANCE METHODS
4485
+
4486
+
4487
+ /*
4488
+ * Return a new BigNumber whose value is the absolute value of this BigNumber.
4489
+ */
4490
+ P.absoluteValue = P.abs = function () {
4491
+ var x = new BigNumber(this);
4492
+ if (x.s < 0) x.s = 1;
4493
+ return x;
4494
+ };
4495
+
4496
+
4497
+ /*
4498
+ * Return
4499
+ * 1 if the value of this BigNumber is greater than the value of BigNumber(y, b),
4500
+ * -1 if the value of this BigNumber is less than the value of BigNumber(y, b),
4501
+ * 0 if they have the same value,
4502
+ * or null if the value of either is NaN.
4503
+ */
4504
+ P.comparedTo = function (y, b) {
4505
+ return compare(this, new BigNumber(y, b));
4506
+ };
4507
+
4508
+
4509
+ /*
4510
+ * If dp is undefined or null or true or false, return the number of decimal places of the
4511
+ * value of this BigNumber, or null if the value of this BigNumber is ±Infinity or NaN.
4512
+ *
4513
+ * Otherwise, if dp is a number, return a new BigNumber whose value is the value of this
4514
+ * BigNumber rounded to a maximum of dp decimal places using rounding mode rm, or
4515
+ * ROUNDING_MODE if rm is omitted.
4516
+ *
4517
+ * [dp] {number} Decimal places: integer, 0 to MAX inclusive.
4518
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
4519
+ *
4520
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
4521
+ */
4522
+ P.decimalPlaces = P.dp = function (dp, rm) {
4523
+ var c, n, v,
4524
+ x = this;
4525
+
4526
+ if (dp != null) {
4527
+ intCheck(dp, 0, MAX);
4528
+ if (rm == null) rm = ROUNDING_MODE;
4529
+ else intCheck(rm, 0, 8);
4530
+
4531
+ return round(new BigNumber(x), dp + x.e + 1, rm);
4532
+ }
4533
+
4534
+ if (!(c = x.c)) return null;
4535
+ n = ((v = c.length - 1) - bitFloor(this.e / LOG_BASE)) * LOG_BASE;
4536
+
4537
+ // Subtract the number of trailing zeros of the last number.
4538
+ if (v = c[v]) for (; v % 10 == 0; v /= 10, n--);
4539
+ if (n < 0) n = 0;
4540
+
4541
+ return n;
4542
+ };
4543
+
4544
+
4545
+ /*
4546
+ * n / 0 = I
4547
+ * n / N = N
4548
+ * n / I = 0
4549
+ * 0 / n = 0
4550
+ * 0 / 0 = N
4551
+ * 0 / N = N
4552
+ * 0 / I = 0
4553
+ * N / n = N
4554
+ * N / 0 = N
4555
+ * N / N = N
4556
+ * N / I = N
4557
+ * I / n = I
4558
+ * I / 0 = I
4559
+ * I / N = N
4560
+ * I / I = N
4561
+ *
4562
+ * Return a new BigNumber whose value is the value of this BigNumber divided by the value of
4563
+ * BigNumber(y, b), rounded according to DECIMAL_PLACES and ROUNDING_MODE.
4564
+ */
4565
+ P.dividedBy = P.div = function (y, b) {
4566
+ return div(this, new BigNumber(y, b), DECIMAL_PLACES, ROUNDING_MODE);
4567
+ };
4568
+
4569
+
4570
+ /*
4571
+ * Return a new BigNumber whose value is the integer part of dividing the value of this
4572
+ * BigNumber by the value of BigNumber(y, b).
4573
+ */
4574
+ P.dividedToIntegerBy = P.idiv = function (y, b) {
4575
+ return div(this, new BigNumber(y, b), 0, 1);
4576
+ };
4577
+
4578
+
4579
+ /*
4580
+ * Return a BigNumber whose value is the value of this BigNumber exponentiated by n.
4581
+ *
4582
+ * If m is present, return the result modulo m.
4583
+ * If n is negative round according to DECIMAL_PLACES and ROUNDING_MODE.
4584
+ * If POW_PRECISION is non-zero and m is not present, round to POW_PRECISION using ROUNDING_MODE.
4585
+ *
4586
+ * The modular power operation works efficiently when x, n, and m are integers, otherwise it
4587
+ * is equivalent to calculating x.exponentiatedBy(n).modulo(m) with a POW_PRECISION of 0.
4588
+ *
4589
+ * n {number|string|BigNumber} The exponent. An integer.
4590
+ * [m] {number|string|BigNumber} The modulus.
4591
+ *
4592
+ * '[BigNumber Error] Exponent not an integer: {n}'
4593
+ */
4594
+ P.exponentiatedBy = P.pow = function (n, m) {
4595
+ var half, isModExp, i, k, more, nIsBig, nIsNeg, nIsOdd, y,
4596
+ x = this;
4597
+
4598
+ n = new BigNumber(n);
4599
+
4600
+ // Allow NaN and ±Infinity, but not other non-integers.
4601
+ if (n.c && !n.isInteger()) {
4602
+ throw Error
4603
+ (bignumberError + 'Exponent not an integer: ' + valueOf(n));
4604
+ }
4605
+
4606
+ if (m != null) m = new BigNumber(m);
4607
+
4608
+ // Exponent of MAX_SAFE_INTEGER is 15.
4609
+ nIsBig = n.e > 14;
4610
+
4611
+ // If x is NaN, ±Infinity, ±0 or ±1, or n is ±Infinity, NaN or ±0.
4612
+ if (!x.c || !x.c[0] || x.c[0] == 1 && !x.e && x.c.length == 1 || !n.c || !n.c[0]) {
4613
+
4614
+ // The sign of the result of pow when x is negative depends on the evenness of n.
4615
+ // If +n overflows to ±Infinity, the evenness of n would be not be known.
4616
+ y = new BigNumber(Math.pow(+valueOf(x), nIsBig ? n.s * (2 - isOdd(n)) : +valueOf(n)));
4617
+ return m ? y.mod(m) : y;
4618
+ }
4619
+
4620
+ nIsNeg = n.s < 0;
4621
+
4622
+ if (m) {
4623
+
4624
+ // x % m returns NaN if abs(m) is zero, or m is NaN.
4625
+ if (m.c ? !m.c[0] : !m.s) return new BigNumber(NaN);
4626
+
4627
+ isModExp = !nIsNeg && x.isInteger() && m.isInteger();
4628
+
4629
+ if (isModExp) x = x.mod(m);
4630
+
4631
+ // Overflow to ±Infinity: >=2**1e10 or >=1.0000024**1e15.
4632
+ // Underflow to ±0: <=0.79**1e10 or <=0.9999975**1e15.
4633
+ } else if (n.e > 9 && (x.e > 0 || x.e < -1 || (x.e == 0
4634
+ // [1, 240000000]
4635
+ ? x.c[0] > 1 || nIsBig && x.c[1] >= 24e7
4636
+ // [80000000000000] [99999750000000]
4637
+ : x.c[0] < 8e13 || nIsBig && x.c[0] <= 9999975e7))) {
4638
+
4639
+ // If x is negative and n is odd, k = -0, else k = 0.
4640
+ k = x.s < 0 && isOdd(n) ? -0 : 0;
4641
+
4642
+ // If x >= 1, k = ±Infinity.
4643
+ if (x.e > -1) k = 1 / k;
4644
+
4645
+ // If n is negative return ±0, else return ±Infinity.
4646
+ return new BigNumber(nIsNeg ? 1 / k : k);
4647
+
4648
+ } else if (POW_PRECISION) {
4649
+
4650
+ // Truncating each coefficient array to a length of k after each multiplication
4651
+ // equates to truncating significant digits to POW_PRECISION + [28, 41],
4652
+ // i.e. there will be a minimum of 28 guard digits retained.
4653
+ k = mathceil(POW_PRECISION / LOG_BASE + 2);
4654
+ }
4655
+
4656
+ if (nIsBig) {
4657
+ half = new BigNumber(0.5);
4658
+ if (nIsNeg) n.s = 1;
4659
+ nIsOdd = isOdd(n);
4660
+ } else {
4661
+ i = Math.abs(+valueOf(n));
4662
+ nIsOdd = i % 2;
4663
+ }
4664
+
4665
+ y = new BigNumber(ONE);
4666
+
4667
+ // Performs 54 loop iterations for n of 9007199254740991.
4668
+ for (; ;) {
4669
+
4670
+ if (nIsOdd) {
4671
+ y = y.times(x);
4672
+ if (!y.c) break;
4673
+
4674
+ if (k) {
4675
+ if (y.c.length > k) y.c.length = k;
4676
+ } else if (isModExp) {
4677
+ y = y.mod(m); //y = y.minus(div(y, m, 0, MODULO_MODE).times(m));
4678
+ }
4679
+ }
4680
+
4681
+ if (i) {
4682
+ i = mathfloor(i / 2);
4683
+ if (i === 0) break;
4684
+ nIsOdd = i % 2;
4685
+ } else {
4686
+ n = n.times(half);
4687
+ round(n, n.e + 1, 1);
4688
+
4689
+ if (n.e > 14) {
4690
+ nIsOdd = isOdd(n);
4691
+ } else {
4692
+ i = +valueOf(n);
4693
+ if (i === 0) break;
4694
+ nIsOdd = i % 2;
4695
+ }
4696
+ }
4697
+
4698
+ x = x.times(x);
4699
+
4700
+ if (k) {
4701
+ if (x.c && x.c.length > k) x.c.length = k;
4702
+ } else if (isModExp) {
4703
+ x = x.mod(m); //x = x.minus(div(x, m, 0, MODULO_MODE).times(m));
4704
+ }
4705
+ }
4706
+
4707
+ if (isModExp) return y;
4708
+ if (nIsNeg) y = ONE.div(y);
4709
+
4710
+ return m ? y.mod(m) : k ? round(y, POW_PRECISION, ROUNDING_MODE, more) : y;
4711
+ };
4712
+
4713
+
4714
+ /*
4715
+ * Return a new BigNumber whose value is the value of this BigNumber rounded to an integer
4716
+ * using rounding mode rm, or ROUNDING_MODE if rm is omitted.
4717
+ *
4718
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
4719
+ *
4720
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {rm}'
4721
+ */
4722
+ P.integerValue = function (rm) {
4723
+ var n = new BigNumber(this);
4724
+ if (rm == null) rm = ROUNDING_MODE;
4725
+ else intCheck(rm, 0, 8);
4726
+ return round(n, n.e + 1, rm);
4727
+ };
4728
+
4729
+
4730
+ /*
4731
+ * Return true if the value of this BigNumber is equal to the value of BigNumber(y, b),
4732
+ * otherwise return false.
4733
+ */
4734
+ P.isEqualTo = P.eq = function (y, b) {
4735
+ return compare(this, new BigNumber(y, b)) === 0;
4736
+ };
4737
+
4738
+
4739
+ /*
4740
+ * Return true if the value of this BigNumber is a finite number, otherwise return false.
4741
+ */
4742
+ P.isFinite = function () {
4743
+ return !!this.c;
4744
+ };
4745
+
4746
+
4747
+ /*
4748
+ * Return true if the value of this BigNumber is greater than the value of BigNumber(y, b),
4749
+ * otherwise return false.
4750
+ */
4751
+ P.isGreaterThan = P.gt = function (y, b) {
4752
+ return compare(this, new BigNumber(y, b)) > 0;
4753
+ };
4754
+
4755
+
4756
+ /*
4757
+ * Return true if the value of this BigNumber is greater than or equal to the value of
4758
+ * BigNumber(y, b), otherwise return false.
4759
+ */
4760
+ P.isGreaterThanOrEqualTo = P.gte = function (y, b) {
4761
+ return (b = compare(this, new BigNumber(y, b))) === 1 || b === 0;
4762
+
4763
+ };
4764
+
4765
+
4766
+ /*
4767
+ * Return true if the value of this BigNumber is an integer, otherwise return false.
4768
+ */
4769
+ P.isInteger = function () {
4770
+ return !!this.c && bitFloor(this.e / LOG_BASE) > this.c.length - 2;
4771
+ };
4772
+
4773
+
4774
+ /*
4775
+ * Return true if the value of this BigNumber is less than the value of BigNumber(y, b),
4776
+ * otherwise return false.
4777
+ */
4778
+ P.isLessThan = P.lt = function (y, b) {
4779
+ return compare(this, new BigNumber(y, b)) < 0;
4780
+ };
4781
+
4782
+
4783
+ /*
4784
+ * Return true if the value of this BigNumber is less than or equal to the value of
4785
+ * BigNumber(y, b), otherwise return false.
4786
+ */
4787
+ P.isLessThanOrEqualTo = P.lte = function (y, b) {
4788
+ return (b = compare(this, new BigNumber(y, b))) === -1 || b === 0;
4789
+ };
4790
+
4791
+
4792
+ /*
4793
+ * Return true if the value of this BigNumber is NaN, otherwise return false.
4794
+ */
4795
+ P.isNaN = function () {
4796
+ return !this.s;
4797
+ };
4798
+
4799
+
4800
+ /*
4801
+ * Return true if the value of this BigNumber is negative, otherwise return false.
4802
+ */
4803
+ P.isNegative = function () {
4804
+ return this.s < 0;
4805
+ };
4806
+
4807
+
4808
+ /*
4809
+ * Return true if the value of this BigNumber is positive, otherwise return false.
4810
+ */
4811
+ P.isPositive = function () {
4812
+ return this.s > 0;
4813
+ };
4814
+
4815
+
4816
+ /*
4817
+ * Return true if the value of this BigNumber is 0 or -0, otherwise return false.
4818
+ */
4819
+ P.isZero = function () {
4820
+ return !!this.c && this.c[0] == 0;
4821
+ };
4822
+
4823
+
4824
+ /*
4825
+ * n - 0 = n
4826
+ * n - N = N
4827
+ * n - I = -I
4828
+ * 0 - n = -n
4829
+ * 0 - 0 = 0
4830
+ * 0 - N = N
4831
+ * 0 - I = -I
4832
+ * N - n = N
4833
+ * N - 0 = N
4834
+ * N - N = N
4835
+ * N - I = N
4836
+ * I - n = I
4837
+ * I - 0 = I
4838
+ * I - N = N
4839
+ * I - I = N
4840
+ *
4841
+ * Return a new BigNumber whose value is the value of this BigNumber minus the value of
4842
+ * BigNumber(y, b).
4843
+ */
4844
+ P.minus = function (y, b) {
4845
+ var i, j, t, xLTy,
4846
+ x = this,
4847
+ a = x.s;
4848
+
4849
+ y = new BigNumber(y, b);
4850
+ b = y.s;
4851
+
4852
+ // Either NaN?
4853
+ if (!a || !b) return new BigNumber(NaN);
4854
+
4855
+ // Signs differ?
4856
+ if (a != b) {
4857
+ y.s = -b;
4858
+ return x.plus(y);
4859
+ }
4860
+
4861
+ var xe = x.e / LOG_BASE,
4862
+ ye = y.e / LOG_BASE,
4863
+ xc = x.c,
4864
+ yc = y.c;
4865
+
4866
+ if (!xe || !ye) {
4867
+
4868
+ // Either Infinity?
4869
+ if (!xc || !yc) return xc ? (y.s = -b, y) : new BigNumber(yc ? x : NaN);
4870
+
4871
+ // Either zero?
4872
+ if (!xc[0] || !yc[0]) {
4873
+
4874
+ // Return y if y is non-zero, x if x is non-zero, or zero if both are zero.
4875
+ return yc[0] ? (y.s = -b, y) : new BigNumber(xc[0] ? x :
4876
+
4877
+ // IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity
4878
+ ROUNDING_MODE == 3 ? -0 : 0);
4879
+ }
4880
+ }
4881
+
4882
+ xe = bitFloor(xe);
4883
+ ye = bitFloor(ye);
4884
+ xc = xc.slice();
4885
+
4886
+ // Determine which is the bigger number.
4887
+ if (a = xe - ye) {
4888
+
4889
+ if (xLTy = a < 0) {
4890
+ a = -a;
4891
+ t = xc;
4892
+ } else {
4893
+ ye = xe;
4894
+ t = yc;
4895
+ }
4896
+
4897
+ t.reverse();
4898
+
4899
+ // Prepend zeros to equalise exponents.
4900
+ for (b = a; b--; t.push(0));
4901
+ t.reverse();
4902
+ } else {
4903
+
4904
+ // Exponents equal. Check digit by digit.
4905
+ j = (xLTy = (a = xc.length) < (b = yc.length)) ? a : b;
4906
+
4907
+ for (a = b = 0; b < j; b++) {
4908
+
4909
+ if (xc[b] != yc[b]) {
4910
+ xLTy = xc[b] < yc[b];
4911
+ break;
4912
+ }
4913
+ }
4914
+ }
4915
+
4916
+ // x < y? Point xc to the array of the bigger number.
4917
+ if (xLTy) {
4918
+ t = xc;
4919
+ xc = yc;
4920
+ yc = t;
4921
+ y.s = -y.s;
4922
+ }
4923
+
4924
+ b = (j = yc.length) - (i = xc.length);
4925
+
4926
+ // Append zeros to xc if shorter.
4927
+ // No need to add zeros to yc if shorter as subtract only needs to start at yc.length.
4928
+ if (b > 0) for (; b--; xc[i++] = 0);
4929
+ b = BASE - 1;
4930
+
4931
+ // Subtract yc from xc.
4932
+ for (; j > a;) {
4933
+
4934
+ if (xc[--j] < yc[j]) {
4935
+ for (i = j; i && !xc[--i]; xc[i] = b);
4936
+ --xc[i];
4937
+ xc[j] += BASE;
4938
+ }
4939
+
4940
+ xc[j] -= yc[j];
4941
+ }
4942
+
4943
+ // Remove leading zeros and adjust exponent accordingly.
4944
+ for (; xc[0] == 0; xc.splice(0, 1), --ye);
4945
+
4946
+ // Zero?
4947
+ if (!xc[0]) {
4948
+
4949
+ // Following IEEE 754 (2008) 6.3,
4950
+ // n - n = +0 but n - n = -0 when rounding towards -Infinity.
4951
+ y.s = ROUNDING_MODE == 3 ? -1 : 1;
4952
+ y.c = [y.e = 0];
4953
+ return y;
4954
+ }
4955
+
4956
+ // No need to check for Infinity as +x - +y != Infinity && -x - -y != Infinity
4957
+ // for finite x and y.
4958
+ return normalise(y, xc, ye);
4959
+ };
4960
+
4961
+
4962
+ /*
4963
+ * n % 0 = N
4964
+ * n % N = N
4965
+ * n % I = n
4966
+ * 0 % n = 0
4967
+ * -0 % n = -0
4968
+ * 0 % 0 = N
4969
+ * 0 % N = N
4970
+ * 0 % I = 0
4971
+ * N % n = N
4972
+ * N % 0 = N
4973
+ * N % N = N
4974
+ * N % I = N
4975
+ * I % n = N
4976
+ * I % 0 = N
4977
+ * I % N = N
4978
+ * I % I = N
4979
+ *
4980
+ * Return a new BigNumber whose value is the value of this BigNumber modulo the value of
4981
+ * BigNumber(y, b). The result depends on the value of MODULO_MODE.
4982
+ */
4983
+ P.modulo = P.mod = function (y, b) {
4984
+ var q, s,
4985
+ x = this;
4986
+
4987
+ y = new BigNumber(y, b);
4988
+
4989
+ // Return NaN if x is Infinity or NaN, or y is NaN or zero.
4990
+ if (!x.c || !y.s || y.c && !y.c[0]) {
4991
+ return new BigNumber(NaN);
4992
+
4993
+ // Return x if y is Infinity or x is zero.
4994
+ } else if (!y.c || x.c && !x.c[0]) {
4995
+ return new BigNumber(x);
4996
+ }
4997
+
4998
+ if (MODULO_MODE == 9) {
4999
+
5000
+ // Euclidian division: q = sign(y) * floor(x / abs(y))
5001
+ // r = x - qy where 0 <= r < abs(y)
5002
+ s = y.s;
5003
+ y.s = 1;
5004
+ q = div(x, y, 0, 3);
5005
+ y.s = s;
5006
+ q.s *= s;
5007
+ } else {
5008
+ q = div(x, y, 0, MODULO_MODE);
5009
+ }
5010
+
5011
+ y = x.minus(q.times(y));
5012
+
5013
+ // To match JavaScript %, ensure sign of zero is sign of dividend.
5014
+ if (!y.c[0] && MODULO_MODE == 1) y.s = x.s;
5015
+
5016
+ return y;
5017
+ };
5018
+
5019
+
5020
+ /*
5021
+ * n * 0 = 0
5022
+ * n * N = N
5023
+ * n * I = I
5024
+ * 0 * n = 0
5025
+ * 0 * 0 = 0
5026
+ * 0 * N = N
5027
+ * 0 * I = N
5028
+ * N * n = N
5029
+ * N * 0 = N
5030
+ * N * N = N
5031
+ * N * I = N
5032
+ * I * n = I
5033
+ * I * 0 = N
5034
+ * I * N = N
5035
+ * I * I = I
5036
+ *
5037
+ * Return a new BigNumber whose value is the value of this BigNumber multiplied by the value
5038
+ * of BigNumber(y, b).
5039
+ */
5040
+ P.multipliedBy = P.times = function (y, b) {
5041
+ var c, e, i, j, k, m, xcL, xlo, xhi, ycL, ylo, yhi, zc,
5042
+ base, sqrtBase,
5043
+ x = this,
5044
+ xc = x.c,
5045
+ yc = (y = new BigNumber(y, b)).c;
5046
+
5047
+ // Either NaN, ±Infinity or ±0?
5048
+ if (!xc || !yc || !xc[0] || !yc[0]) {
5049
+
5050
+ // Return NaN if either is NaN, or one is 0 and the other is Infinity.
5051
+ if (!x.s || !y.s || xc && !xc[0] && !yc || yc && !yc[0] && !xc) {
5052
+ y.c = y.e = y.s = null;
5053
+ } else {
5054
+ y.s *= x.s;
5055
+
5056
+ // Return ±Infinity if either is ±Infinity.
5057
+ if (!xc || !yc) {
5058
+ y.c = y.e = null;
5059
+
5060
+ // Return ±0 if either is ±0.
5061
+ } else {
5062
+ y.c = [0];
5063
+ y.e = 0;
5064
+ }
5065
+ }
5066
+
5067
+ return y;
5068
+ }
5069
+
5070
+ e = bitFloor(x.e / LOG_BASE) + bitFloor(y.e / LOG_BASE);
5071
+ y.s *= x.s;
5072
+ xcL = xc.length;
5073
+ ycL = yc.length;
5074
+
5075
+ // Ensure xc points to longer array and xcL to its length.
5076
+ if (xcL < ycL) {
5077
+ zc = xc;
5078
+ xc = yc;
5079
+ yc = zc;
5080
+ i = xcL;
5081
+ xcL = ycL;
5082
+ ycL = i;
5083
+ }
5084
+
5085
+ // Initialise the result array with zeros.
5086
+ for (i = xcL + ycL, zc = []; i--; zc.push(0));
5087
+
5088
+ base = BASE;
5089
+ sqrtBase = SQRT_BASE;
5090
+
5091
+ for (i = ycL; --i >= 0;) {
5092
+ c = 0;
5093
+ ylo = yc[i] % sqrtBase;
5094
+ yhi = yc[i] / sqrtBase | 0;
5095
+
5096
+ for (k = xcL, j = i + k; j > i;) {
5097
+ xlo = xc[--k] % sqrtBase;
5098
+ xhi = xc[k] / sqrtBase | 0;
5099
+ m = yhi * xlo + xhi * ylo;
5100
+ xlo = ylo * xlo + ((m % sqrtBase) * sqrtBase) + zc[j] + c;
5101
+ c = (xlo / base | 0) + (m / sqrtBase | 0) + yhi * xhi;
5102
+ zc[j--] = xlo % base;
5103
+ }
5104
+
5105
+ zc[j] = c;
5106
+ }
5107
+
5108
+ if (c) {
5109
+ ++e;
5110
+ } else {
5111
+ zc.splice(0, 1);
5112
+ }
5113
+
5114
+ return normalise(y, zc, e);
5115
+ };
5116
+
5117
+
5118
+ /*
5119
+ * Return a new BigNumber whose value is the value of this BigNumber negated,
5120
+ * i.e. multiplied by -1.
5121
+ */
5122
+ P.negated = function () {
5123
+ var x = new BigNumber(this);
5124
+ x.s = -x.s || null;
5125
+ return x;
5126
+ };
5127
+
5128
+
5129
+ /*
5130
+ * n + 0 = n
5131
+ * n + N = N
5132
+ * n + I = I
5133
+ * 0 + n = n
5134
+ * 0 + 0 = 0
5135
+ * 0 + N = N
5136
+ * 0 + I = I
5137
+ * N + n = N
5138
+ * N + 0 = N
5139
+ * N + N = N
5140
+ * N + I = N
5141
+ * I + n = I
5142
+ * I + 0 = I
5143
+ * I + N = N
5144
+ * I + I = I
5145
+ *
5146
+ * Return a new BigNumber whose value is the value of this BigNumber plus the value of
5147
+ * BigNumber(y, b).
5148
+ */
5149
+ P.plus = function (y, b) {
5150
+ var t,
5151
+ x = this,
5152
+ a = x.s;
5153
+
5154
+ y = new BigNumber(y, b);
5155
+ b = y.s;
5156
+
5157
+ // Either NaN?
5158
+ if (!a || !b) return new BigNumber(NaN);
5159
+
5160
+ // Signs differ?
5161
+ if (a != b) {
5162
+ y.s = -b;
5163
+ return x.minus(y);
5164
+ }
5165
+
5166
+ var xe = x.e / LOG_BASE,
5167
+ ye = y.e / LOG_BASE,
5168
+ xc = x.c,
5169
+ yc = y.c;
5170
+
5171
+ if (!xe || !ye) {
5172
+
5173
+ // Return ±Infinity if either ±Infinity.
5174
+ if (!xc || !yc) return new BigNumber(a / 0);
5175
+
5176
+ // Either zero?
5177
+ // Return y if y is non-zero, x if x is non-zero, or zero if both are zero.
5178
+ if (!xc[0] || !yc[0]) return yc[0] ? y : new BigNumber(xc[0] ? x : a * 0);
5179
+ }
5180
+
5181
+ xe = bitFloor(xe);
5182
+ ye = bitFloor(ye);
5183
+ xc = xc.slice();
5184
+
5185
+ // Prepend zeros to equalise exponents. Faster to use reverse then do unshifts.
5186
+ if (a = xe - ye) {
5187
+ if (a > 0) {
5188
+ ye = xe;
5189
+ t = yc;
5190
+ } else {
5191
+ a = -a;
5192
+ t = xc;
5193
+ }
5194
+
5195
+ t.reverse();
5196
+ for (; a--; t.push(0));
5197
+ t.reverse();
5198
+ }
5199
+
5200
+ a = xc.length;
5201
+ b = yc.length;
5202
+
5203
+ // Point xc to the longer array, and b to the shorter length.
5204
+ if (a - b < 0) {
5205
+ t = yc;
5206
+ yc = xc;
5207
+ xc = t;
5208
+ b = a;
5209
+ }
5210
+
5211
+ // Only start adding at yc.length - 1 as the further digits of xc can be ignored.
5212
+ for (a = 0; b;) {
5213
+ a = (xc[--b] = xc[b] + yc[b] + a) / BASE | 0;
5214
+ xc[b] = BASE === xc[b] ? 0 : xc[b] % BASE;
5215
+ }
5216
+
5217
+ if (a) {
5218
+ xc = [a].concat(xc);
5219
+ ++ye;
5220
+ }
5221
+
5222
+ // No need to check for zero, as +x + +y != 0 && -x + -y != 0
5223
+ // ye = MAX_EXP + 1 possible
5224
+ return normalise(y, xc, ye);
5225
+ };
5226
+
5227
+
5228
+ /*
5229
+ * If sd is undefined or null or true or false, return the number of significant digits of
5230
+ * the value of this BigNumber, or null if the value of this BigNumber is ±Infinity or NaN.
5231
+ * If sd is true include integer-part trailing zeros in the count.
5232
+ *
5233
+ * Otherwise, if sd is a number, return a new BigNumber whose value is the value of this
5234
+ * BigNumber rounded to a maximum of sd significant digits using rounding mode rm, or
5235
+ * ROUNDING_MODE if rm is omitted.
5236
+ *
5237
+ * sd {number|boolean} number: significant digits: integer, 1 to MAX inclusive.
5238
+ * boolean: whether to count integer-part trailing zeros: true or false.
5239
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
5240
+ *
5241
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {sd|rm}'
5242
+ */
5243
+ P.precision = P.sd = function (sd, rm) {
5244
+ var c, n, v,
5245
+ x = this;
5246
+
5247
+ if (sd != null && sd !== !!sd) {
5248
+ intCheck(sd, 1, MAX);
5249
+ if (rm == null) rm = ROUNDING_MODE;
5250
+ else intCheck(rm, 0, 8);
5251
+
5252
+ return round(new BigNumber(x), sd, rm);
5253
+ }
5254
+
5255
+ if (!(c = x.c)) return null;
5256
+ v = c.length - 1;
5257
+ n = v * LOG_BASE + 1;
5258
+
5259
+ if (v = c[v]) {
5260
+
5261
+ // Subtract the number of trailing zeros of the last element.
5262
+ for (; v % 10 == 0; v /= 10, n--);
5263
+
5264
+ // Add the number of digits of the first element.
5265
+ for (v = c[0]; v >= 10; v /= 10, n++);
5266
+ }
5267
+
5268
+ if (sd && x.e + 1 > n) n = x.e + 1;
5269
+
5270
+ return n;
5271
+ };
5272
+
5273
+
5274
+ /*
5275
+ * Return a new BigNumber whose value is the value of this BigNumber shifted by k places
5276
+ * (powers of 10). Shift to the right if n > 0, and to the left if n < 0.
5277
+ *
5278
+ * k {number} Integer, -MAX_SAFE_INTEGER to MAX_SAFE_INTEGER inclusive.
5279
+ *
5280
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {k}'
5281
+ */
5282
+ P.shiftedBy = function (k) {
5283
+ intCheck(k, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER);
5284
+ return this.times('1e' + k);
5285
+ };
5286
+
5287
+
5288
+ /*
5289
+ * sqrt(-n) = N
5290
+ * sqrt(N) = N
5291
+ * sqrt(-I) = N
5292
+ * sqrt(I) = I
5293
+ * sqrt(0) = 0
5294
+ * sqrt(-0) = -0
5295
+ *
5296
+ * Return a new BigNumber whose value is the square root of the value of this BigNumber,
5297
+ * rounded according to DECIMAL_PLACES and ROUNDING_MODE.
5298
+ */
5299
+ P.squareRoot = P.sqrt = function () {
5300
+ var m, n, r, rep, t,
5301
+ x = this,
5302
+ c = x.c,
5303
+ s = x.s,
5304
+ e = x.e,
5305
+ dp = DECIMAL_PLACES + 4,
5306
+ half = new BigNumber('0.5');
5307
+
5308
+ // Negative/NaN/Infinity/zero?
5309
+ if (s !== 1 || !c || !c[0]) {
5310
+ return new BigNumber(!s || s < 0 && (!c || c[0]) ? NaN : c ? x : 1 / 0);
5311
+ }
5312
+
5313
+ // Initial estimate.
5314
+ s = Math.sqrt(+valueOf(x));
5315
+
5316
+ // Math.sqrt underflow/overflow?
5317
+ // Pass x to Math.sqrt as integer, then adjust the exponent of the result.
5318
+ if (s == 0 || s == 1 / 0) {
5319
+ n = coeffToString(c);
5320
+ if ((n.length + e) % 2 == 0) n += '0';
5321
+ s = Math.sqrt(+n);
5322
+ e = bitFloor((e + 1) / 2) - (e < 0 || e % 2);
5323
+
5324
+ if (s == 1 / 0) {
5325
+ n = '5e' + e;
5326
+ } else {
5327
+ n = s.toExponential();
5328
+ n = n.slice(0, n.indexOf('e') + 1) + e;
5329
+ }
5330
+
5331
+ r = new BigNumber(n);
5332
+ } else {
5333
+ r = new BigNumber(s + '');
5334
+ }
5335
+
5336
+ // Check for zero.
5337
+ // r could be zero if MIN_EXP is changed after the this value was created.
5338
+ // This would cause a division by zero (x/t) and hence Infinity below, which would cause
5339
+ // coeffToString to throw.
5340
+ if (r.c[0]) {
5341
+ e = r.e;
5342
+ s = e + dp;
5343
+ if (s < 3) s = 0;
5344
+
5345
+ // Newton-Raphson iteration.
5346
+ for (; ;) {
5347
+ t = r;
5348
+ r = half.times(t.plus(div(x, t, dp, 1)));
5349
+
5350
+ if (coeffToString(t.c).slice(0, s) === (n = coeffToString(r.c)).slice(0, s)) {
5351
+
5352
+ // The exponent of r may here be one less than the final result exponent,
5353
+ // e.g 0.0009999 (e-4) --> 0.001 (e-3), so adjust s so the rounding digits
5354
+ // are indexed correctly.
5355
+ if (r.e < e) --s;
5356
+ n = n.slice(s - 3, s + 1);
5357
+
5358
+ // The 4th rounding digit may be in error by -1 so if the 4 rounding digits
5359
+ // are 9999 or 4999 (i.e. approaching a rounding boundary) continue the
5360
+ // iteration.
5361
+ if (n == '9999' || !rep && n == '4999') {
5362
+
5363
+ // On the first iteration only, check to see if rounding up gives the
5364
+ // exact result as the nines may infinitely repeat.
5365
+ if (!rep) {
5366
+ round(t, t.e + DECIMAL_PLACES + 2, 0);
5367
+
5368
+ if (t.times(t).eq(x)) {
5369
+ r = t;
5370
+ break;
5371
+ }
5372
+ }
5373
+
5374
+ dp += 4;
5375
+ s += 4;
5376
+ rep = 1;
5377
+ } else {
5378
+
5379
+ // If rounding digits are null, 0{0,4} or 50{0,3}, check for exact
5380
+ // result. If not, then there are further digits and m will be truthy.
5381
+ if (!+n || !+n.slice(1) && n.charAt(0) == '5') {
5382
+
5383
+ // Truncate to the first rounding digit.
5384
+ round(r, r.e + DECIMAL_PLACES + 2, 1);
5385
+ m = !r.times(r).eq(x);
5386
+ }
5387
+
5388
+ break;
5389
+ }
5390
+ }
5391
+ }
5392
+ }
5393
+
5394
+ return round(r, r.e + DECIMAL_PLACES + 1, ROUNDING_MODE, m);
5395
+ };
5396
+
5397
+
5398
+ /*
5399
+ * Return a string representing the value of this BigNumber in exponential notation and
5400
+ * rounded using ROUNDING_MODE to dp fixed decimal places.
5401
+ *
5402
+ * [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
5403
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
5404
+ *
5405
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
5406
+ */
5407
+ P.toExponential = function (dp, rm) {
5408
+ if (dp != null) {
5409
+ intCheck(dp, 0, MAX);
5410
+ dp++;
5411
+ }
5412
+ return format(this, dp, rm, 1);
5413
+ };
5414
+
5415
+
5416
+ /*
5417
+ * Return a string representing the value of this BigNumber in fixed-point notation rounding
5418
+ * to dp fixed decimal places using rounding mode rm, or ROUNDING_MODE if rm is omitted.
5419
+ *
5420
+ * Note: as with JavaScript's number type, (-0).toFixed(0) is '0',
5421
+ * but e.g. (-0.00001).toFixed(0) is '-0'.
5422
+ *
5423
+ * [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
5424
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
5425
+ *
5426
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
5427
+ */
5428
+ P.toFixed = function (dp, rm) {
5429
+ if (dp != null) {
5430
+ intCheck(dp, 0, MAX);
5431
+ dp = dp + this.e + 1;
5432
+ }
5433
+ return format(this, dp, rm);
5434
+ };
5435
+
5436
+
5437
+ /*
5438
+ * Return a string representing the value of this BigNumber in fixed-point notation rounded
5439
+ * using rm or ROUNDING_MODE to dp decimal places, and formatted according to the properties
5440
+ * of the format or FORMAT object (see BigNumber.set).
5441
+ *
5442
+ * The formatting object may contain some or all of the properties shown below.
5443
+ *
5444
+ * FORMAT = {
5445
+ * prefix: '',
5446
+ * groupSize: 3,
5447
+ * secondaryGroupSize: 0,
5448
+ * groupSeparator: ',',
5449
+ * decimalSeparator: '.',
5450
+ * fractionGroupSize: 0,
5451
+ * fractionGroupSeparator: '\xA0', // non-breaking space
5452
+ * suffix: ''
5453
+ * };
5454
+ *
5455
+ * [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
5456
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
5457
+ * [format] {object} Formatting options. See FORMAT pbject above.
5458
+ *
5459
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
5460
+ * '[BigNumber Error] Argument not an object: {format}'
5461
+ */
5462
+ P.toFormat = function (dp, rm, format) {
5463
+ var str,
5464
+ x = this;
5465
+
5466
+ if (format == null) {
5467
+ if (dp != null && rm && typeof rm == 'object') {
5468
+ format = rm;
5469
+ rm = null;
5470
+ } else if (dp && typeof dp == 'object') {
5471
+ format = dp;
5472
+ dp = rm = null;
5473
+ } else {
5474
+ format = FORMAT;
5475
+ }
5476
+ } else if (typeof format != 'object') {
5477
+ throw Error
5478
+ (bignumberError + 'Argument not an object: ' + format);
5479
+ }
5480
+
5481
+ str = x.toFixed(dp, rm);
5482
+
5483
+ if (x.c) {
5484
+ var i,
5485
+ arr = str.split('.'),
5486
+ g1 = +format.groupSize,
5487
+ g2 = +format.secondaryGroupSize,
5488
+ groupSeparator = format.groupSeparator || '',
5489
+ intPart = arr[0],
5490
+ fractionPart = arr[1],
5491
+ isNeg = x.s < 0,
5492
+ intDigits = isNeg ? intPart.slice(1) : intPart,
5493
+ len = intDigits.length;
5494
+
5495
+ if (g2) {
5496
+ i = g1;
5497
+ g1 = g2;
5498
+ g2 = i;
5499
+ len -= i;
5500
+ }
5501
+
5502
+ if (g1 > 0 && len > 0) {
5503
+ i = len % g1 || g1;
5504
+ intPart = intDigits.substr(0, i);
5505
+ for (; i < len; i += g1) intPart += groupSeparator + intDigits.substr(i, g1);
5506
+ if (g2 > 0) intPart += groupSeparator + intDigits.slice(i);
5507
+ if (isNeg) intPart = '-' + intPart;
5508
+ }
5509
+
5510
+ str = fractionPart
5511
+ ? intPart + (format.decimalSeparator || '') + ((g2 = +format.fractionGroupSize)
5512
+ ? fractionPart.replace(new RegExp('\\d{' + g2 + '}\\B', 'g'),
5513
+ '$&' + (format.fractionGroupSeparator || ''))
5514
+ : fractionPart)
5515
+ : intPart;
5516
+ }
5517
+
5518
+ return (format.prefix || '') + str + (format.suffix || '');
5519
+ };
5520
+
5521
+
5522
+ /*
5523
+ * Return an array of two BigNumbers representing the value of this BigNumber as a simple
5524
+ * fraction with an integer numerator and an integer denominator.
5525
+ * The denominator will be a positive non-zero value less than or equal to the specified
5526
+ * maximum denominator. If a maximum denominator is not specified, the denominator will be
5527
+ * the lowest value necessary to represent the number exactly.
5528
+ *
5529
+ * [md] {number|string|BigNumber} Integer >= 1, or Infinity. The maximum denominator.
5530
+ *
5531
+ * '[BigNumber Error] Argument {not an integer|out of range} : {md}'
5532
+ */
5533
+ P.toFraction = function (md) {
5534
+ var d, d0, d1, d2, e, exp, n, n0, n1, q, r, s,
5535
+ x = this,
5536
+ xc = x.c;
5537
+
5538
+ if (md != null) {
5539
+ n = new BigNumber(md);
5540
+
5541
+ // Throw if md is less than one or is not an integer, unless it is Infinity.
5542
+ if (!n.isInteger() && (n.c || n.s !== 1) || n.lt(ONE)) {
5543
+ throw Error
5544
+ (bignumberError + 'Argument ' +
5545
+ (n.isInteger() ? 'out of range: ' : 'not an integer: ') + valueOf(n));
5546
+ }
5547
+ }
5548
+
5549
+ if (!xc) return new BigNumber(x);
5550
+
5551
+ d = new BigNumber(ONE);
5552
+ n1 = d0 = new BigNumber(ONE);
5553
+ d1 = n0 = new BigNumber(ONE);
5554
+ s = coeffToString(xc);
5555
+
5556
+ // Determine initial denominator.
5557
+ // d is a power of 10 and the minimum max denominator that specifies the value exactly.
5558
+ e = d.e = s.length - x.e - 1;
5559
+ d.c[0] = POWS_TEN[(exp = e % LOG_BASE) < 0 ? LOG_BASE + exp : exp];
5560
+ md = !md || n.comparedTo(d) > 0 ? (e > 0 ? d : n1) : n;
5561
+
5562
+ exp = MAX_EXP;
5563
+ MAX_EXP = 1 / 0;
5564
+ n = new BigNumber(s);
5565
+
5566
+ // n0 = d1 = 0
5567
+ n0.c[0] = 0;
5568
+
5569
+ for (; ;) {
5570
+ q = div(n, d, 0, 1);
5571
+ d2 = d0.plus(q.times(d1));
5572
+ if (d2.comparedTo(md) == 1) break;
5573
+ d0 = d1;
5574
+ d1 = d2;
5575
+ n1 = n0.plus(q.times(d2 = n1));
5576
+ n0 = d2;
5577
+ d = n.minus(q.times(d2 = d));
5578
+ n = d2;
5579
+ }
5580
+
5581
+ d2 = div(md.minus(d0), d1, 0, 1);
5582
+ n0 = n0.plus(d2.times(n1));
5583
+ d0 = d0.plus(d2.times(d1));
5584
+ n0.s = n1.s = x.s;
5585
+ e = e * 2;
5586
+
5587
+ // Determine which fraction is closer to x, n0/d0 or n1/d1
5588
+ r = div(n1, d1, e, ROUNDING_MODE).minus(x).abs().comparedTo(
5589
+ div(n0, d0, e, ROUNDING_MODE).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0];
5590
+
5591
+ MAX_EXP = exp;
5592
+
5593
+ return r;
5594
+ };
5595
+
5596
+
5597
+ /*
5598
+ * Return the value of this BigNumber converted to a number primitive.
5599
+ */
5600
+ P.toNumber = function () {
5601
+ return +valueOf(this);
5602
+ };
5603
+
5604
+
5605
+ /*
5606
+ * Return a string representing the value of this BigNumber rounded to sd significant digits
5607
+ * using rounding mode rm or ROUNDING_MODE. If sd is less than the number of digits
5608
+ * necessary to represent the integer part of the value in fixed-point notation, then use
5609
+ * exponential notation.
5610
+ *
5611
+ * [sd] {number} Significant digits. Integer, 1 to MAX inclusive.
5612
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
5613
+ *
5614
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {sd|rm}'
5615
+ */
5616
+ P.toPrecision = function (sd, rm) {
5617
+ if (sd != null) intCheck(sd, 1, MAX);
5618
+ return format(this, sd, rm, 2);
5619
+ };
5620
+
5621
+
5622
+ /*
5623
+ * Return a string representing the value of this BigNumber in base b, or base 10 if b is
5624
+ * omitted. If a base is specified, including base 10, round according to DECIMAL_PLACES and
5625
+ * ROUNDING_MODE. If a base is not specified, and this BigNumber has a positive exponent
5626
+ * that is equal to or greater than TO_EXP_POS, or a negative exponent equal to or less than
5627
+ * TO_EXP_NEG, return exponential notation.
5628
+ *
5629
+ * [b] {number} Integer, 2 to ALPHABET.length inclusive.
5630
+ *
5631
+ * '[BigNumber Error] Base {not a primitive number|not an integer|out of range}: {b}'
5632
+ */
5633
+ P.toString = function (b) {
5634
+ var str,
5635
+ n = this,
5636
+ s = n.s,
5637
+ e = n.e;
5638
+
5639
+ // Infinity or NaN?
5640
+ if (e === null) {
5641
+ if (s) {
5642
+ str = 'Infinity';
5643
+ if (s < 0) str = '-' + str;
5644
+ } else {
5645
+ str = 'NaN';
5646
+ }
5647
+ } else {
5648
+ if (b == null) {
5649
+ str = e <= TO_EXP_NEG || e >= TO_EXP_POS
5650
+ ? toExponential(coeffToString(n.c), e)
5651
+ : toFixedPoint(coeffToString(n.c), e, '0');
5652
+ } else if (b === 10 && alphabetHasNormalDecimalDigits) {
5653
+ n = round(new BigNumber(n), DECIMAL_PLACES + e + 1, ROUNDING_MODE);
5654
+ str = toFixedPoint(coeffToString(n.c), n.e, '0');
5655
+ } else {
5656
+ intCheck(b, 2, ALPHABET.length, 'Base');
5657
+ str = convertBase(toFixedPoint(coeffToString(n.c), e, '0'), 10, b, s, true);
5658
+ }
5659
+
5660
+ if (s < 0 && n.c[0]) str = '-' + str;
5661
+ }
5662
+
5663
+ return str;
5664
+ };
5665
+
5666
+
5667
+ /*
5668
+ * Return as toString, but do not accept a base argument, and include the minus sign for
5669
+ * negative zero.
5670
+ */
5671
+ P.valueOf = P.toJSON = function () {
5672
+ return valueOf(this);
5673
+ };
5674
+
5675
+
5676
+ P._isBigNumber = true;
5677
+
5678
+ P[Symbol.toStringTag] = 'BigNumber';
5679
+
5680
+ // Node.js v10.12.0+
5681
+ P[Symbol.for('nodejs.util.inspect.custom')] = P.valueOf;
5682
+
5683
+ if (configObject != null) BigNumber.set(configObject);
5684
+
5685
+ return BigNumber;
5686
+ }
5687
+
5688
+
5689
+ // PRIVATE HELPER FUNCTIONS
5690
+
5691
+ // These functions don't need access to variables,
5692
+ // e.g. DECIMAL_PLACES, in the scope of the `clone` function above.
5693
+
5694
+
5695
+ function bitFloor(n) {
5696
+ var i = n | 0;
5697
+ return n > 0 || n === i ? i : i - 1;
5698
+ }
5699
+
5700
+
5701
+ // Return a coefficient array as a string of base 10 digits.
5702
+ function coeffToString(a) {
5703
+ var s, z,
5704
+ i = 1,
5705
+ j = a.length,
5706
+ r = a[0] + '';
5707
+
5708
+ for (; i < j;) {
5709
+ s = a[i++] + '';
5710
+ z = LOG_BASE - s.length;
5711
+ for (; z--; s = '0' + s);
5712
+ r += s;
5713
+ }
5714
+
5715
+ // Determine trailing zeros.
5716
+ for (j = r.length; r.charCodeAt(--j) === 48;);
5717
+
5718
+ return r.slice(0, j + 1 || 1);
5719
+ }
5720
+
5721
+
5722
+ // Compare the value of BigNumbers x and y.
5723
+ function compare(x, y) {
5724
+ var a, b,
5725
+ xc = x.c,
5726
+ yc = y.c,
5727
+ i = x.s,
5728
+ j = y.s,
5729
+ k = x.e,
5730
+ l = y.e;
5731
+
5732
+ // Either NaN?
5733
+ if (!i || !j) return null;
5734
+
5735
+ a = xc && !xc[0];
5736
+ b = yc && !yc[0];
5737
+
5738
+ // Either zero?
5739
+ if (a || b) return a ? b ? 0 : -j : i;
5740
+
5741
+ // Signs differ?
5742
+ if (i != j) return i;
5743
+
5744
+ a = i < 0;
5745
+ b = k == l;
5746
+
5747
+ // Either Infinity?
5748
+ if (!xc || !yc) return b ? 0 : !xc ^ a ? 1 : -1;
5749
+
5750
+ // Compare exponents.
5751
+ if (!b) return k > l ^ a ? 1 : -1;
5752
+
5753
+ j = (k = xc.length) < (l = yc.length) ? k : l;
5754
+
5755
+ // Compare digit by digit.
5756
+ for (i = 0; i < j; i++) if (xc[i] != yc[i]) return xc[i] > yc[i] ^ a ? 1 : -1;
5757
+
5758
+ // Compare lengths.
5759
+ return k == l ? 0 : k > l ^ a ? 1 : -1;
5760
+ }
5761
+
5762
+
5763
+ /*
5764
+ * Check that n is a primitive number, an integer, and in range, otherwise throw.
5765
+ */
5766
+ function intCheck(n, min, max, name) {
5767
+ if (n < min || n > max || n !== mathfloor(n)) {
5768
+ throw Error
5769
+ (bignumberError + (name || 'Argument') + (typeof n == 'number'
5770
+ ? n < min || n > max ? ' out of range: ' : ' not an integer: '
5771
+ : ' not a primitive number: ') + String(n));
5772
+ }
5773
+ }
5774
+
5775
+
5776
+ // Assumes finite n.
5777
+ function isOdd(n) {
5778
+ var k = n.c.length - 1;
5779
+ return bitFloor(n.e / LOG_BASE) == k && n.c[k] % 2 != 0;
5780
+ }
5781
+
5782
+
5783
+ function toExponential(str, e) {
5784
+ return (str.length > 1 ? str.charAt(0) + '.' + str.slice(1) : str) +
5785
+ (e < 0 ? 'e' : 'e+') + e;
5786
+ }
5787
+
5788
+
5789
+ function toFixedPoint(str, e, z) {
5790
+ var len, zs;
5791
+
5792
+ // Negative exponent?
5793
+ if (e < 0) {
5794
+
5795
+ // Prepend zeros.
5796
+ for (zs = z + '.'; ++e; zs += z);
5797
+ str = zs + str;
5798
+
5799
+ // Positive exponent
5800
+ } else {
5801
+ len = str.length;
5802
+
5803
+ // Append zeros.
5804
+ if (++e > len) {
5805
+ for (zs = z, e -= len; --e; zs += z);
5806
+ str += zs;
5807
+ } else if (e < len) {
5808
+ str = str.slice(0, e) + '.' + str.slice(e);
5809
+ }
5810
+ }
5811
+
5812
+ return str;
5813
+ }
5814
+
5815
+
5816
+ // EXPORT
5817
+
5818
+
5819
+ var BigNumber = clone();
5820
+
5821
+ /**
5822
+ * Convert token amount to USD
5823
+ * @param {string|number} tokenAmount - The amount of tokens
5824
+ * @param {string|number} tokenPrice - The price of one token in USD
5825
+ * @returns {BigNumber} - The equivalent amount in USD
5826
+ */
5827
+ function convertTokenAmountToUSD(tokenAmount, tokenPrice) {
5828
+ if (!tokenAmount || !tokenPrice)
5829
+ return '0';
5830
+ const amount = new BigNumber(tokenAmount);
5831
+ const price = new BigNumber(tokenPrice);
5832
+ return amount.multipliedBy(price).decimalPlaces(4).toString();
5833
+ }
5834
+ /**
5835
+ * Convert USD to token amount
5836
+ * @param {string|number} usdAmount - The amount in USD
5837
+ * @param {string|number} tokenPrice - The price of one token in USD
5838
+ * @returns {BigNumber} - The equivalent amount of tokens
5839
+ */
5840
+ function convertUSDToTokenAmount(usdAmount, tokenPrice) {
5841
+ if (!usdAmount || !tokenPrice)
5842
+ return '0';
5843
+ const amount = new BigNumber(usdAmount);
5844
+ const price = new BigNumber(tokenPrice);
5845
+ return amount.dividedBy(price).toString();
5846
+ }
5847
+ const INTL_NUMBER_FORMATTER = new Intl.NumberFormat('en-US', {
5848
+ minimumFractionDigits: 0,
5849
+ maximumFractionDigits: 5,
5850
+ });
5851
+ /**
5852
+ * Formats a number to USD
5853
+ *
5854
+ * @param amount - The amount to format
5855
+ * @returns The formatted currency string
5856
+ */
5857
+ function formatUSD(amount) {
5858
+ return INTL_NUMBER_FORMATTER.format(amount);
5859
+ }
5860
+ function trimExtraDecimals(value, maxDecimals) {
5861
+ var _a;
5862
+ const split = value.split('.');
5863
+ if (split.length > 1) {
5864
+ const decimals = (_a = split[1]) !== null && _a !== void 0 ? _a : '';
5865
+ if (decimals.length > maxDecimals) {
5866
+ const newValue = `${split[0]}.${decimals.slice(0, maxDecimals)}`;
5867
+ return newValue;
5868
+ }
5869
+ }
5870
+ return value;
5871
+ }
5872
+
2915
5873
  const NumericInput = (_a) => {
2916
5874
  var _b;
2917
- var { onParsedValueChanged, initialValue = '', forcedUpdateValue, maxDecimals, balance = '0', tokenPrice = 0 } = _a, props = __rest(_a, ["onParsedValueChanged", "initialValue", "forcedUpdateValue", "maxDecimals", "balance", "tokenPrice"]);
5875
+ var { onParsedValueChanged, initialValue = '', forcedUpdateValue, maxDecimals, balance = '0', tokenPrice = 0, numericInputMode } = _a, props = __rest(_a, ["onParsedValueChanged", "initialValue", "forcedUpdateValue", "maxDecimals", "balance", "tokenPrice", "numericInputMode"]);
2918
5876
  const [inputValue, setInputValue] = React.useState(initialValue);
2919
5877
  // Probably a better way to handle this
2920
5878
  // This was introduce to handle the "MAX" button setting an amount
@@ -2953,26 +5911,12 @@ const NumericInput = (_a) => {
2953
5911
  var _a;
2954
5912
  try {
2955
5913
  const formattedInput = event.currentTarget.value
2956
- .replace(/[^0-9\.\,$%]/g, '')
5914
+ .replace(/[^0-9\.\,%]/g, '')
2957
5915
  .replace(',', '.');
2958
- if (formattedInput.includes('$') || formattedInput.includes('%')) {
5916
+ if (formattedInput.includes('%')) {
2959
5917
  let cleanedInput = formattedInput;
2960
- if (formattedInput.includes('$') && formattedInput.includes('%')) {
2961
- // only allow one of them
2962
- cleanedInput = formattedInput
2963
- .replaceAll('$', '')
2964
- .replaceAll('%', '')
2965
- .concat('%');
2966
- }
2967
- else if (formattedInput.includes('%')) {
2968
- // remove duplicates & always add % at the end
2969
- cleanedInput = formattedInput.replaceAll('%', '').concat('%');
2970
- }
2971
- else if (formattedInput.includes('$')) {
2972
- // Always add $ at the beginning
2973
- cleanedInput = formattedInput.replaceAll('$', '');
2974
- cleanedInput = `$${cleanedInput}`;
2975
- }
5918
+ // remove duplicates & always add % at the end
5919
+ cleanedInput = formattedInput.replaceAll('%', '').concat('%');
2976
5920
  setInputValue(cleanedInput);
2977
5921
  debouncePriceUpdate.cancel();
2978
5922
  return;
@@ -2988,6 +5932,11 @@ const NumericInput = (_a) => {
2988
5932
  }
2989
5933
  }
2990
5934
  }
5935
+ // Means the user is currently typing and will add decimal, no need to fetch or parse
5936
+ // example input: 0.00
5937
+ const isTypingDecimals = (formattedInput.includes('.') && formattedInput.endsWith('0')) ||
5938
+ formattedInput.endsWith('.') ||
5939
+ (formattedInput === '0' && (inputValue === '' || inputValue === '0'));
2991
5940
  // if input includes at least one character different than `0` (zero), `.` (dot), or `,` (comma)
2992
5941
  // means that user is typing a valid amount, for example: 1.02 or 0.005
2993
5942
  // if so, then update the input value and fetch the price
@@ -2995,12 +5944,7 @@ const NumericInput = (_a) => {
2995
5944
  setInputValue(formattedInput);
2996
5945
  debouncePriceUpdate(formattedInput);
2997
5946
  }
2998
- else if (
2999
- // Means the user is currently typing and will add decimal, no need to fetch or parse
3000
- // example input: 0.00
3001
- (formattedInput.includes('.') && formattedInput.endsWith('0')) ||
3002
- formattedInput.endsWith('.') ||
3003
- (formattedInput === '0' && (inputValue === '' || inputValue === '0'))) {
5947
+ else if (isTypingDecimals) {
3004
5948
  setInputValue(formattedInput);
3005
5949
  }
3006
5950
  else if (!isNaN(+formattedInput)) {
@@ -3011,6 +5955,19 @@ const NumericInput = (_a) => {
3011
5955
  else {
3012
5956
  setInputValue('');
3013
5957
  }
5958
+ if (numericInputMode === 'price' && !isTypingDecimals) {
5959
+ const usdAmount = Number(formattedInput);
5960
+ if (tokenPrice === 0 || usdAmount === 0) {
5961
+ setInputValue('');
5962
+ onParsedValueChanged('');
5963
+ }
5964
+ else if (usdAmount > 0) {
5965
+ const tokenAmount = convertUSDToTokenAmount(usdAmount, tokenPrice);
5966
+ setInputValue(usdAmount.toString());
5967
+ debouncePriceUpdate(tokenAmount);
5968
+ return;
5969
+ }
5970
+ }
3014
5971
  }
3015
5972
  catch (error) {
3016
5973
  setInputValue('');
@@ -3028,21 +5985,8 @@ const NumericInput = (_a) => {
3028
5985
  setInputValue(formattedValue);
3029
5986
  onParsedValueChanged(formattedValue);
3030
5987
  }
3031
- if (inputValue.includes('$')) {
3032
- const usdAmount = Number(inputValue.replace('$', ''));
3033
- if (tokenPrice === 0 || usdAmount === 0) {
3034
- setInputValue('0');
3035
- onParsedValueChanged('0');
3036
- }
3037
- else if (usdAmount > 0) {
3038
- const newValue = usdAmount / Number(tokenPrice);
3039
- const newValueFormatted = newValue.toFixed(4);
3040
- setInputValue(newValueFormatted);
3041
- onParsedValueChanged(newValueFormatted);
3042
- }
3043
- }
3044
5988
  };
3045
- return (jsxRuntime.jsx("form", { className: "tw-px-squid-m tw-pb-[15px] tw-pt-[5px]", onSubmit: handleSubmit, children: jsxRuntime.jsx("input", Object.assign({}, props, { onChange: handlePriceChanged, value: inputValue, type: "string", placeholder: (_b = props.placeholder) !== null && _b !== void 0 ? _b : '0', className: "tw-h-[55px] tw-w-full tw-rounded-squid-s tw-bg-transparent tw-px-squid-xs tw-py-squid-s tw-text-heading-small tw-font-heading-regular tw-text-grey-300 placeholder:tw-text-grey-600 hover:tw-bg-material-light-thin focus:tw-bg-material-light-thin focus:tw-text-royal-400 focus:tw-outline-none" })) }));
5989
+ return (jsxRuntime.jsxs("form", { className: "tw-relative tw-px-squid-m tw-pb-[15px] tw-pt-[5px] tw-text-heading-small tw-font-heading-regular", onSubmit: handleSubmit, children: [numericInputMode === 'price' && (jsxRuntime.jsx("span", { className: "tw-absolute tw-left-[30px] tw-top-[11px] tw-leading-[43px] tw-text-grey-600", children: "$" })), jsxRuntime.jsx("input", Object.assign({}, props, { onChange: handlePriceChanged, value: inputValue, type: "string", placeholder: (_b = props.placeholder) !== null && _b !== void 0 ? _b : '0', className: cn('tw-h-[55px] tw-w-full tw-rounded-squid-s tw-bg-transparent tw-px-squid-xs tw-py-squid-s tw-text-grey-300 placeholder:tw-text-grey-600 hover:tw-bg-material-light-thin focus:tw-bg-material-light-thin focus:tw-text-royal-400 focus:tw-outline-none', numericInputMode === 'price' && 'tw-pl-[33px]') }))] }));
3046
5990
  };
3047
5991
 
3048
5992
  // font size, line height, and letter spacing classes
@@ -3602,17 +6546,54 @@ function SwapInputsIcon() {
3602
6546
  return (jsxRuntime.jsxs("svg", { width: "20", height: "21", viewBox: "0 0 20 21", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("g", { clipPath: "url(#clip0_40_7936)", children: jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M20 10.5C20 16.0228 15.5228 20.5 10 20.5C4.47715 20.5 0 16.0228 0 10.5C0 4.97715 4.47715 0.5 10 0.5C15.5228 0.5 20 4.97715 20 10.5ZM8.38268 4.57612C8.75636 4.7309 9 5.09554 9 5.5V15.5C9 16.0523 8.55228 16.5 8 16.5C7.44772 16.5 7 16.0523 7 15.5V7.91421L5.70711 9.20711C5.31658 9.59763 4.68342 9.59763 4.29289 9.20711C3.90237 8.81658 3.90237 8.18342 4.29289 7.79289L7.29289 4.79289C7.57889 4.5069 8.00901 4.42134 8.38268 4.57612ZM11 15.5C11 15.9045 11.2436 16.2691 11.6173 16.4239C11.991 16.5787 12.4211 16.4931 12.7071 16.2071L15.7071 13.2071C16.0976 12.8166 16.0976 12.1834 15.7071 11.7929C15.3166 11.4024 14.6834 11.4024 14.2929 11.7929L13 13.0858V5.5C13 4.94771 12.5523 4.5 12 4.5C11.4477 4.5 11 4.94771 11 5.5V15.5Z", fill: "currentColor" }) }), jsxRuntime.jsx("defs", { children: jsxRuntime.jsx("clipPath", { id: "clip0_40_7936", children: jsxRuntime.jsx("rect", { width: "20", height: "20", fill: "white", transform: "translate(0 0.5)" }) }) })] }));
3603
6547
  }
3604
6548
 
6549
+ const buttonClassName = 'tw-flex tw-h-squid-l tw-items-center tw-gap-1.5 tw-rounded-squid-s tw-px-squid-xs';
3605
6550
  const interactiveChipClassName = 'hover:tw-bg-material-light-thin';
3606
- function SwapConfiguration({ priceImpactPercentage, amount, forcedAmount, swapAmountUsd = '0', balance = '0', tokenPrice = 0, isFetching = false, chain, token, direction, onAmountChange, onWalletButtonClick, onAssetsButtonClick, onBalanceButtonClick, onSwapAmountButtonClick, address, error, criticalPriceImpactPercentage = 5, emptyAddressLabel = 'Connect wallet', }) {
6551
+ function SwapConfiguration({ priceImpactPercentage, amount, forcedAmount: _forcedAmount, balance = '0', tokenPrice = 0, isFetching = false, chain, token, direction, onAmountChange, onWalletButtonClick, onAssetsButtonClick, onBalanceButtonClick, address, error, criticalPriceImpactPercentage = 5, emptyAddressLabel = 'Connect wallet', }) {
3607
6552
  var _a, _b;
6553
+ const [inputMode, setInputMode] = React.useState('token');
6554
+ const [tokenAmount, setTokenAmount] = React.useState('0');
6555
+ const forcedAmount = React.useMemo(() => {
6556
+ var _a, _b;
6557
+ if (_forcedAmount) {
6558
+ return _forcedAmount;
6559
+ }
6560
+ if (inputMode === 'token') {
6561
+ const amountLimitedToMaxDecimals = trimExtraDecimals(amount !== null && amount !== void 0 ? amount : '0', (_a = token === null || token === void 0 ? void 0 : token.decimals) !== null && _a !== void 0 ? _a : 18);
6562
+ return amountLimitedToMaxDecimals;
6563
+ }
6564
+ else if (inputMode === 'price') {
6565
+ const tokenAmountToUsd = convertTokenAmountToUSD(amount || 0, tokenPrice);
6566
+ const amountLimitedToMaxDecimals = trimExtraDecimals(tokenAmountToUsd, (_b = token === null || token === void 0 ? void 0 : token.decimals) !== null && _b !== void 0 ? _b : 18);
6567
+ return amountLimitedToMaxDecimals;
6568
+ }
6569
+ return '0';
6570
+ }, [_forcedAmount, inputMode, amount, tokenPrice, token === null || token === void 0 ? void 0 : token.decimals]);
6571
+ const swapAmountUsd = React.useMemo(() => {
6572
+ if (tokenPrice && forcedAmount) {
6573
+ return formatUSD(convertTokenAmountToUSD(forcedAmount, tokenPrice));
6574
+ }
6575
+ return '0';
6576
+ }, [forcedAmount, tokenPrice]);
3608
6577
  const priceImpactClass = ((_a = Number(priceImpactPercentage)) !== null && _a !== void 0 ? _a : 0) > Number(criticalPriceImpactPercentage)
3609
6578
  ? 'tw-text-status-negative'
3610
6579
  : 'tw-text-grey-300';
3611
6580
  const isBalanceChipInteractive = !!onBalanceButtonClick;
3612
6581
  const BalanceChipTag = isBalanceChipInteractive ? 'button' : 'div';
3613
- const isSwapAmountChipInteractive = !!onSwapAmountButtonClick;
3614
- const SwapAmountChipTag = isSwapAmountChipInteractive ? 'button' : 'div';
3615
- return (jsxRuntime.jsxs("section", { className: "tw-relative tw-h-[205px] tw-max-h-[205px] tw-w-[480px] tw-overflow-hidden tw-border-t tw-border-t-material-light-thin tw-bg-grey-900 tw-pb-squid-m", children: [jsxRuntime.jsx("header", { className: "tw-flex tw-items-center tw-gap-1 tw-px-squid-l tw-py-squid-xs tw-leading-5 tw-text-grey-300", children: jsxRuntime.jsxs("button", { onClick: onWalletButtonClick, className: "-tw-ml-squid-xs tw-flex tw-h-squid-l tw-items-center tw-gap-squid-xxs tw-rounded-squid-s tw-px-squid-xs tw-text-grey-600 hover:tw-bg-material-light-thin", children: [jsxRuntime.jsx(BodyText, { className: "tw-text-grey-500", size: "small", children: direction === 'from' ? 'Pay' : 'Receive' }), jsxRuntime.jsx(BodyText, { size: "small", children: ":" }), jsxRuntime.jsxs("div", { className: "tw-flex tw-items-center tw-gap-1", children: [jsxRuntime.jsx(BodyText, { size: "small", className: address ? 'tw-text-grey-300' : 'tw-text-royal-400', children: address ? address : emptyAddressLabel }), jsxRuntime.jsx(ChevronArrowIcon, { className: address ? 'tw-text-grey-600' : 'tw-text-royal-400' })] })] }) }), jsxRuntime.jsx("div", { className: "tw-px-squid-l", children: jsxRuntime.jsx(AssetsButton, { onClick: onAssetsButtonClick, chainImageUrl: chain === null || chain === void 0 ? void 0 : chain.iconUrl, tokenImageUrl: token === null || token === void 0 ? void 0 : token.iconUrl, tokenSymbol: token === null || token === void 0 ? void 0 : token.symbol, chainBgColor: chain === null || chain === void 0 ? void 0 : chain.bgColor, tokenBgColor: token === null || token === void 0 ? void 0 : token.bgColor, tokenTextColor: token === null || token === void 0 ? void 0 : token.textColor }) }), isFetching && (jsxRuntime.jsx("div", { className: "tw-absolute tw-bottom-4 tw-left-squid-l tw-z-10 tw-overflow-hidden", children: jsxRuntime.jsx("div", { className: "tw-h-[94px] tw-w-[1260px] tw-animate-move-loading-cover-to-right tw-bg-dark-cover" }) })), direction === 'from' ? (jsxRuntime.jsx(NumericInput, { balance: balance, tokenPrice: tokenPrice, forcedUpdateValue: forcedAmount, initialValue: amount, maxDecimals: (_b = token === null || token === void 0 ? void 0 : token.decimals) !== null && _b !== void 0 ? _b : 18, onParsedValueChanged: (value) => onAmountChange === null || onAmountChange === void 0 ? void 0 : onAmountChange(value) })) : (jsxRuntime.jsx("div", { className: cn('tw-w-full tw-px-squid-m tw-pb-[15px] tw-pt-[5px]', isFetching && 'tw-opacity-50'), children: jsxRuntime.jsx("div", { className: "tw-flex tw-h-[55px] tw-w-full tw-items-center tw-rounded-squid-s tw-bg-transparent tw-px-squid-xs tw-py-squid-s tw-text-heading-small tw-font-heading-regular tw-text-grey-300", children: jsxRuntime.jsx("span", { children: amount }) }) })), !(token === null || token === void 0 ? void 0 : token.iconUrl) ? null : (jsxRuntime.jsxs("footer", { className: cn('tw-flex tw-h-squid-m tw-max-h-squid-m tw-items-center tw-justify-between tw-gap-2 tw-px-squid-m tw-text-grey-500', isFetching && 'tw-opacity-50'), children: [error ? (jsxRuntime.jsx("div", { className: "tw-px-squid-xs", children: jsxRuntime.jsx(ErrorMessage, { message: error.message }) })) : (jsxRuntime.jsxs(SwapAmountChipTag, { onClick: onSwapAmountButtonClick, className: cn('tw-flex tw-h-squid-l tw-items-center tw-gap-1.5 tw-rounded-squid-s tw-px-squid-xs', isSwapAmountChipInteractive && interactiveChipClassName), children: [jsxRuntime.jsx(SwapInputsIcon, {}), jsxRuntime.jsx(UsdAmount, { usdAmount: swapAmountUsd }), priceImpactPercentage && direction === 'to' ? (jsxRuntime.jsxs("span", { className: clsx('tw-flex tw-items-center', priceImpactClass), children: [jsxRuntime.jsx(ArrowTriangle, {}), jsxRuntime.jsx(CaptionText, { bold: true, children: priceImpactPercentage.toString().concat('%') })] })) : null] })), jsxRuntime.jsxs(BalanceChipTag, { onClick: onBalanceButtonClick, className: cn('tw-flex tw-h-squid-l tw-items-center tw-gap-1.5 tw-rounded-squid-s tw-px-squid-xs', isBalanceChipInteractive && interactiveChipClassName), children: [jsxRuntime.jsx(CaptionText, { className: "tw-opacity-66", children: "Balance" }), jsxRuntime.jsxs(CaptionText, { children: [balance, " ", token.symbol] }), jsxRuntime.jsx(MaxIcon, {})] })] }))] }));
6582
+ const handleSwapAmountButtonClick = React.useCallback(() => {
6583
+ if (inputMode === 'token') {
6584
+ setInputMode('price');
6585
+ }
6586
+ else {
6587
+ setInputMode('token');
6588
+ }
6589
+ }, [inputMode]);
6590
+ const tokenAmountFormatted = React.useMemo(() => {
6591
+ return formatUSD(tokenAmount || 0);
6592
+ }, [tokenAmount]);
6593
+ return (jsxRuntime.jsxs("section", { className: "tw-relative tw-h-[205px] tw-max-h-[205px] tw-w-[480px] tw-overflow-hidden tw-border-t tw-border-t-material-light-thin tw-bg-grey-900 tw-pb-squid-m", children: [jsxRuntime.jsx("header", { className: "tw-flex tw-items-center tw-gap-1 tw-px-squid-l tw-py-squid-xs tw-leading-5 tw-text-grey-300", children: jsxRuntime.jsxs("button", { onClick: onWalletButtonClick, className: "-tw-ml-squid-xs tw-flex tw-h-squid-l tw-items-center tw-gap-squid-xxs tw-rounded-squid-s tw-px-squid-xs tw-text-grey-600 hover:tw-bg-material-light-thin", children: [jsxRuntime.jsx(BodyText, { className: "tw-text-grey-500", size: "small", children: direction === 'from' ? 'Pay' : 'Receive' }), jsxRuntime.jsx(BodyText, { size: "small", children: ":" }), jsxRuntime.jsxs("div", { className: "tw-flex tw-items-center tw-gap-1", children: [jsxRuntime.jsx(BodyText, { size: "small", className: address ? 'tw-text-grey-300' : 'tw-text-royal-400', children: address ? address : emptyAddressLabel }), jsxRuntime.jsx(ChevronArrowIcon, { className: address ? 'tw-text-grey-600' : 'tw-text-royal-400' })] })] }) }), jsxRuntime.jsx("div", { className: "tw-px-squid-l", children: jsxRuntime.jsx(AssetsButton, { onClick: onAssetsButtonClick, chainImageUrl: chain === null || chain === void 0 ? void 0 : chain.iconUrl, tokenImageUrl: token === null || token === void 0 ? void 0 : token.iconUrl, tokenSymbol: token === null || token === void 0 ? void 0 : token.symbol, chainBgColor: chain === null || chain === void 0 ? void 0 : chain.bgColor, tokenBgColor: token === null || token === void 0 ? void 0 : token.bgColor, tokenTextColor: token === null || token === void 0 ? void 0 : token.textColor }) }), isFetching && (jsxRuntime.jsx("div", { className: "tw-absolute tw-bottom-4 tw-left-squid-l tw-z-10 tw-overflow-hidden", children: jsxRuntime.jsx("div", { className: "tw-h-[94px] tw-w-[1260px] tw-animate-move-loading-cover-to-right tw-bg-dark-cover" }) })), direction === 'from' ? (jsxRuntime.jsx(NumericInput, { balance: balance, tokenPrice: tokenPrice, forcedUpdateValue: forcedAmount, initialValue: amount, maxDecimals: (_b = token === null || token === void 0 ? void 0 : token.decimals) !== null && _b !== void 0 ? _b : 18, onParsedValueChanged: (value) => {
6594
+ setTokenAmount(value);
6595
+ onAmountChange === null || onAmountChange === void 0 ? void 0 : onAmountChange(value);
6596
+ }, numericInputMode: inputMode })) : (jsxRuntime.jsx("div", { className: cn('tw-w-full tw-px-squid-m tw-pb-[15px] tw-pt-[5px]', isFetching && 'tw-opacity-50'), children: jsxRuntime.jsx("div", { className: "tw-flex tw-h-[55px] tw-w-full tw-items-center tw-rounded-squid-s tw-bg-transparent tw-px-squid-xs tw-py-squid-s tw-text-heading-small tw-font-heading-regular tw-text-grey-300", children: jsxRuntime.jsx("span", { children: amount }) }) })), !(token === null || token === void 0 ? void 0 : token.iconUrl) ? null : (jsxRuntime.jsxs("footer", { className: cn('tw-flex tw-h-squid-m tw-max-h-squid-m tw-items-center tw-justify-between tw-gap-2 tw-px-squid-m tw-text-grey-500', isFetching && 'tw-opacity-50'), children: [error ? (jsxRuntime.jsx("div", { className: "tw-px-squid-xs", children: jsxRuntime.jsx(ErrorMessage, { message: error.message }) })) : (jsxRuntime.jsxs("button", { onClick: handleSwapAmountButtonClick, className: cn(buttonClassName, interactiveChipClassName), children: [jsxRuntime.jsx(SwapInputsIcon, {}), inputMode === 'token' ? (jsxRuntime.jsx(UsdAmount, { usdAmount: swapAmountUsd })) : (jsxRuntime.jsxs("span", { className: "tw-text-grey-500", children: [jsxRuntime.jsx(CaptionText, { children: tokenAmountFormatted }), ' ', jsxRuntime.jsx(CaptionText, { className: "tw-opacity-66", children: token.symbol })] })), priceImpactPercentage && direction === 'to' ? (jsxRuntime.jsxs("span", { className: clsx('tw-flex tw-items-center', priceImpactClass), children: [jsxRuntime.jsx(ArrowTriangle, {}), jsxRuntime.jsx(CaptionText, { bold: true, children: priceImpactPercentage.toString().concat('%') })] })) : null] })), jsxRuntime.jsxs(BalanceChipTag, { onClick: onBalanceButtonClick, className: cn(buttonClassName, isBalanceChipInteractive && interactiveChipClassName), children: [jsxRuntime.jsx(CaptionText, { className: "tw-opacity-66", children: "Balance" }), jsxRuntime.jsxs(CaptionText, { children: [balance, " ", token.symbol] }), jsxRuntime.jsx(MaxIcon, {})] })] }))] }));
3616
6597
  }
3617
6598
 
3618
6599
  function SwapProgressViewHeader({ title, description, }) {