Skip to content

Commit 4bd3b81

Browse files
committed
evaluate_to_constant returns NaN for non-numeric, by default
1 parent a9d5e30 commit 4bd3b81

File tree

9 files changed

+40
-37
lines changed

9 files changed

+40
-37
lines changed

build/math-expressions.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/math-expressions_umd.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/assumptions/element_of_sets.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function is_integer_ast(tree, assumptions, original_assumptions) {
153153
return Number.isInteger(tree);
154154

155155
// if can convert to constant, evaluate directly
156-
var c = evaluate_to_constant(tree, false);
156+
var c = evaluate_to_constant(tree, {remove_units_first: false, nan_for_non_numeric: false});
157157
if(c !== null) {
158158
return Number.isInteger(c);
159159
}
@@ -436,7 +436,7 @@ function is_real_ast(tree, assumptions, original_assumptions) {
436436
return Number.isFinite(tree);
437437

438438
// if can convert to constant, evaluate directly
439-
var c = evaluate_to_constant(tree, false);
439+
var c = evaluate_to_constant(tree, {remove_units_first: false, nan_for_non_numeric: false});
440440

441441
if(c !== null) {
442442
if(typeof c === 'number') {
@@ -762,7 +762,7 @@ function is_complex_ast(tree, assumptions, original_assumptions) {
762762
return Number.isFinite(tree);
763763

764764
// if can convert to constant, evaluate directly
765-
var c = evaluate_to_constant(tree, false);
765+
var c = evaluate_to_constant(tree, {remove_units_first: false, nan_for_non_numeric: false});
766766
if(c !== null) {
767767
if(typeof c === 'number') {
768768
return Number.isFinite(c);
@@ -1057,7 +1057,7 @@ function is_nonzero_ast(tree, assumptions, original_assumptions) {
10571057
}
10581058

10591059
// if can convert to constant, evaluate directly
1060-
var c = evaluate_to_constant(tree, false);
1060+
var c = evaluate_to_constant(tree, {remove_units_first: false, nan_for_non_numeric: false});
10611061
if(c !== null) {
10621062
if(typeof c === 'number') {
10631063
if(Number.isFinite(c))
@@ -1489,7 +1489,7 @@ function is_positive_ast(tree, assumptions, strict, original_assumptions) {
14891489
return is_real;
14901490

14911491
// if can convert to constant, evaluate directly
1492-
var c = evaluate_to_constant(tree, false);
1492+
var c = evaluate_to_constant(tree, {remove_units_first: false, nan_for_non_numeric: false});
14931493

14941494
if(c !== null) {
14951495
if(typeof c === 'number') {

lib/expression/evaluation.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,20 @@ const evaluate = function (expr, bindings) {
3232
// return parser.ast.to.finiteField( expr.tree, modulus )( bindings );
3333
// };
3434

35-
const evaluate_to_constant = function (expr_or_tree, remove_units_first = true, scale_based_on_unit = true) {
35+
const evaluate_to_constant = function (expr_or_tree,
36+
{ remove_units_first = true, scale_based_on_unit = true, nan_for_non_numeric = true } = {}
37+
) {
3638
// evaluate to number by converting tree to number
3739
// and calling without arguments
3840

39-
// return null if couldn't evaluate to constant (e.g., contains a variable)
40-
// otherwise returns constant
41+
// return NaN (or null if nan_for_non_numeric is false)
42+
// if couldn't evaluate to constant (e.g., contains a variable)
43+
// otherwise returns constant (which could be NaN for an expression like 0/0)
4144
// NOTE: constant could be a math.js complex number object
4245

4346
var tree = get_tree(expr_or_tree);
4447

45-
if(remove_units_first) {
48+
if (remove_units_first) {
4649
tree = remove_units(tree, scale_based_on_unit);
4750
}
4851

@@ -56,14 +59,14 @@ const evaluate_to_constant = function (expr_or_tree, remove_units_first = true,
5659
} else if (tree === "i" && math.define_i) {
5760
return { re: 0, im: 1 };
5861
}
59-
return null;
62+
return nan_for_non_numeric ? NaN : null;
6063
}
6164

6265
if (!Array.isArray(tree)) {
63-
return null;
66+
return nan_for_non_numeric ? NaN : null;
6467
}
6568

66-
var num = null;
69+
var num = nan_for_non_numeric ? NaN : null;
6770
try {
6871
var the_f = f(tree);
6972
num = the_f();

lib/expression/simplify.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,9 @@ function evaluate_numbers_sub(tree, assumptions, max_digits, skip_ordering, eval
584584
return null;
585585
}
586586

587-
var c = evaluate_to_constant(tree, false);
587+
var c = evaluate_to_constant(tree, {remove_units_first: false});
588588

589-
if (c !== null) {
589+
if (!Number.isNaN(c)) {
590590
if (typeof c === 'number') {
591591
if (Number.isFinite(c)) {
592592
let n = simplify_number(c);
@@ -882,8 +882,8 @@ function collect_like_terms_factors(expr_or_tree, assumptions, max_digits) {
882882
return true;
883883
if (Array.isArray(s) && s[0] === '-' && (typeof s[1] === 'number'))
884884
return true;
885-
let c = evaluate_to_constant(s, false);
886-
if (typeof c === 'number' && Number.isFinite(c))
885+
var c = evaluate_to_constant(s, {remove_units_first: false});
886+
if (Number.isFinite(c))
887887
return true;
888888

889889
return false;

lib/polynomial/polynomial.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ function expression_to_polynomial(expr_or_tree) {
2424
if(typeof tree === 'number')
2525
return tree;
2626

27-
let c = evaluate_to_constant(tree, false);
28-
if(c !== null && Number.isFinite(c)) {
27+
var c = evaluate_to_constant(tree, {remove_units_first: false});
28+
if(Number.isFinite(c)) {
2929
return simplify.simplify(tree);
3030
}
3131

@@ -80,10 +80,10 @@ function expression_to_polynomial(expr_or_tree) {
8080
// if pow isn't a literal nonnegative integer
8181
if((typeof pow !== 'number') || pow < 0 || !Number.isInteger(pow)) {
8282

83-
let pow_num = evaluate_to_constant(pow, false);
83+
let pow_num = evaluate_to_constant(pow, {remove_units_first: false});
8484

8585
// check if pow is a rational number with a small base
86-
if(pow_num !== null || Number.isFinite(pow_num)) {
86+
if(Number.isFinite(pow_num)) {
8787
let pow_fraction = math.fraction(pow_num);
8888
if(pow_fraction.d <= 100) {
8989
if(pow_fraction.s < 0)
@@ -117,9 +117,9 @@ function expression_to_polynomial(expr_or_tree) {
117117
else if(operator === '/') {
118118
var denom = operands[1];
119119

120-
var denom_num = evaluate_to_constant(denom, false);
120+
var denom_num = evaluate_to_constant(denom, {remove_units_first: false});
121121

122-
if(denom_num === null || !Number.isFinite(denom_num)) {
122+
if(!Number.isFinite(denom_num)) {
123123
// return entire tree as polynomial variable
124124
return ['polynomial', tree, [[1, 1]]];
125125
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "math-expressions",
33
"description": "Perform basic equality testing and symbolic computations on mathematical expressions involving transcendental functions",
4-
"version": "2.0.0-alpha59",
4+
"version": "2.0.0-alpha60",
55
"author": {
66
"name": "Jim Fowler",
77
"email": "[email protected]",

spec/slow_simplify.spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ describe("evaluate_to_constant", function () {
276276

277277
});
278278

279-
it("with blanks gives null", function () {
280-
expect(me.fromText("1+2+").evaluate_to_constant()).toEqual(null);
279+
it("with blanks gives NaN", function () {
280+
expect(me.fromText("1+2+").evaluate_to_constant()).toEqual(NaN);
281281
})
282282

283283
it("multiple pluses and minus", function () {
@@ -298,19 +298,19 @@ describe("evaluate_to_constant", function () {
298298
})
299299

300300
it("with units", function () {
301-
expect(me.fromText("50%").evaluate_to_constant(false)).toEqual(null);
301+
expect(me.fromText("50%").evaluate_to_constant({remove_units_first:false})).toEqual(NaN);
302302
expect(me.fromText("50%").evaluate_to_constant()).toEqual(0.5);
303-
expect(me.fromText("50%").evaluate_to_constant(true, false)).toEqual(50);
303+
expect(me.fromText("50%").evaluate_to_constant({scale_based_on_unit: false})).toEqual(50);
304304
expect(me.fromText("50%").remove_units().evaluate_to_constant()).toEqual(0.5);
305305
expect(me.fromText("50%").remove_units(false).evaluate_to_constant()).toEqual(50);
306-
expect(me.fromText("$9.5").evaluate_to_constant(false)).toEqual(null);
306+
expect(me.fromText("$9.5").evaluate_to_constant({remove_units_first:false})).toEqual(NaN);
307307
expect(me.fromText("$9.5").evaluate_to_constant()).toEqual(9.5);
308-
expect(me.fromText("$9.5").evaluate_to_constant(true, false)).toEqual(9.5);
308+
expect(me.fromText("$9.5").evaluate_to_constant({scale_based_on_unit: false})).toEqual(9.5);
309309
expect(me.fromText("$9.5").remove_units().evaluate_to_constant()).toEqual(9.5);
310310
expect(me.fromText("$9.5").remove_units(false).evaluate_to_constant()).toEqual(9.5);
311-
expect(me.fromText("180deg").evaluate_to_constant(false)).toEqual(null);
311+
expect(me.fromText("180deg").evaluate_to_constant({remove_units_first:false})).toEqual(NaN);
312312
expect(me.fromText("180deg").evaluate_to_constant()).toEqual(Math.PI);
313-
expect(me.fromText("180deg").evaluate_to_constant(true, false)).toEqual(180);
313+
expect(me.fromText("180deg").evaluate_to_constant({scale_based_on_unit: false})).toEqual(180);
314314
expect(me.fromText("180deg").remove_units().evaluate_to_constant()).toEqual(Math.PI);
315315
expect(me.fromText("180deg").remove_units(false).evaluate_to_constant()).toEqual(180);
316316
})

0 commit comments

Comments
 (0)