You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Integer division and modulo give different results in Python and JavaScript: -7 / 2 returns -3 in C/C++/C#/Go/Rust but -3.5 in Python and JS #114
BinaryOperator.Divide and BinaryOperator.Modulo are spelled / and % for every target: PythonGenerator.GetOperatorSpelling (Coder/Languages/PythonGenerator.cs:803-808) and JavaScriptGenerator.cs:554-558 use them unchanged. The project treats int / int as truncating integer division; FloatingPointLiteralTests exists because 1 / 2 is integer division in C, C++ and Go. But Python's / and JavaScript's / are true division, and Python's % floors. So the same AST computes different values depending on the target language.
Every generator emits return (-7 / 2). Running the emitted code:
Target
half() returns
C, C++, C#, Go, Rust (checked with rustc)
-3
Python (python3)
-3.5, a float from a function annotated -> int
JavaScript (node)
-3.5
Modulo also differs: Python evaluates -7 % 2 to 1, where the C family gives -1.
Why it matters
The whole purpose of the tool is that one AST means the same thing in every target. Arithmetic that silently changes value, and in Python's case changes type, is hard to spot in generated code and breaks anything that uses the result, such as indexing or loop bounds.
Suggested fix / acceptance criteria
When both operands are statically integers (integer literals and integer-typed variables or parameters, where the type is known), emit truncating division: int(a / b) or math.trunc(a / b) in Python, and Math.trunc(a / b) in JavaScript. Don't use Python's //, because it floors: -7 // 2 == -4.
For integer modulo in Python, emit math.fmod(a, b) cast to int, or a - b * int(a / b).
If operand types are unknown, document the difference. A generator-level comment would also work.
Add tests with negative operands for Divide and Modulo in the Python and JavaScript generators.
Priority: High. The same AST gives different values depending on the target, with no error: -7 / 2 is -3 in the C family but -3.5 in Python and JS, and Python's % floors. Python also returns a float from a function annotated -> int. Any index or loop bound built from integer division is silently wrong in two of the seven targets.
Area / suggested owner:PythonGenerator.GetOperatorSpelling and JavaScriptGenerator operator spelling. Both need an operand-type check before choosing the spelling.
Notes: The acceptance criteria rightly rule out Python //, which floors. When operand types are unknown, a generated comment is the minimum; guessing is worse than flagging.
Activity
matt-edmondson commentedon Sep 27, 2026
Triage
-7 / 2is-3in the C family but-3.5in Python and JS, and Python's%floors. Python also returns a float from a function annotated-> int. Any index or loop bound built from integer division is silently wrong in two of the seven targets.PythonGenerator.GetOperatorSpellingandJavaScriptGeneratoroperator spelling. Both need an operand-type check before choosing the spelling.//, which floors. When operand types are unknown, a generated comment is the minimum; guessing is worse than flagging.Generated by Claude Code