davedelong / DDMathParser

String → Number
MIT License
854 stars 153 forks source link

Incorrect evaluation of mixed fractions #142

Closed Roman-Kerimov closed 6 years ago

Roman-Kerimov commented 6 years ago

For example, "2½".evaluate() returns 1 instead of 2.5

davedelong commented 6 years ago

Ah... yes, because it's parsing the 2, and then the ½... it sees them as separate tokens, and assumes they're an implicit multiplication.

davedelong commented 6 years ago

But I agree that's not ideal. :)

davedelong commented 6 years ago

ok, I've got a rudimentary fix in place, but this has actually highlighted a deeper problem with DDMathParser's token resolution.

The current fix is to inject a + operator in between a number and a fraction, however that will break evaluation for things like 2½ * 2, because:

I'm thinking I'll need to change how resolution is done in order to fully solve this.

Roman-Kerimov commented 6 years ago

I think that this problem can be solved by adding a special addition operator with the highest priority.

davedelong commented 6 years ago

@Roman-Kerimov you're right, that's probably a simpler fix than what I was thinking. The fix has been pushed to master. Thanks!