Mathjs: Can't compare string with anything

Created on 1 Mar 2018  路  7Comments  路  Source: josdejong/mathjs

version

[email protected]

Strings comparison doesn't work for strings:

math.eval('"abc" == "abc"');
// or
math.equal('abc', 'abc');
// and 
math.equal(1, 'abc');

return error:

Error: Cannot convert "abc" to a number
    at convert (/node_modules/mathjs/lib/core/typed.js:195:17)
    at Array.convertArg (/node_modules/typed-function/typed-function.js:819:22)
    at convertArgs (/node_modules/typed-function/typed-function.js:766:45)
    at generic (/node_modules/typed-function/typed-function.js:1073:27)
    at equalScalar (/node_modules/typed-function/typed-function.js:1092:24)
    at any, any (/node_modules/mathjs/lib/function/relational/equal.js:69:14)
    at generic (/node_modules/typed-function/typed-function.js:1073:27)
    at Object.equal (/node_modules/typed-function/typed-function.js:1092:24)
question

Most helpful comment

whether I can overwrite ("wrap") default behaviour for == function

yes that should be very easy:

math.import({
  equal: function (a, b) { return a === b } 
}, {override: true})

math.eval('"abc" == "abc"') // true

or something like that :)

All 7 comments

I've just tried old [email protected] version and it works for examples noted above

That's correct, the behavior of comparing strings has been changed in v4, see history: http://mathjs.org/history.html#20180225-version-400 -> "String comparison"

@josdejong ok, does it mean that we no longer can use textual expressions like?:

math.eval('"abc" == "abc"');

Is there alternative how could I use textual expressions for text comparison?

That's correct, the alternative is explained in the history http://mathjs.org/history.html#20180225-version-400:

Changed the behavior of relational functions (compare, equal, equalScalar, larger, largerEq, smaller, smallerEq, unequal) to compare strings by their numeric value they contain instead of alphabetically. This also impacts functions deepEqual, sort, min, max, median, and partitionSelect. Use compareNatural if you need to sort an array with text. See #680.

For most functions it's a nice improvement that you can enter text containing values, like math.sqrt('4') and math.multiply('2', '4') works now. In some cases like comparing strings lexically it's more verbose now to compare strings. Since math.js is about maths, we think the main use case is using strings containing values and it is a good trade off in general. To compare strings lexically, you can use compareNatural, something like:

math.eval('compareNatural("abc", "abc") == 0'); // true

You can of course write your own wrapper functions to make this less verbose.

@josdejong Thanks! My goal was to use mathjs in my middleware extension for comparison wide variety of fields (strings or numbers). But with compareNatural it won't work. And thanks for the hint! I'm going to take a look whether I can overwrite ("wrap") default behaviour for == function (https://github.com/josdejong/mathjs/blob/master/lib/function/relational/equal.js).

whether I can overwrite ("wrap") default behaviour for == function

yes that should be very easy:

math.import({
  equal: function (a, b) { return a === b } 
}, {override: true})

math.eval('"abc" == "abc"') // true

or something like that :)

I think this is fixed by the changes referenced in #1085 and if people want to override the default behavour they can using https://github.com/josdejong/mathjs/issues/1051#issuecomment-369930811. :smile:

@hyzhak please feel free to reopen if there are still issues here.

Was this page helpful?
0 / 5 - 0 ratings