-
Notifications
You must be signed in to change notification settings - Fork 415
Home
##What is the difference between big.js, bignumber.js and decimal.js?##
big.js is a minimalist arbitrary-precision library. It is the simplest and smallest of the three, less than half the size of bignumber.js and with only half the number of methods. It doesn't accept NaN or Infinity as legitimate values, it doesn't work with numbers of other bases, and the runtime configuration opions are limited to setting the number of decimal places and rounding mode of operations involving division. (As of v3.1.0, the exponent values at which toString
produces exponential notation can also be set.)
Big.DP = 7;
Big.RM = 4; // round half-up
var x = new Big(5);
x.div(3).toString(); // '1.6666667'
Like the other two libraries, it includes toExponential
, toFixed
and toPrecision
methods. Unlike the others, it stores values in base 10 which makes it easy to change the properties of a Big
number directly if required
var x = new Big('123.456');
x.c; // [1, 2, 3, 4, 5, 6] (coefficient, i.e. significand/mantissa)
x.e; // 2 (exponent)
x.s; // 1 (sign)
x.e = -3;
x.toString(); // '0.00123456'
bignumber.js and decimal.js store values in a higher base than big.js so they will be faster when performing operations on values with a large number of digits. They also support NaN and Infinity and can work with values in other bases (2 to 64 inclusive).
var x = new BigNumber('ff.8', 16);
x.toString(); // '255.5'
x.toString(16); // 'ff.8'
bignumber.js and decimal.js also have various runtime configuration options, such as the ability to set the exponent value at which toString produces exponential notation.
var x = new Decimal(987654321);
x.toString(); // '987654321'
Decimal.config({ toExpPos: 4 });
x.toString(); // '9.87654321e+8'
decimal.js was orginally intended to be version two of bignumber.js but I decided to make them separate libraries. The main difference between them is that with decimal.js precision is specified in terms of significant digits instead of decimal places, and all calculations are rounded to the precision (similar to Python's decimal module) rather than just those involving division.
Bignumber.config({ DECIMAL_PLACES: 3, ROUNDING_MODE; 1 });
var x = new BigNumber('123.456789');
x.plus(1).toString(); // '124.456789'
Decimal.config({ precision: 7, rounding: 4 });
var y = new Decimal('123.456789');
y.plus(1).toString(); // '124.4568'
This could be considered to make bignumber.js easier to use than decimal.js, and perhaps more suitable for financial applications, because the user doesn't need to worry about losing precision unless an operation involving division is used.
Conversely, decimal.js may be considered better for more scientific applications as it can handle very small or large values more efficiently: for example, it does not have the limitation of bignumber.js that if adding a BigNumber value with a small exponent to one with a large exponent, bignumber.js will attempt to perform the operation to full precision which may involve the creation of a massive array, and consequently cause the operation to hang.
decimal.js also adds exp
, ln
, log
and random
functions, and supports non-integer powers, which bignumber.js does not. These additions make decimal.js significantly larger than bignumber.js.
One thing that bignumber.js has that decimal.js currently does not, is support for binary, octal and hexadecimal prefixes
var x = new BigNumber('0b1101');
x.toString() // '13'
big.js: minimalist; easy-to-use; precision specified in decimal places; precision applied to division only; 4 rounding modes.
bignumber.js: configuration options; NaN; Infinity; precision specified in decimal places; precision applied to division only; random numbers; base conversion; 9 rounding modes; modulo modes; modular exponentiation.
decimal.js: configuration options; NaN; Infinity; non-integer powers, exp, ln, log; trigonometric functions; precision specified in significant digits; precision always applied; random numbers; 9 rounding modes; modulo modes; binary, octal, and hexadecimal; binary exponential notation.