Skip to content

Truncate integer /= and %= in Python and JavaScript the way the C family does [patch] - #214

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/183-compound-integer-division
Oct 13, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/183-compound-integer-division

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #183

What changed

#114 lowered integer / and % in BinaryExpression. Compound assignment goes through GenerateAssignmentStatement instead, which still wrote x /= y and x %= y verbatim:

Target a = 5; a /= 2 a = -7; a %= 2
C family 2 -1
Python, before 2.5 (a float) 1
JavaScript, before 2.5 -1

Tests (CompoundIntegerDivisionTests)

  • Python_MatchesTheCFamily and JavaScript_MatchesTheCFamily run the generated code. They cover /= and %= on a parameter, a typed local, a literal divisor, and a divisor that is a call, across seven operand pairs including every sign combination. Each result must equal the C# result, Python's must be an int, and the call must be made exactly once.
  • OtherCompoundAssignments_AreUnchanged (Python, JavaScript): /= on unknown or double operands and += keep their spelling.

With the generator changes reverted, both run tests fail (div_params(-7, 2) = -3.5, expected -3). With the changes, the full suite passes (1157/1157).

🤖 Generated with Claude Code

https://claude.ai/code/session_01P8rVj8U65vKDPQv2LuKHEw


Generated by Claude Code

…ily does [patch]

#114 lowered integer / and % in BinaryExpression, but compound assignment
takes its own path and still wrote x /= y, which is true division in Python
and JavaScript, and x %= y, which floors in Python. When both sides are known
to be integers, Python and JavaScript now write the assignment out as
x = <lowered binary>, reusing GenerateBinary.

Fixes #183

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P8rVj8U65vKDPQv2LuKHEw
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit f2aa5b6 into main Oct 13, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/183-compound-integer-division branch October 13, 2026 08:23
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.

Compound /= and %= give different results in Python and JavaScript: a /= 2 makes an int 2.5 (not covered by #114)

1 participant