-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Formula evaluation with adjacent parentheses incorrect #3333
Comments
Thanks for your input. Implicit multiplications can be tricky. A simpler case of your example is the following: math.parse('1/(2)(4)') // 0.125 I agree that the behavior is quite confusing. Mathjs has some special rules in place dealing with implicit multiplication. The difficult thing is that in different cases, you would expect different types of behavior. Just giving implicit multiplication the same precedence as multiply and divide gives odd cases too. math.parse('1/(2)(x)').toString({ parenthesis: 'all' }) // "1 / (2 x)"
math.parse('1/2 x').toString({ parenthesis: 'all' }) // "(1 / 2) x"
math.parse('4km / 2h').toString({ parenthesis: 'all' }) // "(4 km) / (2 h)" I'm always open for good ideas to improve the behavior, but implicit multiplication is a tricky subject, see for example #792, #2370. |
@josdejong we're using these regexes to "fix" the expressions before evaluation: expression
.replace(/(\d)(\()/, '$1*$2')
.replace(/(\))(\d)/, '$1*$2')
.replace(/(\))(\()/, '$1*$2') |
Thanks for sharing. Please be careful with regexes like these, they can go wrong when you have nesting, like nested parenthesis inside parenthesis. In general, it can help to present to the user how the experession is evaluated by shoing the exrpesion with all the (relevant) parens. |
It's looking specifically for something like |
Having a formula with division followed by adjacent parentheses statements is evaluated incorrectly.
Example:
Both of these formulas should evaluate to 71, but only the last one does.
The text was updated successfully, but these errors were encountered: