mathjs 12.3.0 → 12.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. package/HISTORY.md +27 -0
  2. package/lib/browser/math.js +1 -1
  3. package/lib/browser/math.js.LICENSE.txt +2 -2
  4. package/lib/browser/math.js.map +1 -1
  5. package/lib/cjs/entry/configReadonly.js +1 -1
  6. package/lib/cjs/entry/dependenciesAny/dependenciesHelpClass.generated.js +2 -2
  7. package/lib/cjs/entry/dependenciesNumber/dependenciesHelpClass.generated.js +2 -2
  8. package/lib/cjs/entry/impureFunctionsAny.generated.js +14 -14
  9. package/lib/cjs/entry/impureFunctionsNumber.generated.js +1 -1
  10. package/lib/cjs/entry/pureFunctionsAny.generated.js +11 -11
  11. package/lib/cjs/expression/Help.js +21 -4
  12. package/lib/cjs/expression/node/OperatorNode.js +2 -1
  13. package/lib/cjs/expression/transform/utils/compileInlineExpression.js +5 -4
  14. package/lib/cjs/function/string/bin.js +3 -3
  15. package/lib/cjs/function/string/format.js +3 -3
  16. package/lib/cjs/function/string/hex.js +3 -3
  17. package/lib/cjs/function/string/oct.js +3 -3
  18. package/lib/cjs/header.js +2 -2
  19. package/lib/cjs/type/unit/physicalConstants.js +1 -1
  20. package/lib/cjs/utils/bignumber/formatter.js +17 -27
  21. package/lib/cjs/utils/map.js +174 -14
  22. package/lib/cjs/utils/number.js +75 -33
  23. package/lib/cjs/utils/scope.js +4 -10
  24. package/lib/cjs/version.js +1 -1
  25. package/lib/esm/entry/configReadonly.js +1 -1
  26. package/lib/esm/entry/dependenciesAny/dependenciesHelpClass.generated.js +2 -2
  27. package/lib/esm/entry/dependenciesNumber/dependenciesHelpClass.generated.js +2 -2
  28. package/lib/esm/entry/impureFunctionsAny.generated.js +16 -16
  29. package/lib/esm/entry/impureFunctionsNumber.generated.js +1 -1
  30. package/lib/esm/entry/pureFunctionsAny.generated.js +12 -12
  31. package/lib/esm/expression/Help.js +21 -4
  32. package/lib/esm/expression/node/OperatorNode.js +2 -1
  33. package/lib/esm/expression/transform/utils/compileInlineExpression.js +5 -4
  34. package/lib/esm/function/string/bin.js +3 -3
  35. package/lib/esm/function/string/format.js +3 -3
  36. package/lib/esm/function/string/hex.js +3 -3
  37. package/lib/esm/function/string/oct.js +3 -3
  38. package/lib/esm/type/unit/physicalConstants.js +1 -1
  39. package/lib/esm/utils/bignumber/formatter.js +19 -28
  40. package/lib/esm/utils/map.js +97 -1
  41. package/lib/esm/utils/number.js +76 -34
  42. package/lib/esm/utils/scope.js +5 -11
  43. package/lib/esm/version.js +1 -1
  44. package/package.json +12 -12
  45. package/types/index.d.ts +30 -18
@@ -3,10 +3,10 @@ import { clone } from '../utils/object.js';
3
3
  import { format } from '../utils/string.js';
4
4
  import { factory } from '../utils/factory.js';
5
5
  var name = 'Help';
