davedelong/DDMathParser

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

graycampbell opened this issue · 3 comments

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.

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.

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!

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