New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparings strings #1085

Closed
majeeddl opened this Issue Apr 16, 2018 · 12 comments

Comments

Projects
None yet
4 participants
@majeeddl

majeeddl commented Apr 16, 2018

when I use string formula , the error is shown, I attached image for this error

2018-04-16_15-26-16

@FSMaxB

This comment has been minimized.

Collaborator

FSMaxB commented Apr 17, 2018

The documentation says the following:

Strings are compared by their numerical value.

This behavior is kind of surprising I have to say.

@josdejong: Is there a string comparison function? So far I haven't found one, if there isn't it might be a good addition.

@harrysarson

This comment has been minimized.

Collaborator

harrysarson commented Apr 17, 2018

See #1051 for duplicate issue.

Comparisions between strings was removed in v4 because people doing "67" > "7" were getting a nasty suprise as strings were being compared lexically.

Mathjs now tries for convert to number before making the comparison.

@josdejong maybe mathjs should allow people to compare strings using == but not > etc. For instance in javascript you can compare objects using == but not using > or >=. This might address both types of problems people are having.

edited

Clarified == vs ===.

@FSMaxB

This comment has been minimized.

Collaborator

FSMaxB commented Apr 17, 2018

On one hand I like the idea of having an === operator, since people using JavaScript might already be familiar with it, but on the other hand it might also be confusing.

But there needs to be a way to do string comparisons either way!

@harrysarson

This comment has been minimized.

Collaborator

harrysarson commented Apr 17, 2018

Sorry, I meant ==, I was suggesting using the existing equallity operator in mathjs to compare strings. I have edited my comment to make this clear.

@majeeddl majeeddl closed this Apr 17, 2018

@josdejong

This comment has been minimized.

Owner

josdejong commented Apr 17, 2018

@josdejong: Is there a string comparison function? So far I haven't found one, if there isn't it might be a good addition.

Yes, we should indeed add a function equalText or something like that. You can create such a function yourself with a one-liner: equalText(a, b) = (compareNatural(a, b) == 0).

@harrysarson Good idea. I expect though that changing == (math.equal) to also allow to compare strings with text has tricky cases. For example when the input contains a numeric value which cannot be parsed due to a typo, like "3,0E2" == "300", which will returns false instead of a parse error.

I like the idea of introducing a strictEqual function and === operator. This will allow comparing strings, and we can also use it to strictly compare numeric values, like make sure that both sides have the same numeric value and both are a number. But @FSMaxB you're right that this also makes things more complicated and confusing.

@josdejong josdejong reopened this Apr 17, 2018

@josdejong josdejong changed the title from Problem with string to Comparings strings Apr 17, 2018

@harrysarson

This comment has been minimized.

Collaborator

harrysarson commented Apr 17, 2018

@josdejong you make a very good point about strings that are almost but not quite numbers.

I think it is much better for someone to try and compare two strings and get an error message than compare two nearly numbers and get false due to their typo and spend ages debugging for this.

I take back my string equality suggestion

@josdejong

This comment has been minimized.

Owner

josdejong commented Apr 18, 2018

It was a good suggestion, please keep them coming Harry :). It would have been the nicest solution but unfortunately there are some drawbacks.

I was thinking more about the strictEqual function and === operator. I think it will become quite confusing: in JavaScript strict equality on objects only returns true when having the same object instance. For example a BigNumber is from JavaScript perspective an object, so math.bignumber(2) === math.bignumber(2) returns false, but from mathjs perspective you may see a bignumber as an "atomic" unit like numbers and strings, and you may expect it to return true.

So maybe we should simply go for an equalText function.

@harrysarson

This comment has been minimized.

Collaborator

harrysarson commented Apr 24, 2018

The two different equals operators in js lead to a fair bit of cunfusion, I would definately argue for keeping things simple in mathjs and sticking to ==.

I much prefer the idea of an equalText function :) As a name suggestion: strcmp like in c.

@josdejong

This comment has been minimized.

Owner

josdejong commented Apr 26, 2018

I've thought about it a bit more and I agree with Harry that we should go for the simple solution.

Let's implement two simple functions compareText and equalText then.

I'm open to other naming of these functions. strcmp is of course well known from c and php for example, though I don't think there is much familiarity with it in the JavaScript world. I personally don't really like the name strcmp because it is quite cryptic. Also, so far the function names in mathjs mostly use full words and no abbreviations. But if people really want we can do it like that (and people can of course always create their own favorite alias for the function name).

@harrysarson

This comment has been minimized.

Collaborator

harrysarson commented Apr 26, 2018

Would compareText return -1,0 or 1 based on the comparison?

@josdejong

This comment has been minimized.

Owner

josdejong commented Apr 27, 2018

It would work similar to compare and compareNatural (and strcmp): returns 1 when the first argument is larger than the second, 0 when both are equal, and -1 when the first argument is smaller than the second.

In fact, thinking about it, we can simply use compareNatural for text comparison so an additional method compareText may be overkill. At least equalText is really needed.

console.log(math.compare(2, 1)) // 1
console.log(math.compare(1, 1)) // 0
console.log(math.compare(1, 2)) // -1

console.log(math.compareNatural('B', 'A')) // 1
console.log(math.compareNatural('A', 'A')) // 0
console.log(math.compareNatural('A', 'B')) // -1
@josdejong

This comment has been minimized.

Owner

josdejong commented May 28, 2018

I've implemented functions compareText and equalText, they are now available in v4.4.0. Enjoy :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment