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

BugFix3301: Precedence of % (mod) is higher than * and / #3311

Merged
merged 4 commits into from
Nov 7, 2024

Conversation

nkumawat34
Copy link
Contributor

Added the BugFix of Issue #3301

This PR addresses the problem where the precedence of the % (modulo) operator was incorrectly treated as having higher precedence than * and /. The fix ensures that % now has the same precedence as * and /, resolving the issue caused by the functional addition for % that led to incorrect operator precedence.

@josdejong josdejong added this to the v14 milestone Nov 6, 2024
@josdejong josdejong mentioned this pull request Nov 6, 2024
7 tasks
@josdejong josdejong changed the base branch from develop to v14 November 6, 2024 16:49
Copy link
Owner

@josdejong josdejong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THanks @nkumawat34 , impressive! I thought this would be tricky to fix.

Two remarks:

  1. Can you update the Precedence table: https://github.com/josdejong/mathjs/blob/develop/docs/expressions/syntax.md#precedence
  2. Can you add more unit tests to check the precedence of % and mod vs the four other operators * / .* and ./ ? (precedence should be equal now)

@nkumawat34
Copy link
Contributor Author

Please check now I updated the PR

@josdejong
Copy link
Owner

Thanks, that's a thorough est of tests 😎

One idea: how about changing the parseAndEval approach to the following:

function parseAndStringify(expr) {
  return math.parse(expr).toString({parenthesis: 'all'})
}

// ...
assert.strictEqual(parseAndStringify('10 % 3 * 2'), '(10 % 3) * 2')

Then there is no need for the explanatory comments like // Expected as (10 % 3) * 2 = 1 * 2 = 2? We would lose checking the actual outcome though. What do you think? (I'm ok with both, it's just an idea)

@josdejong
Copy link
Owner

josdejong commented Nov 7, 2024

Thinking about it, we could do something like this:

function assertParseAndEval(expr, expectedString, expectedResult) {
  const node = parse(expr)
  assert.strictEqual(node.toString({ parenthesis: 'all' }), expectedStr)
  approxEqual(node.evaluate(), expectedResult)
}

// ...

assertParseAndEval('10 % 3 * 2', '(10 % 3) * 2', 2)

😎

EDIT: I see there is already a helper function parseAndStringifyWithParens that is use to test implicit multiplication.

@nkumawat34
Copy link
Contributor Author

nkumawat34 commented Nov 7, 2024

should I have to use parseAndStringifyWithParens or not ?

@josdejong
Copy link
Owner

I'm ok with any of the three options: leave as is, create and use assertParseAndEval, or use parseAndStringifyWithParens. I have a slight preference for using parseAndStringifyWithParens. What do you think?

@nkumawat34
Copy link
Contributor Author

Ok I am going with this parseAndStringifyWithParens.I think it will be better.

@nkumawat34
Copy link
Contributor Author

done

@josdejong
Copy link
Owner

Thanks for the quick updates 👌, merging your PR now in the v14 branch.

@josdejong josdejong merged commit d0f8b2b into josdejong:v14 Nov 7, 2024
8 checks passed
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

Successfully merging this pull request may close these issues.

2 participants