Skip to content
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

Precedence of the modulus operator #3189

Open
zyw727725828 opened this issue Apr 17, 2024 · 3 comments
Open

Precedence of the modulus operator #3189

zyw727725828 opened this issue Apr 17, 2024 · 3 comments
Labels

Comments

@zyw727725828
Copy link

Why does 250/10%10 return the result Infinity? Why calculate % first and then /

@josdejong
Copy link
Owner

You can find the operator precedence here: https://mathjs.org/docs/expressions/syntax.html#precedence

You can check how an expression is evaluated via:

math.parse('250/10%10').toString({ parenthesis: 'all'})
// "250 / (10 % 10)"

If you need an alterative order of exection, you can use parenthesis:

math.evaluate('(250 / 10) % 10') 
// 5

@cirossmonteiro
Copy link

@josdejong I'm sorry for reviving this discussion, but according to the link provided before, division and modulus have the same precedence, which means the evaluation should be done from the left to the right.
Here's a screenshot from Javascript computation in Chrome's DevTools.
image

@josdejong josdejong reopened this Apr 29, 2024
josdejong added a commit that referenced this issue Apr 29, 2024
@josdejong
Copy link
Owner

Thanks, you're right. I now see that the documentation is not up to date. The order of predencence of the modules operator was changed in #2304 I think, or maybe in consecutive changes around implementing special rules for implicit multiplication.

I've updated the docs to be consistent with the actual behavior: 599f4ee

Separately, we can discuss if we want to make changes in this behavior or not.

@josdejong josdejong changed the title Calculation order problem Precedence of the modulus operator Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants