davedelong / DDMathParser

String → Number
MIT License
854 stars 153 forks source link

8÷2(2+2) evaluates to 1 instead of 16 #167

Closed graycampbell closed 5 years ago

graycampbell commented 5 years ago

I'm assuming this is because the math parser places a higher precedence on implicit multiplication than explicit multiplication, since using 8÷2×(2+2) instead yields a result of 16. Google's calculator and Wolfram Alpha, however, both evaluate 8÷2(2+2) as 16.

davedelong commented 5 years ago

Right.

By default, implicit multiplication has a higher precedence than regular multiplication. This is because we typically interpret 1/3x to be 1 ÷ (3 * x) and not (1 ÷ 3) * x.

The option was added because of #64. It because enabled by default in commit e168c8e80c1cda346eb43c7982001acb80bbe8a0, and it lives on in the Configuration struct as the useHighPrecedenceImplicitMultiplication.

If you'd like, you can create a Configuration struct where that value is false, and then use that Configuration when parsing your string.

graycampbell commented 5 years ago

I even saw that property when I was looking through Configuration, but apparently it didn't register with me what it actually meant. Thanks for clearing that up for me!

davedelong commented 5 years ago

Glad to help! Please file another issue if you have more questions :)