1 | import { factory } from '../../../utils/factory.js';
|
2 | import { deepMap } from '../../../utils/collection.js';
|
3 | var name = 'bignumber';
|
4 | var dependencies = ['typed', 'BigNumber'];
|
5 | export var createBignumber = /* #__PURE__ */factory(name, dependencies, (_ref) => {
|
6 | var {
|
7 | typed,
|
8 | BigNumber
|
9 | } = _ref;
|
10 |
|
11 | /**
|
12 | * Create a BigNumber, which can store numbers with arbitrary precision.
|
13 | * When a matrix is provided, all elements will be converted to BigNumber.
|
14 | *
|
15 | * Syntax:
|
16 | *
|
17 | * math.bignumber(x)
|
18 | *
|
19 | * Examples:
|
20 | *
|
21 | * 0.1 + 0.2 // returns number 0.30000000000000004
|
22 | * math.bignumber(0.1) + math.bignumber(0.2) // returns BigNumber 0.3
|
23 | *
|
24 | *
|
25 | * 7.2e500 // returns number Infinity
|
26 | * math.bignumber('7.2e500') // returns BigNumber 7.2e500
|
27 | *
|
28 | * See also:
|
29 | *
|
30 | * boolean, complex, index, matrix, string, unit
|
31 | *
|
32 | * @param {number | string | Fraction | BigNumber | Array | Matrix | boolean | null} [value] Value for the big number,
|
33 | * 0 by default.
|
34 | * @returns {BigNumber} The created bignumber
|
35 | */
|
36 | return typed('bignumber', {
|
37 | '': function _() {
|
38 | return new BigNumber(0);
|
39 | },
|
40 | number: function number(x) {
|
41 | // convert to string to prevent errors in case of >15 digits
|
42 | return new BigNumber(x + '');
|
43 | },
|
44 | string: function string(x) {
|
45 | var match = x.match(/(0[box][0-9a-fA-F]*)i([0-9]*)/);
|
46 |
|
47 | if (match) {
|
48 | // x has a word size suffix
|
49 | var size = match[2];
|
50 | var n = BigNumber(match[1]);
|
51 | var twoPowSize = new BigNumber(2).pow(Number(size));
|
52 |
|
53 | if (n.gt(twoPowSize.sub(1))) {
|
54 | throw new SyntaxError("String \"".concat(x, "\" is out of range"));
|
55 | }
|
56 |
|
57 | var twoPowSizeSubOne = new BigNumber(2).pow(Number(size) - 1);
|
58 |
|
59 | if (n.gte(twoPowSizeSubOne)) {
|
60 | return n.sub(twoPowSize);
|
61 | } else {
|
62 | return n;
|
63 | }
|
64 | }
|
65 |
|
66 | return new BigNumber(x);
|
67 | },
|
68 | BigNumber: function BigNumber(x) {
|
69 | // we assume a BigNumber is immutable
|
70 | return x;
|
71 | },
|
72 | Fraction: function Fraction(x) {
|
73 | return new BigNumber(x.n).div(x.d).times(x.s);
|
74 | },
|
75 | null: function _null(x) {
|
76 | return new BigNumber(0);
|
77 | },
|
78 | 'Array | Matrix': function ArrayMatrix(x) {
|
79 | return deepMap(x, this);
|
80 | }
|
81 | });
|
82 | }); |
\ | No newline at end of file |