1 | import { factory } from '../../../utils/factory'
|
2 | import { deepMap } from '../../../utils/collection'
|
3 |
|
4 | const name = 'bignumber'
|
5 | const dependencies = ['typed', 'BigNumber']
|
6 |
|
7 | export const createBignumber = /* #__PURE__ */ factory(name, dependencies, ({ typed, BigNumber }) => {
|
8 | /**
|
9 | * Create a BigNumber, which can store numbers with arbitrary precision.
|
10 | * When a matrix is provided, all elements will be converted to BigNumber.
|
11 | *
|
12 | * Syntax:
|
13 | *
|
14 | * math.bignumber(x)
|
15 | *
|
16 | * Examples:
|
17 | *
|
18 | * 0.1 + 0.2 // returns number 0.30000000000000004
|
19 | * math.bignumber(0.1) + math.bignumber(0.2) // returns BigNumber 0.3
|
20 | *
|
21 | *
|
22 | * 7.2e500 // returns number Infinity
|
23 | * math.bignumber('7.2e500') // returns BigNumber 7.2e500
|
24 | *
|
25 | * See also:
|
26 | *
|
27 | * boolean, complex, index, matrix, string, unit
|
28 | *
|
29 | * @param {number | string | Fraction | BigNumber | Array | Matrix | boolean | null} [value] Value for the big number,
|
30 | * 0 by default.
|
31 | * @returns {BigNumber} The created bignumber
|
32 | */
|
33 | const bignumber = typed('bignumber', {
|
34 | '': function () {
|
35 | return new BigNumber(0)
|
36 | },
|
37 |
|
38 | number: function (x) {
|
39 | // convert to string to prevent errors in case of >15 digits
|
40 | return new BigNumber(x + '')
|
41 | },
|
42 |
|
43 | string: function (x) {
|
44 | return new BigNumber(x)
|
45 | },
|
46 |
|
47 | BigNumber: function (x) {
|
48 | // we assume a BigNumber is immutable
|
49 | return x
|
50 | },
|
51 |
|
52 | Fraction: function (x) {
|
53 | return new BigNumber(x.n).div(x.d).times(x.s)
|
54 | },
|
55 |
|
56 | null: function (x) {
|
57 | return new BigNumber(0)
|
58 | },
|
59 |
|
60 | 'Array | Matrix': function (x) {
|
61 | return deepMap(x, bignumber)
|
62 | }
|
63 | })
|
64 |
|
65 | return bignumber
|
66 | })
|