UNPKG

3.75 kBMarkdownView Raw
1# BigNumbers
2
3For calculations with an arbitrary precision, math.js supports a `BigNumber`
4datatype. BigNumber support is powered by
5[decimal.js](https://github.com/MikeMcl/decimal.js/).
6
7## Usage
8
9A BigNumber can be created using the function `bignumber`:
10
11```js
12math.bignumber('2.3e+500') // BigNumber, 2.3e+500
13```
14
15Most functions can determine the type of output from the type of input:
16a number as input will return a number as output, a BigNumber as input returns
17a BigNumber as output. Functions which cannot determine the type of output
18from the input (for example `math.evaluate`) use the default number type `number`,
19which can be configured when instantiating math.js. To configure the use of
20BigNumbers instead of [numbers](numbers.md) by default, configure math.js like:
21
22```js
23math.config({
24 number: 'BigNumber', // Default type of number:
25 // 'number' (default), 'BigNumber', or 'Fraction'
26 precision: 64 // Number of significant digits for BigNumbers
27})
28
29// use math
30math.evaluate('0.1 + 0.2') // BigNumber, 0.3
31```
32
33The default precision for BigNumber is 64 digits, and can be configured with
34the option `precision`.
35
36
37## Support
38
39Most functions in math.js support BigNumbers, but not all of them.
40For example the function `random` doesn't support BigNumbers.
41
42
43## Round-off errors
44
45Calculations with BigNumber are much slower than calculations with Number,
46but they can be executed with an arbitrary precision. By using a higher
47precision, it is less likely that round-off errors occur:
48
49```js
50// round-off errors with numbers
51math.add(0.1, 0.2) // Number, 0.30000000000000004
52math.divide(0.3, 0.2) // Number, 1.4999999999999998
53
54// no round-off errors with BigNumbers :)
55math.add(math.bignumber(0.1), math.bignumber(0.2)) // BigNumber, 0.3
56math.divide(math.bignumber(0.3), math.bignumber(0.2)) // BigNumber, 1.5
57```
58
59
60## Limitations
61
62It's important to realize that BigNumbers do not solve *all* problems related
63to precision and round-off errors. Numbers with an infinite number of digits
64cannot be represented with a regular number nor a BigNumber. Though a BigNumber
65can store a much larger number of digits, the amount of digits remains limited
66if only to keep calculations fast enough to remain practical.
67
68```js
69const one = math.bignumber(1)
70const three = math.bignumber(3)
71const third = math.divide(one, three)
72console.log(third.toString())
73// outputs 0.3333333333333333333333333333333333333333333333333333333333333333
74
75const ans = math.multiply(third, three)
76console.log(ans.toString())
77// outputs 0.9999999999999999999999999999999999999999999999999999999999999999
78// this should be 1 again, but `third` is rounded to a limited number of digits 3
79```
80
81
82## Conversion
83
84BigNumbers can be converted to numbers and vice versa using the functions
85`number` and `bignumber`. When converting a BigNumber to a number, the high
86precision of the BigNumber will be lost. When a BigNumber is too large to be represented
87as Number, it will be initialized as `Infinity`.
88
89```js
90// converting numbers and BigNumbers
91const a = math.number(0.3) // number, 0.3
92const b = math.bignumber(a) // BigNumber, 0.3
93const c = math.number(b) // number, 0.3
94
95// exceeding the maximum of a number
96const d = math.bignumber('1.2e500') // BigNumber, 1.2e+500
97const e = math.number(d) // number, Infinity
98
99// loosing precision when converting to number
100const f = math.bignumber('0.2222222222222222222') // BigNumber, 0.2222222222222222222
101const g = math.number(f) // number, 0.2222222222222222
102```