You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
and `math.unit('5cm')` use the `number` configuration setting.
58
-
59
-
Note that `math.sqrt(4)` will always return the number `2` regardless of
60
-
the `number` configuration, because the numeric type can be determined from
61
-
the input value.
62
-
63
-
Available values are: `'number'` (default), `'BigNumber'`, `'bigint'`, or `'Fraction'`.
64
-
[BigNumbers](../datatypes/bignumbers.md) have higher precision than the default numbers of JavaScript,
65
-
[bigint](../datatypes/bigints.md) can represent large integer numbers,
66
-
and [`Fractions`](../datatypes/fractions.md) store values in terms of a numerator and
67
-
denominator.
68
-
69
-
-`numberFallback`. When `number` is configured for example with value `'bigint'`,
70
-
and a value cannot be represented as `bigint` like in `math.evaluate('2.3')`,
71
-
the value will be parsed in the type configured with `numberFallback`.
72
-
Available values: `'number'` (default) or `'BigNumber'`.
73
-
74
-
-`precision`. The maximum number of significant digits for BigNumbers.
75
-
This setting only applies to BigNumbers, not to numbers.
76
-
Default value is `64`.
77
-
78
-
-`predictable`. Predictable output type of functions. When true, output type
79
-
depends only on the input types. When false (default), output type can vary
80
-
depending on input values. For example `math.sqrt(-4)` returns `complex('2i')` when
81
-
predictable is false, and returns `NaN` when true.
82
-
Predictable output can be needed when programmatically handling the result of
83
-
a calculation, but can be inconvenient for users when evaluating dynamic
84
-
equations.
85
-
86
-
-`randomSeed`. Set this option to seed pseudo random number generation, making it deterministic. The pseudo random number generator is reset with the seed provided each time this option is set. For example, setting it to `'a'` will cause `math.random()` to return `0.43449421599986604` upon the first call after setting the option every time. Set to `null` to seed the pseudo random number generator with a random seed. Default value is `null`.
87
-
88
-
-`legacySubset`. When set to `true`, the `subset` function behaves as in earlier versions of math.js: retrieving a subset where the index size contains only one element, returns the value itself. When set to `false` (default), `subset` eliminates dimensions from the result only if the index dimension is a scalar; if the index dimension is a range, matrix, or array, the dimensions are preserved. This option is helpful for maintaining compatibility with legacy code that depends on the previous behavior.
89
-
41
+
The following configuration options and sub-options are available.
42
+
43
+
- {string} `number`. When the type of a numeric value is not otherwise
44
+
specifically determined, either in parsing or in computation, the type
45
+
given by this option is tried first. For example, when set to `Fraction`,
46
+
`math.evaluate('0.15 + 02')` will return the exact rational number
47
+
`math.fraction(7, 20)` and `math.sum([])` will return `math.fraction(0)`.
48
+
Note there are separate backup options for parsing (`parse.numberFallback`)
49
+
and computation (`compute.numberApproximate`) when this `number` type is
50
+
not appropriate, see below. The currently supported values for this
51
+
option are `number` (the default), `BigNumber` (which can support higher
52
+
precision than the built-in JavaScript number type), `Fraction`, `bigint`
53
+
(which can represent arbitrarily large integer numbers, but not
54
+
fractions or decimals).
55
+
56
+
- compute:
57
+
- {number} `defaultAbsTol`. The minimum absolute difference used to test
58
+
equality between two compared values of floating-point (inexact) types.
59
+
This value is used by all relational functions. Default value is `1e-15`.
60
+
61
+
- {number} `defaultRelTol`. The minimum relative difference used to test
62
+
equality between two compared values of floating-point (inexact) types.
63
+
This value is used by all relational functions. Default value is `1e-12`.
64
+
65
+
- {string} `numberApproximate`. The type to use for floating-point
66
+
(approximate real) values as output when the type is not uniquely
67
+
determined by the input types (e.g., the square root of a bigint or the
68
+
value of a mathematical constant). Default value is `number`.
69
+
70
+
- {string|null} `randomSeed`. Set this option to seed pseudo-random number
71
+
generation, making it deterministic. The pseudo-random number generator is
72
+
reset with the seed provided each time this option is set to a new value.
73
+
For example, setting it to `'a'` will always cause `math.random()` to
74
+
return `0.43449421599986604` on its next call. Set to `null` to seed the
75
+
pseudo random number generator with a random seed. Default value is `null`.
76
+
77
+
- {boolean} `uniformType`. When this option is true, the output type of any
78
+
function depends only on the input types. When false, the output type can
79
+
vary depending on input values within the same type. For example,
80
+
`math.sqrt(-4)` returns `math.complex('2i')` when this option is false,
81
+
and returns `NaN` when true. Predictable output can be needed when
82
+
programmatically handling the result of a calculation, but can be
83
+
inconvenient for users when evaluating dynamic equations. Default
84
+
value is `false`.
85
+
86
+
- BigNumber:
87
+
- {number} `precision`. The maximum number of significant digits kept
88
+
for values of BigNumber type. Note that increasing it will not affect
89
+
previously computed BigNumbers, but will retain the larger number
90
+
of digits for future computations. Default value is `64`.
91
+
92
+
- Matrix:
93
+
- {string} `defaultType`. The data type used to represent a matrix output
94
+
by any function, when that type is not uniquely determined by the input
95
+
types. Whenever possible, the type of matrix output from functions is
96
+
determined from the function input: An array as input will return an
97
+
Array and a Matrix as input will return a Matrix (which is also returned
98
+
in case of mixed-type matrix inputs). However, some functions, such as
99
+
`math.identity(n)`, have no matrix parameter. In such cases, the type
100
+
of the output is determined by this option, including in the case of
101
+
the `[...]` matrix-creating operator in mathjs expressions. Currently
102
+
supported types are `Array` and `Matrix`, the default.
103
+
104
+
- parse:
105
+
- {string} `numberFallback`. When the top-level `number` option is
106
+
configured, for example, with value `'bigint'`, and a literal value in
107
+
an expression, such as `'2.3'`, cannot be represented as that type,
108
+
the value will instead be parsed into the type specified by
109
+
`numberFallback`. The currently supported values are `BigNumber`,
110
+
`Fraction`, and `number`, the default.
111
+
112
+
- compatibility:
113
+
- {boolean} `subset`. When set to `true`, the `subset` function behaves
114
+
as in earlier versions of math.js: retrieving a subset where the index
115
+
size contains only one element, returns the value itself. When set to
116
+
`false`, `subset` eliminates dimensions from the result only if the
117
+
index dimension is a scalar; if the index dimension is a range, matrix,
118
+
or array, the dimensions are preserved. This option is helpful for
119
+
maintaining compatibility with legacy code that depends on the
120
+
previous behavior. It is, however, deprecated and will be removed in some
121
+
future version of mathjs.
90
122
91
123
## Examples
92
124
@@ -98,25 +130,27 @@ This section shows a number of configuration examples.
98
130
import { create, all } from'mathjs'
99
131
100
132
constconfig= {
101
-
matrix:'Array'// Choose 'Matrix' (default) or 'Array'
133
+
compute: {
134
+
Matrix: { defaultType:'Array' } // Choose 'Matrix' (default) or 'Array'
135
+
}
102
136
}
103
137
constmath=create(all, config)
104
138
105
139
// range will output an Array
106
140
math.range(0, 4) // Array [0, 1, 2, 3]
107
141
108
142
// change the configuration from Arrays to Matrices
109
-
math.config({
110
-
matrix:'Matrix'// Choose 'Matrix' (default) or 'Array'
You may notice, however, in this setup the parser might encounter a non-integer
34
+
literal like `2.5`, that can't be represented by a bigint. In such cases where
35
+
the type specified by the top-level `number` configuration option is not
36
+
appropriate, backup types can also be configured: the parser will use the
37
+
`parse.numberFallback` option, that can be `number`, `BigNumber`, or
38
+
`Fraction`; and computations (and similar items, like the values of physical
39
+
constants) will use the `compute.numberApproximate` option, that can be
40
+
`number` or `BigNumber`.
41
+
31
42
## Support
32
43
33
44
All basic arithmetic functions in math.js support `bigint`. Since `bigint` can only hold integer values, it is not applicable to for example trigonometric functions. When using a `bigint` in a function that does not support it, like `sqrt`, it will convert the `bigint` into a regular `number` and then execute the function:
0 commit comments