6
- var dependencies = ['parse'];
6
+ var dependencies = ['evaluate'];
7
7
  export var createHelpClass = /* #__PURE__ */factory(name, dependencies, _ref => {
8
8
  var {
9
- parse
9
+ evaluate
10
10
  } = _ref;
11
11
  /**
12
12
  * Documentation object
@@ -55,14 +55,26 @@ export var createHelpClass = /* #__PURE__ */factory(name, dependencies, _ref =>
55
55
  }
56
56
  if (doc.examples) {
57
57
  desc += 'Examples:\n';
58
- var scope = {};
58
+
59
+ // after evaluating the examples, we restore config in case the examples
60
+ // did change the config.
61
+ var configChanged = false;
62
+ var originalConfig = evaluate('config()');
63
+ var scope = {
64
+ config: newConfig => {
65
+ configChanged = true;
66
+ return evaluate('config(newConfig)', {
67
+ newConfig
68
+ });
69
+ }
70
+ };
59
71
  for (var i = 0; i < doc.examples.length; i++) {
60
72
  var expr = doc.examples[i];
61
73
  desc += ' ' + expr + '\n';
62
74
  var res = void 0;
63
75
  try {
64
76
  // note: res can be undefined when `expr` is an empty string
65
- res = parse(expr).compile().evaluate(scope);
77
+ res = evaluate(expr, scope);
66
78
  } catch (e) {
67
79
  res = e;
68
80
  }
@@ -73,6 +85,11 @@ export var createHelpClass = /* #__PURE__ */factory(name, dependencies, _ref =>
73
85
  }
74
86
  }
75
87
  desc += '\n';
88
+ if (configChanged) {
89
+ evaluate('config(originalConfig)', {
90
+ originalConfig
91
+ });
92
+ }
76
93
  }
77
94
  if (doc.mayThrow && doc.mayThrow.length) {
78
95
  desc += 'Throws: ' + doc.mayThrow.join(', ') + '\n\n';
@@ -1,6 +1,7 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import { isNode, isConstantNode, isOperatorNode, isParenthesisNode } from '../../utils/is.js';
3
3
  import { map } from '../../utils/array.js';
4
+ import { createSubScope } from '../../utils/scope.js';
4
5
  import { escape } from '../../utils/string.js';
5
6
  import { getSafeProperty, isSafeMethod } from '../../utils/customs.js';
6
7
  import { getAssociativity, getPrecedence, isAssociativeWith, properties } from '../operators.js';
@@ -289,7 +290,7 @@ export var createOperatorNode = /* #__PURE__ */factory(name, dependencies, _ref
289
290
  // "raw" evaluation
290
291
  var rawArgs = this.args;
291
292
  return function evalOperatorNode(scope, args, context) {
292
- return fn(rawArgs, math, scope);
293
+ return fn(rawArgs, math, createSubScope(scope, args));
293
294
  };
294
295
  } else if (evalArgs.length === 1) {
295
296
  var evalArg0 = evalArgs[0];
@@ -1,11 +1,11 @@
1
1
  import { isSymbolNode } from '../../../utils/is.js';
2
- import { createSubScope } from '../../../utils/scope.js';
2
+ import { PartitionedMap } from '../../../utils/map.js';
3
3
 
4
4
  /**
5
5
  * Compile an inline expression like "x > 0"
6
6
  * @param {Node} expression
7
7
  * @param {Object} math
8
- * @param {Object} scope
8
+ * @param {Map} scope
9
9
  * @return {function} Returns a function with one argument which fills in the
10
10
  * undefined variable (like "x") and evaluates the expression
11
11
  */
@@ -20,10 +20,11 @@ export function compileInlineExpression(expression, math, scope) {
20
20
 
21
21
  // create a test function for this equation
22
22
  var name = symbol.name; // variable name
23
- var subScope = createSubScope(scope);
23
+ var argsScope = new Map();
24
+ var subScope = new PartitionedMap(scope, argsScope, new Set([name]));
24
25
  var eq = expression.compile();
25
26
  return function inlineExpression(x) {
26
- subScope.set(name, x);
27
+ argsScope.set(name, x);
27
28
  return eq.evaluate(subScope);
28
29
  };
29
30
  }
@@ -19,8 +19,8 @@ var dependencies = ['typed', 'format'];
19
19
  * oct
20
20
  * hex
21
21
  *
22
- * @param {number} value Value to be stringified
23
- * @param {number} wordSize Optional word size (see `format`)
22
+ * @param {number | BigNumber} value Value to be stringified
23
+ * @param {number | BigNumber} wordSize Optional word size (see `format`)
24
24
  * @return {string} The formatted value
25
25
  */
26
26
  export var createBin = factory(name, dependencies, _ref => {
@@ -34,7 +34,7 @@ export var createBin = factory(name, dependencies, _ref => {
34
34
  notation: 'bin'
35
35
  });
36
36
  },
37
- 'number | BigNumber, number': function numberBigNumberNumber(n, wordSize) {
37
+ 'number | BigNumber, number | BigNumber': function numberBigNumberNumberBigNumber(n, wordSize) {
38
38
  return format(n, {
39
39
  notation: 'bin',
40
40
  wordSize
@@ -42,7 +42,7 @@ export var createFormat = /* #__PURE__ */factory(name, dependencies, _ref => {
42
42
  * - `'bin'`, `'oct'`, or `'hex'`
43
43
  * Format the number using binary, octal, or hexadecimal notation.
44
44
  * For example `'0b1101'` and `'0x10fe'`.
45
- * - `wordSize: number`
45
+ * - `wordSize: number | BigNumber`
46
46
  * The word size in bits to use for formatting in binary, octal, or
47
47
  * hexadecimal notation. To be used only with `'bin'`, `'oct'`, or `'hex'`
48
48
  * values for `notation` option. When this option is defined the value
@@ -50,7 +50,7 @@ export var createFormat = /* #__PURE__ */factory(name, dependencies, _ref => {
50
50
  * size and the size suffix is appended to the output.
51
51
  * For example `format(-1, {notation: 'hex', wordSize: 8}) === '0xffi8'`.
52
52
  * Default value is undefined.
53
- * - `precision: number`
53
+ * - `precision: number | BigNumber`
54
54
  * Limit the number of digits of the formatted value.
55
55
  * For regular numbers, must be a number between `0` and `16`.
56
56
  * For bignumbers, the maximum depends on the configured precision,
@@ -126,6 +126,6 @@ export var createFormat = /* #__PURE__ */factory(name, dependencies, _ref => {
126
126
  */
127
127
  return typed(name, {
128
128
  any: formatString,
129
- 'any, Object | function | number': formatString
129
+ 'any, Object | function | number | BigNumber': formatString
130
130
  });
131
131
  });
@@ -18,8 +18,8 @@ var dependencies = ['typed', 'format'];
18
18
  * oct
19
19
  * bin
20
20
  *
21
- * @param {number} value Value to be stringified
22
- * @param {number} wordSize Optional word size (see `format`)
21
+ * @param {number | BigNumber} value Value to be stringified
22
+ * @param {number | BigNumber} wordSize Optional word size (see `format`)
23
23
  * @return {string} The formatted value
24
24
  */
25
25
  export var createHex = factory(name, dependencies, _ref => {
@@ -33,7 +33,7 @@ export var createHex = factory(name, dependencies, _ref => {
33
33
  notation: 'hex'
34
34
  });
35
35
  },
36
- 'number | BigNumber, number': function numberBigNumberNumber(n, wordSize) {
36
+ 'number | BigNumber, number | BigNumber': function numberBigNumberNumberBigNumber(n, wordSize) {
37
37
  return format(n, {
38
38
  notation: 'hex',
39
39
  wordSize
@@ -19,8 +19,8 @@ var dependencies = ['typed', 'format'];
19
19
  * bin
20
20
  * hex
21
21
  *
22
- * @param {number} value Value to be stringified
23
- * @param {number} wordSize Optional word size (see `format`)
22
+ * @param {number | BigNumber} value Value to be stringified
23
+ * @param {number | BigNumber} wordSize Optional word size (see `format`)
24
24
  * @return {string} The formatted value
25
25
  */
26
26
 
@@ -35,7 +35,7 @@ export var createOct = factory(name, dependencies, _ref => {
35
35
  notation: 'oct'
36
36
  });
37
37
  },
38
- 'number | BigNumber, number': function numberBigNumberNumber(n, wordSize) {
38
+ 'number | BigNumber, number | BigNumber': function numberBigNumberNumberBigNumber(n, wordSize) {
39
39
  return format(n, {
40
40
  notation: 'oct',
41
41
  wordSize
@@ -44,7 +44,7 @@ export var createAvogadro = /* #__PURE__ */unitFactory('avogadro', '6.02214076e2
44
44
  export var createBoltzmann = /* #__PURE__ */unitFactory('boltzmann', '1.380649e-23', 'J K^-1');
45
45
  export var createFaraday = /* #__PURE__ */unitFactory('faraday', '96485.33212331001', 'C mol^-1');
46
46
  export var createFirstRadiation = /* #__PURE__ */unitFactory('firstRadiation', '3.7417718521927573e-16', 'W m^2');
47
- // export const createSpectralRadiance = /* #__PURE__ */ unitFactory('spectralRadiance', '1.1910429723971881e-16', 'W m^2 sr^-1') // TODO spectralRadiance
47
+ // TODO spectralRadiance = 1.1910429723971881e-16 W m^2 sr^-1
48
48
  export var createLoschmidt = /* #__PURE__ */unitFactory('loschmidt', '2.686780111798444e25', 'm^-3');
49
49
  export var createGasConstant = /* #__PURE__ */unitFactory('gasConstant', '8.31446261815324', 'J K^-1 mol^-1');
50
50
  export var createMolarPlanckConstant = /* #__PURE__ */unitFactory('molarPlanckConstant', '3.990312712893431e-10', 'J s mol^-1');
@@ -1,4 +1,5 @@
1
- import { isInteger } from '../number.js';
1
+ import { isBigNumber, isNumber } from '../is.js';
2
+ import { isInteger, normalizeFormatOptions } from '../number.js';
2
3
 
3
4
  /**
4
5
  * Formats a BigNumber in a given base
@@ -119,7 +120,7 @@ function formatBigNumberToBase(n, base, size) {
119
120
  * format(12400, {notation: 'engineering'}) // returns '12.400e+3'
120
121
  *
121
122
  * @param {BigNumber} value
122
- * @param {Object | Function | number} [options]
123
+ * @param {Object | Function | number | BigNumber} [options]
123
124
  * @return {string} str The formatted value
124
125
  */
125
126
  export function format(value, options) {
@@ -132,30 +133,11 @@ export function format(value, options) {
132
133
  if (!value.isFinite()) {
133
134
  return value.isNaN() ? 'NaN' : value.gt(0) ? 'Infinity' : '-Infinity';
134
135
  }
135
-
136
- // default values for options
137
- var notation = 'auto';
138
- var precision;
139
- var wordSize;
140
- if (options !== undefined) {
141
- // determine notation from options
142
- if (options.notation) {
143
- notation = options.notation;
144
- }
145
-
146
- // determine precision from options
147
- if (typeof options === 'number') {
148
- precision = options;
149
- } else if (options.precision !== undefined) {
150
- precision = options.precision;
151
- }
152
- if (options.wordSize) {
153
- wordSize = options.wordSize;
154
- if (typeof wordSize !== 'number') {
155
- throw new Error('Option "wordSize" must be a number');
156
- }
157
- }
158
- }
136
+ var {
137
+ notation,
138
+ precision,
139
+ wordSize
140
+ } = normalizeFormatOptions(options);
159
141
 
160
142
  // handle the various notations
161
143
  switch (notation) {
@@ -175,8 +157,8 @@ export function format(value, options) {
175
157
  {
176
158
  // determine lower and upper bound for exponential notation.
177
159
  // TODO: implement support for upper and lower to be BigNumbers themselves
178
- var lowerExp = options && options.lowerExp !== undefined ? options.lowerExp : -3;
179
- var upperExp = options && options.upperExp !== undefined ? options.upperExp : 5;
160
+ var lowerExp = _toNumberOrDefault(options === null || options === void 0 ? void 0 : options.lowerExp, -3);
161
+ var upperExp = _toNumberOrDefault(options === null || options === void 0 ? void 0 : options.upperExp, 5);
180
162
 
181
163
  // handle special case zero
182
164
  if (value.isZero()) return '0';
@@ -249,4 +231,13 @@ export function toExponential(value, precision) {
249
231
  */
250
232
  export function toFixed(value, precision) {
251
233
  return value.toFixed(precision);
234
+ }
235
+ function _toNumberOrDefault(value, defaultValue) {
236
+ if (isNumber(value)) {
237
+ return value;
238
+ } else if (isBigNumber(value)) {
239
+ return value.toNumber();
240
+ } else {
241
+ return defaultValue;
242
+ }
252
243
  }
@@ -14,7 +14,7 @@ export class ObjectWrappingMap {
14
14
  this.wrappedObject = object;
15
15
  }
16
16
  keys() {
17
- return Object.keys(this.wrappedObject);
17
+ return Object.keys(this.wrappedObject).values();
18
18
  }
19
19
  get(key) {
20
20
  return getSafeProperty(this.wrappedObject, key);
@@ -26,6 +26,102 @@ export class ObjectWrappingMap {
26
26
  has(key) {
27
27
  return hasSafeProperty(this.wrappedObject, key);
28
28
  }
29
+ entries() {
30
+ return mapIterator(this.keys(), key => [key, this.get(key)]);
31
+ }
32
+ forEach(callback) {
33
+ for (var key of this.keys()) {
34
+ callback(this.get(key), key, this);
35
+ }
36
+ }
37
+ delete(key) {
38
+ delete this.wrappedObject[key];
39
+ }
40
+ clear() {
41
+ for (var key of this.keys()) {
42
+ this.delete(key);
43
+ }
44
+ }
45
+ get size() {
46
+ return Object.keys(this.wrappedObject).length;
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Create a map with two partitions: a and b.
52
+ * The set with bKeys determines which keys/values are read/written to map b,
53
+ * all other values are read/written to map a
54
+ *
55
+ * For example:
56
+ *
57
+ * const a = new Map()
58
+ * const b = new Map()
59
+ * const p = new PartitionedMap(a, b, new Set(['x', 'y']))
60
+ *
61
+ * In this case, values `x` and `y` are read/written to map `b`,
62
+ * all other values are read/written to map `a`.
63
+ */
64
+ export class PartitionedMap {
65
+ /**
66
+ * @param {Map} a
67
+ * @param {Map} b
68
+ * @param {Set} bKeys
69
+ */
70
+ constructor(a, b, bKeys) {
71
+ this.a = a;
72
+ this.b = b;
73
+ this.bKeys = bKeys;
74
+ }
75
+ get(key) {
76
+ return this.bKeys.has(key) ? this.b.get(key) : this.a.get(key);
77
+ }
78
+ set(key, value) {
79
+ if (this.bKeys.has(key)) {
80
+ this.b.set(key, value);
81
+ } else {
82
+ this.a.set(key, value);
83
+ }
84
+ return this;
85
+ }
86
+ has(key) {
87
+ return this.b.has(key) || this.a.has(key);
88
+ }
89
+ keys() {
90
+ return new Set([...this.a.keys(), ...this.b.keys()])[Symbol.iterator]();
91
+ }
92
+ entries() {
93
+ return mapIterator(this.keys(), key => [key, this.get(key)]);
94
+ }
95
+ forEach(callback) {
96
+ for (var key of this.keys()) {
97
+ callback(this.get(key), key, this);
98
+ }
99
+ }
100
+ delete(key) {
101
+ return this.bKeys.has(key) ? this.b.delete(key) : this.a.delete(key);
102
+ }
103
+ clear() {
104
+ this.a.clear();
105
+ this.b.clear();
106
+ }
107
+ get size() {
108
+ return [...this.keys()].length;
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Create a new iterator that maps over the provided iterator, applying a mapping function to each item
114
+ */
115
+ function mapIterator(it, callback) {
116
+ return {
117
+ next: () => {
118
+ var n = it.next();
119
+ return n.done ? n : {
120
+ value: callback(n.value),
121
+ done: false
122
+ };
123
+ }
124
+ };
29
125
  }
30
126
 
31
127
  /**
@@ -1,4 +1,4 @@
1
- import { isNumber } from './is.js';
1
+ import { isBigNumber, isNumber, isObject } from './is.js';
2
2
 
3
3
  /**
4
4
  * @typedef {{sign: '+' | '-' | '', coefficients: number[], exponent: number}} SplitValue
@@ -21,7 +21,7 @@ export function isInteger(value) {
21
21
  * @param {number} x
22
22
  * @returns {number}
23
23
  */
24
- export var sign = /* #__PURE__ */Math.sign || function (x) {
24
+ export var sign = Math.sign || function (x) {
25
25
  if (x > 0) {
26
26
  return 1;
27
27
  } else if (x < 0) {
@@ -36,7 +36,7 @@ export var sign = /* #__PURE__ */Math.sign || function (x) {
36
36
  * @param {number} x
37
37
  * @returns {number}
38
38
  */
39
- export var log2 = /* #__PURE__ */Math.log2 || function log2(x) {
39
+ export var log2 = Math.log2 || function log2(x) {
40
40
  return Math.log(x) / Math.LN2;
41
41
  };
42
42
 
@@ -45,7 +45,7 @@ export var log2 = /* #__PURE__ */Math.log2 || function log2(x) {
45
45
  * @param {number} x
46
46
  * @returns {number}
47
47
  */
48
- export var log10 = /* #__PURE__ */Math.log10 || function log10(x) {
48
+ export var log10 = Math.log10 || function log10(x) {
49
49
  return Math.log(x) / Math.LN10;
50
50
  };
51
51
 
@@ -54,7 +54,7 @@ export var log10 = /* #__PURE__ */Math.log10 || function log10(x) {
54
54
  * @param {number} x
55
55
  * @returns {number}
56
56
  */
57
- export var log1p = /* #__PURE__ */Math.log1p || function (x) {
57
+ export var log1p = Math.log1p || function (x) {
58
58
  return Math.log(x + 1);
59
59
  };
60
60
 
@@ -67,7 +67,7 @@ export var log1p = /* #__PURE__ */Math.log1p || function (x) {
67
67
  * @param {number} x
68
68
  * @returns {number} Returns the cubic root of x
69
69
  */
70
- export var cbrt = /* #__PURE__ */Math.cbrt || function cbrt(x) {
70
+ export var cbrt = Math.cbrt || function cbrt(x) {
71
71
  if (x === 0) {
72
72
  return x;
73
73
  }
@@ -91,7 +91,7 @@ export var cbrt = /* #__PURE__ */Math.cbrt || function cbrt(x) {
91
91
  * @param {number} x
92
92
  * @return {number} res
93
93
  */
94
- export var expm1 = /* #__PURE__ */Math.expm1 || function expm1(x) {
94
+ export var expm1 = Math.expm1 || function expm1(x) {
95
95
  return x >= 2e-4 || x <= -2e-4 ? Math.exp(x) - 1 : x + x * x / 2 + x * x * x / 6;
96
96
  };
97
97
 
@@ -234,30 +234,11 @@ export function format(value, options) {
234
234
  } else if (isNaN(value)) {
235
235
  return 'NaN';
236
236
  }
237
-
238
- // default values for options
239
- var notation = 'auto';
240
- var precision;
241
- var wordSize;
242
- if (options) {
243
- // determine notation from options
244
- if (options.notation) {
245
- notation = options.notation;
246
- }
247
-
248
- // determine precision from options
249
- if (isNumber(options)) {
250
- precision = options;
251
- } else if (isNumber(options.precision)) {
252
- precision = options.precision;
253
- }
254
- if (options.wordSize) {
255
- wordSize = options.wordSize;
256
- if (typeof wordSize !== 'number') {
257
- throw new Error('Option "wordSize" must be a number');
258
- }
259
- }
260
- }
237
+ var {
238
+ notation,
239
+ precision,
240
+ wordSize
241
+ } = normalizeFormatOptions(options);
261
242
 
262
243
  // handle the various notations
263
244
  switch (notation) {
@@ -275,7 +256,7 @@ export function format(value, options) {
275
256
  return formatNumberToBase(value, 16, wordSize);
276
257
  case 'auto':
277
258
  // remove trailing zeros after the decimal point
278
- return toPrecision(value, precision, options && options).replace(/((\.\d*?)(0+))($|e)/, function () {
259
+ return toPrecision(value, precision, options).replace(/((\.\d*?)(0+))($|e)/, function () {
279
260
  var digits = arguments[2];
280
261
  var e = arguments[4];
281
262
  return digits !== '.' ? digits + e : e;
@@ -285,6 +266,49 @@ export function format(value, options) {
285
266
  }
286
267
  }
287
268
 
269
+ /**
270
+ * Normalize format options into an object:
271
+ * {
272
+ * notation: string,
273
+ * precision: number | undefined,
274
+ * wordSize: number | undefined
275
+ * }
276
+ */
277
+ export function normalizeFormatOptions(options) {
278
+ // default values for options
279
+ var notation = 'auto';
280
+ var precision;
281
+ var wordSize;
282
+ if (options !== undefined) {
283
+ if (isNumber(options)) {
284
+ precision = options;
285
+ } else if (isBigNumber(options)) {
286
+ precision = options.toNumber();
287
+ } else if (isObject(options)) {
288
+ if (options.precision !== undefined) {
289
+ precision = _toNumberOrThrow(options.precision, () => {
290
+ throw new Error('Option "precision" must be a number or BigNumber');
291
+ });
292
+ }
293
+ if (options.wordSize !== undefined) {
294
+ wordSize = _toNumberOrThrow(options.wordSize, () => {
295
+ throw new Error('Option "wordSize" must be a number or BigNumber');
296
+ });
297
+ }
298
+ if (options.notation) {
299
+ notation = options.notation;
300
+ }
301
+ } else {
302
+ throw new Error('Unsupported type of options, number, BigNumber, or object expected');
303
+ }
304
+ }
305
+ return {
306
+ notation,
307
+ precision,
308
+ wordSize
309
+ };
310
+ }
311
+
288
312
  /**
289
313
  * Split a number into sign, coefficients, and exponent
290
314
  * @param {number | string} value
@@ -448,8 +472,8 @@ export function toPrecision(value, precision, options) {
448
472
  }
449
473
 
450
474
  // determine lower and upper bound for exponential notation.
451
- var lowerExp = options && options.lowerExp !== undefined ? options.lowerExp : -3;
452
- var upperExp = options && options.upperExp !== undefined ? options.upperExp : 5;
475
+ var lowerExp = _toNumberOrDefault(options === null || options === void 0 ? void 0 : options.lowerExp, -3);
476
+ var upperExp = _toNumberOrDefault(options === null || options === void 0 ? void 0 : options.upperExp, 5);
453
477
  var split = splitNumber(value);
454
478
  var rounded = precision ? roundDigits(split, precision) : split;
455
479
  if (rounded.exponent < lowerExp || rounded.exponent >= upperExp) {
@@ -654,4 +678,22 @@ export function copysign(x, y) {
654
678
  var signx = x > 0 ? true : x < 0 ? false : 1 / x === Infinity;
655
679
  var signy = y > 0 ? true : y < 0 ? false : 1 / y === Infinity;
656
680
  return signx ^ signy ? -x : x;
681
+ }
682
+ function _toNumberOrThrow(value, onError) {
683
+ if (isNumber(value)) {
684
+ return value;
685
+ } else if (isBigNumber(value)) {
686
+ return value.toNumber();
687
+ } else {
688
+ onError();
689
+ }
690
+ }
691
+ function _toNumberOrDefault(value, defaultValue) {
692
+ if (isNumber(value)) {
693
+ return value;
694
+ } else if (isBigNumber(value)) {
695
+ return value.toNumber();
696
+ } else {
697
+ return defaultValue;
698
+ }
657
699
  }
@@ -1,4 +1,4 @@
1
- import { createEmptyMap, assign } from './map.js';
1
+ import { ObjectWrappingMap, PartitionedMap } from './map.js';
2
2
 
3
3
  /**
4
4
  * Create a new scope which can access the parent scope,
@@ -10,15 +10,9 @@ import { createEmptyMap, assign } from './map.js';
10
10
  * the remaining `args`.
11
11
  *
12
12
  * @param {Map} parentScope
13
- * @param {...any} args
14
- * @returns {Map}
13
+ * @param {Object} args
14
+ * @returns {PartitionedMap}
15
15
  */
16
- export function createSubScope(parentScope) {
17
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
18
- args[_key - 1] = arguments[_key];
19
- }
20
- if (typeof parentScope.createSubScope === 'function') {
21
- return assign(parentScope.createSubScope(), ...args);
22
- }
23
- return assign(createEmptyMap(), parentScope, ...args);
16
+ export function createSubScope(parentScope, args) {
17
+ return new PartitionedMap(parentScope, new ObjectWrappingMap(args), new Set(Object.keys(args)));
24
18
  }
@@ -1,3 +1,3 @@
1
- export var version = '12.3.0';
1
+ export var version = '12.3.2';
2
2
  // Note: This file is automatically generated when building math.js.
3
3
  // Changes made in this file will be overwritten.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mathjs",
3
- "version": "12.3.0",
3
+ "version": "12.3.2",
4
4
  "description": "Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types like numbers, big numbers, complex numbers, fractions, units, and matrices.",
5
5
  "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)",
6
6
  "homepage": "https://mathjs.org",
@@ -25,7 +25,7 @@
25
25
  "unit"
26
26
  ],
27
27
  "dependencies": {
28
- "@babel/runtime": "^7.23.8",
28
+ "@babel/runtime": "^7.23.9",
29
29
  "complex.js": "^2.1.1",
30
30
  "decimal.js": "^10.4.3",
31
31
  "escape-latex": "^1.2.0",
@@ -36,21 +36,21 @@
36
36
  "typed-function": "^4.1.1"
37
37
  },
38
38
  "devDependencies": {
39
- "@babel/core": "7.23.7",
39
+ "@babel/core": "7.23.9",
40
40
  "@babel/plugin-transform-object-assign": "7.23.3",
41
- "@babel/plugin-transform-runtime": "7.23.7",
42
- "@babel/preset-env": "7.23.8",
41
+ "@babel/plugin-transform-runtime": "7.23.9",
42
+ "@babel/preset-env": "7.23.9",
43
43
  "@babel/register": "7.23.7",
44
44
  "@types/assert": "1.5.10",
45
45
  "@types/mocha": "10.0.6",
46
- "@typescript-eslint/eslint-plugin": "6.18.1",
47
- "@typescript-eslint/parser": "6.18.1",
46
+ "@typescript-eslint/eslint-plugin": "6.21.0",
47
+ "@typescript-eslint/parser": "6.21.0",
48
48
  "assert": "2.1.0",
49
49
  "babel-loader": "9.1.3",
50
50
  "benchmark": "2.1.4",
51
- "c8": "9.0.0",
51
+ "c8": "9.1.0",
52
52
  "codecov": "3.8.3",
53
- "core-js": "3.35.0",
53
+ "core-js": "3.35.1",
54
54
  "del": "6.1.1",
55
55
  "dtslint": "4.2.1",
56
56
  "eslint": "8.56.0",
@@ -74,7 +74,7 @@
74
74
  "karma-firefox-launcher": "2.1.2",
75
75
  "karma-mocha": "2.0.1",
76
76
  "karma-mocha-reporter": "2.2.5",
77
- "karma-webpack": "5.0.0",
77
+ "karma-webpack": "5.0.1",
78
78
  "mkdirp": "3.0.1",
79
79
  "mocha": "10.2.0",
80
80
  "mocha-junit-reporter": "2.2.1",
@@ -85,12 +85,12 @@
85
85
  "ndarray-pack": "1.2.1",
86
86
  "numericjs": "1.2.6",
87
87
  "pad-right": "0.2.2",
88
- "prettier": "3.1.1",
88
+ "prettier": "3.2.5",
89
89
  "process": "0.11.10",
90
90
  "sylvester": "0.0.21",
91
91
  "ts-node": "10.9.2",
92
92
  "typescript": "5.3.3",
93
- "webpack": "5.89.0",
93
+ "webpack": "5.90.1",
94
94
  "zeros": "1.0.0"
95
95
  },
96
96
  "type": "module",