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 % (mod) is higher than * and / #3301

Closed
gwhitney opened this issue Oct 28, 2024 · 9 comments
Closed

Precedence of % (mod) is higher than * and / #3301

gwhitney opened this issue Oct 28, 2024 · 9 comments

Comments

@gwhitney
Copy link
Collaborator

The issues section is used only for bug reports. Please use the Discussions section to ask questions and share ideas and suggestions.

Describe the bug
Unlike in any other language I have checked, the precedence of % (or mod) used as a binary remainder operator is higher than * and /. As it is essentially a division operation, its precedence should be the same as * and /, as it is in essentially every other language checked.

To Reproduce
Evaluate 3.6 * 28 % 25. Currently mathjs produces 10.8 = 3.6 * 3 = 3.6 * (28 % 25). It should produce 0.8 = 100.8 % 25 = (3.6 * 28) % 25, as multiplicative/division operators should be evaluated left to right.

@nkumawat34
Copy link
Contributor

can i work on this issue.

@nkumawat34
Copy link
Contributor

nkumawat34 commented Oct 29, 2024

Screenshot (29)
They have written in their document.
It should give priority same with multiplication and divide.

@gwhitney
Copy link
Collaborator Author

Yes, the mathjs documentation is clear on this behavior. It is just different than just about any other system. But I would not recommend working on changing it unless the designer @josdejong agrees there should be a change. He may have his reasons for the unusual behavior, which I would respect. So this issue was more of a proposal/query as to why the departure from norms. I think if the current behavior is retained, such a rationale should be added to the documentation.

@josdejong
Copy link
Owner

Thanks for bringing this up Glen. The precedence of the modulus operator regularly leads to questions and unexpected behavior. I think it would be a good thing to bring bring the behavior in line with other applications: let module have the same precedence as multiplication and division.

Originally, modulus had the same precedence as multiplcation and division. I think the precedence was accidentally changed as as unintened side effect of implementing support for using % as percentage in #2304. See also (#3189 (comment).

It would be great if you can pick this up @nkumawat34 , though it's not the easiest first issue. Do you like a challenge? 😉

@nkumawat34
Copy link
Contributor

I will pick it.

@josdejong
Copy link
Owner

Thanks

@nkumawat34
Copy link
Contributor

Please review my PR #3311

@josdejong
Copy link
Owner

👍 I'll look into it this week, thanks!!

@josdejong
Copy link
Owner

Fixed now in v14.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants