A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic.
      In all examples below, var and semicolons are not shown, and
      if a commented-out value is in quotes it means toString has
      been called on the preceding expression.
    
Big( value ) ⇒ Big
    valueInfinity, NaN and hexadecimal literal
        strings, e.g. '0xff', are not valid.Returns a new instance of a Big number object.
      Throws NaN on an invalid value.
    
x = new Big(9)                       // '9'
y = new Big(x)                       // '9'
Big(435.345)                         // 'new' is optional
new Big('5032485723458348569331745.33434346346912144534543')
new Big('4.321e+4')                  // '43210'
new Big('-735.0918e-430')            // '-7.350918e-428'
    
      number : integer, 0 to 1e+6 inclusive
      Default value: 20
    
      The maximum number of decimal places of the results of operations
      involving division.
      It is relevant only to the div and sqrt methods,
      and the pow method when the exponent is negative.
    
      The value will be checked for validity when one of the above methods is
      called.
      !Big.DP! will be thrown if the value is found to be invalid.
    
Big.DP = 40
      number : 0, 1, 2 or 3
      Default value: 1
    
      The rounding mode used in the above operations and by
      round,
      toExponential,
      toFixed and
      toPrecision.
    
| Value | Description | BigDecimal equivalent | 
|---|---|---|
| 0 | Rounds towards zero. I.e. truncate, no rounding. | ROUND_DOWN | 
| 1 | Rounds towards nearest neighbour. If equidistant, rounds away from zero. | ROUND_HALF_UP | 
| 2 | Rounds towards nearest neighbour. If equidistant, rounds towards even neighbour. | ROUND_HALF_EVEN | 
| 3 | Rounds away from zero. | ROUND_UP | 
      The value will be checked for validity when one of the above methods is
      called.
      !Big.RM! will be thrown if the value is found to be invalid.
    
Big.RM = 0
The methods inherited by a Big number instance from its constructor's prototype object.
A Big number is immutable in the sense that it is not changed by its methods.
.abs() ⇒ BigReturns a Big number whose value is the absolute value, i.e. the magnitude, of this Big number.
x = new Big(-0.8) x.abs() // '0.8'
.cmp( n ) ⇒ number
    n : number|string|Big
| Returns | ||
|---|---|---|
| 1 | If the value of this Big number is greater than the value of n | |
| -1 | If the value of this Big number is less than the value of n | |
| 0 | If this Big number and nhave the same value | |
Throws NaN if n is invalid.
x = new Big(6) y = new Big(5) x.cmp(y) // 1 y.cmp(x.minus(1)) // 0
.div(n) ⇒ Bign : number|string|Big
      Returns a Big number whose value is the value of this Big number divided
      by n.
    
      If the result has more fraction digits than is specified by
      Big.DP, it will be rounded to
      Big.DP decimal places using rounding mode
      Big.RM.
    
      Throws NaN if n is invalid.
      Throws ±Infinity on division by zero.
      Throws NaN on division of zero by zero.
    
x = new Big(355) y = new Big(113) x.div(y) // '3.14159292035398230088' Big.DP = 2 x.div(y) // '3.14' x.div(5) // '71'
.eq(n) ⇒ booleann : number|string|Big
      Returns true if the value of this Big equals the value
      of n, otherwise returns false.
    
Throws NaN if n is invalid.
0 === 1e-324               // true
x = new Big(0)
x.eq('1e-324')             // false
Big(-0).eq(x)              // true  ( -0 === 0 )
    .gt(n) ⇒ boolean
    n : number|string|Big
      Returns true if the value of this Big is greater than
      the value of n, otherwise returns false.
    
Throws NaN if n is invalid.
0.1 > 0.3 - 0.2 // true x = new Big(0.1) x.gt(Big(0.3).minus(0.2)) // false Big(0).gt(x) // false
.gte(n) ⇒ boolean
    n : number|string|Big
      Returns true if the value of this Big is greater than
      or equal to the value of n, otherwise returns
      false.
    
Throws NaN if n is invalid.
0.3 - 0.2 >= 0.1 // false x = new Big(0.3).minus(0.2) x.gte(0.1) // true Big(1).gte(x) // true
.lt(n) ⇒ boolean
    n : number|string|Big
      Returns true if the value of this Big is less than the
      value of n, otherwise returns false.
    
Throws NaN if n is invalid.
0.3 - 0.2 < 0.1 // true x = new Big(0.3).minus(0.2) x.lt(0.1) // false Big(0).lt(x) // true
.lte(n) ⇒ boolean
    n : number|string|Big
      Returns true if the value of this Big is less than or
      equal to the value of n, otherwise returns
      false.
    
Throws NaN if n is invalid.
0.1 <= 0.3 - 0.2 // false x = new Big(0.1) x.lte(Big(0.3).minus(0.2)) // true Big(-1).lte(x) // true
.minus(n) ⇒ Big
    n : number|string|Big
      Returns a Big number whose value is the value of this Big number minus
      n.
    
Throws NaN if n is invalid.
0.3 - 0.1 // 0.19999999999999998 x = new Big(0.3) x.minus(0.1) // '0.2'
.mod(n) ⇒ Bign : number|string|Big
      Returns a Big number whose value is the value of this Big number modulo
      n, i.e. the integer remainder of dividing this Big number by
      n.
    
The result will have the same sign as this Big number, and it will match that of Javascript's % operator (within the limits of its precision) and BigDecimal's remainder method.
Throws NaN if n is negative or otherwise invalid.
1 % 0.9 // 0.09999999999999998 x = new Big(1) x.mod(0.9) // '0.1'
.plus(n) ⇒ Big
    n : number|string|Big
      Returns a Big number whose value is the value of this Big number plus
      n.
    
Throws NaN if n is invalid.
0.1 + 0.2 // 0.30000000000000004 x = new Big(0.1) y = x.plus(0.2) // '0.3' Big(0.7).plus(x).plus(y) // '1'
.pow( exp ) ⇒ Big
    
      exp : number : integer, -1e+6 to 1e+6 inclusive
    
      Returns a Big number whose value is the value of this Big number raised to
      the power exp.
    
      If exp is negative and the result has more fraction digits
      than is specified by Big.DP, it will be
      rounded to Big.DP decimal places using
      rounding mode Big.RM.
    
Throws !pow! if exp is invalid.
Note: High value exponents may cause this method to be slow to return.
Math.pow(0.7, 2) // 0.48999999999999994 x = new Big(0.7) x.pow(2) // '0.49' Big.DP = 20 Big(3).pow(-2) // '0.11111111111111111111' new Big(123.456).pow(1000).toString().length // 5099 new Big(2).pow(1e+6) // Time taken (Node.js): 9 minutes 34 secs.
.round( [dp [, rm]] )
      ⇒ Big
    
      dp : number : integer, 0 to 1e+6 inclusive
      
      rm : number : 0, 1 or 2
    
      Returns a Big number whose value is the value of this Big number rounded
      using rounding mode rm to a maximum of dp
      decimal places.
    
      if dp is omitted or is null or undefined, the
      return value is n rounded to a whole number.
      if rm is omitted or is null or
      undefined, the current Big.RM setting is
      used.
    
      Throws !round! if dp is invalid.
      Throws !Big.RM! if rm is invalid.
    
x = 123.45 Math.round(x) // 123 y = new Big(x) y.round() // '123' y.round(2) // '123.45' y.round(10) // '123.45' y.round(1, 0) // '123.4' y.round(1, 1) // '123.5' y.round(1, 2) // '123.4' y.round(1, 3) // '123.5' y // '123.45'
.sqrt() ⇒ BigReturns a Big number whose value is the square root of this Big number.
      If the result has more fraction digits than is specified by
      Big.DP, it will be rounded to
      Big.DP decimal places using rounding mode
      Big.RM.
    
Throws NaN if this Big number is negative.
x = new Big(16) x.sqrt() // '4' y = new Big(3) y.sqrt() // '1.73205080756887729353'
.times(n) ⇒ Big
    n : number|string|Big
      Returns a Big number whose value is the value of this Big number times
      n.
    
Throws NaN if n is invalid.
0.6 * 3                    // 1.7999999999999998
x = new Big(0.6)
y = x.times(3)             // '1.8'
Big('7e+500').times(y)     // '1.26e+501'
    .toExponential( [dp] ) ⇒
      string
    dp : number : integer, 0 to 1e+6 inclusive
      Returns a string representing the value of this Big number in exponential
      notation to a fixed number of decimal places dp.
    
      If the value of this Big number in exponential notation has more digits to
      the right of the decimal point than is specified by dp, the
      return value will be rounded to dp decimal places using
      rounding mode Big.RM.
    
      If the value of this Big number in exponential notation has fewer digits
      to the right of the decimal point than is specified by dp,
      the return value will be appended with zeros accordingly.
    
      If dp is omitted, or is null or undefined, the
      number of digits after the decimal point defaults to the minimum number of
      digits necessary to represent the value exactly.
    
Throws !toExp! if dp is invalid.
x = 45.6 y = new Big(x) x.toExponential() // '4.56e+1' y.toExponential() // '4.56e+1' x.toExponential(0) // '5e+1' y.toExponential(0) // '5e+1' x.toExponential(1) // '4.6e+1' y.toExponential(1) // '4.6e+1' x.toExponential(3) // '4.560e+1' y.toExponential(3) // '4.560e+1'
.toFixed( [dp] ) ⇒
      string
    
      dp : number : integer, 0 to 1e+6 inclusive
    
      Returns a string representing the value of this Big number in normal
      notation to a fixed number of decimal places dp.
    
      If the value of this Big number in normal notation has more digits to the
      right of the decimal point than is specified by dp, the
      return value will be rounded to dp decimal places using
      rounding mode Big.RM.
    
      If the value of this Big number in normal notation has fewer fraction
      digits then is specified by dp, the return value will be
      appended with zeros accordingly.
    
      Unlike Number.prototype.toFixed, which returns
      exponential notation if a number is greater or equal to 1021,
      this method will always return normal notation.
    
      If dp is omitted, or is null or
      undefined, then the return value is simply the value in normal notation.
      This is  also unlike Number.prototype.toFixed, which returns
      the value to zero decimal places.
    
Throws !toFix! if dp is invalid.
x = 45.6 y = new Big(x) x.toFixed() // '46' y.toFixed() // '45.6' y.toFixed(0) // '46' x.toFixed(3) // '45.600' y.toFixed(3) // '45.600'
.toPrecision( [sd] ) ⇒
      string
    sd : number : integer, 1 to 1e+6 inclusive
      Returns a string representing the value of this Big number to the
      specified number of significant digits sd.
    
      If the value of this Big number has more digits than is specified by
      sd, the return value will be rounded to sd
      significant digits using rounding mode
      Big.RM.
    
      If the value of this Big number has fewer digits than is specified by
      sd, the return value will be appended with zeros accordingly.
    
      If sd is less than the number of digits necessary to
      represent the integer part of the value in normal notation, then
      exponential notation is used.
    
      If sd is omitted, or is null or undefined, then
      the return value is the same as .toString().
    
Throws !toPre! if sd is invalid.
x = 45.6 y = new Big(x) x.toPrecision() // '45.6' y.toPrecision() // '45.6' x.toPrecision(1) // '5e+1' y.toPrecision(1) // '5e+1' x.toPrecision(5) // '45.600' y.toPrecision(5) // '45.600'
.toString() ⇒ string
    Returns a string representing the value of this Big number.
If this Big number has a positive exponent that is equal to or greater than 21, or a negative exponent equal to or less than -7, then exponential notation is returned.
      The point at which toString returns exponential rather than
      normal notation can be adjusted by changing the value of
      TO_EXP_POS and TO_EXP_NEG in the
      EDITABLE DEFAULTS section at the top of the source code file. By default,
      Big numbers correspond to Javascript's number type in this regard.
    
x = new Big('9.99e+20')
x.toString()               // '999000000000000000000'
y = new Big('1E21')
x.toString()               // '1e+21'
    .valueOf() ⇒ string
    
      As toString.
    
x = new Big('177.7e+457')
x.valueOf()                // '1.777e+459'
    A Big number is an object with three properties:
| Property | Description | Type | Value | 
|---|---|---|---|
| c | coefficient* | number [] | Array of single digits | 
| e | exponent | number | Integer, -1e+6 to 1e+6 inclusive | 
| s | sign | number | -1 or 1 | 
*significand
      The value of a Big number is stored in a normalised decimal floating point
      format which corresponds to the value's toExponential form,
      with the decimal point to be positioned after the most significant
      (left-most) digit of the coefficient.
    
Note that, as with Javascript numbers, the original exponent and fractional trailing zeros are not preserved.
x = new Big(0.123)                 // '0.123'
x.toExponential()                  // '1.23e-1'
x.c                                // '1,2,3'
x.e                                // -1
x.s                                // 1
y = new Number(-123.4567000e+2)    // '-12345.67'
y.toExponential()                  // '-1.234567e+4'
z = new Big('-123.4567000e+2')     // '-12345.67'
z.toExponential()                  // '-1.234567e+4'
z.c                                // '1,2,3,4,5,6,7'
z.e                                // 4
z.s                                // -1
    
      A Big number is mutable in the sense that the value of its properties can
      be changed.
      For example, to rapidly shift a value by a power of 10:
    
x = new Big('1234.000')    // '1234'
x.toExponential()          // '1.234e+3'
x.c                        // '1,2,3,4'
x.e                        // 3
x.e = -5
x                          // '0.00001234'
    If changing the coefficient array directly, which is not recommended, be careful to avoid leading or trailing zeros (unless zero itself is being represented).
Minus zero is a valid Big number value, but like Javascript numbers the minus sign is not shown.
x = new Number(-0) // 0 1 / x == -Infinity // true y = new Big(-0) // '0' y.c // '0' [0].toString() y.e // 0 y.s // -1
      The errors that are thrown are instances of Error with
      name BigError and message as
      shown in the table below.
    
| Method(s) | Error message | Thrown on | 
|---|---|---|
| 
            Big | NaN | Invalid number | 
| div | ±Infinity | Division by zero | 
| NaN | Division of zero by zero | |
| !Big.DP! | Invalid Big.DP | |
| !Big.RM! | Invalid Big.RM | |
| mod | NaN | Modulo zero | 
| pow | !pow! | Invalid exponent | 
| !Big.DP! | Invalid Big.DP | |
| !Big.RM! | Invalid Big.RM | |
| round | !round! | Invalid dp | 
| !Big.RM! | Invalid rm/Big.RM | |
| sqrt | NaN | Negative number | 
| !Big.DP! | Invalid Big.DP | |
| !Big.RM! | Invalid Big.RM | |
| toExponential | !toExp! | Invalid dp | 
| !Big.RM! | Invalid Big.RM | |
| toFixed | !toFix! | Invalid dp | 
| !Big.RM! | Invalid Big.RM | |
| toPrecision | !toPre! | Invalid sd | 
| !Big.RM! | Invalid Big.RM | 
Many arbitrary-precision libraries retain trailing fractional zeros as they can indicate the precision of a value. This can be useful but the results of arithmetic operations can be misleading.
x = new BigDecimal("1.0")
y = new BigDecimal("1.1000")
z = x.add(y)                      // 2.1000
x = new BigDecimal("1.20")
y = new BigDecimal("3.45000")
z = x.multiply(y)                 // 4.1400000
    To specify the precision of a value is to imply that the value lies within a certain range.
      In the first example, x has a value of 1.0. The trailing zero
      shows the precision of the value, implying that the value is in the range
      0.95 to 1.05. Similarly, the precision indicated by the trailing zeros of
      y indicates that the value is in the range 1.09995 to
      1.10005. If we add the two lowest values in the ranges we get 0.95 +
      1.09995 = 2.04995 and if we add the two highest values we get 1.05 +
      1.10005 = 2.15005, so the range of the result of the addition implied by
      the precision of its operands is 2.04995 to 2.15005. The result given by
      BigDecimal of 2.1000 however, indicates that the value is in the range
      2.09995 to 2.10005 and therefore the precision implied by its trailing
      zeros is misleading.
    
In the second example, the true range is 4.122744 to 4.157256 yet the BigDecimal answer of 4.1400000 indicates a range of 4.13999995 to 4.14000005. Again, the precision implied by the trailing zeros is misleading.
      This library, like binary floating-point and most calculators, does not
      retain trailing fractional zeros.
      Instead, the toExponential, toFixed and
      toPrecision methods enable trailing zeros to be added if and
      when required.