Repository navigation
Truncate integer division and remainder in Python and JavaScript - #188
Merged
Merged
Conversation
… [patch] The AST means / and % on two integers the way the C family does: the quotient truncates toward zero and the remainder takes the dividend's sign. Python's and JavaScript's / are true division, and Python's % floors, so -7 / 2 came out as -3.5 and -7 % 2 as 1. When both operands are statically integers, Python now writes int(a / b) and a - b * int(a / b) (bound through a lambda when an operand is not a literal or a variable, so it is evaluated once), and JavaScript writes Math.trunc(a / b). Both generators now open the static-type scope Go already used, so parameters and typed locals are known to be integers. Fixes #114 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AXWSrf4nXKEZF5xVNTiVV5
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #114
Problem
The AST means
/and%on two integers the way C, C++, C#, Go and Rust do: the quotient truncates toward zero and the remainder takes the dividend's sign. Python's and JavaScript's/are true division, and Python's%floors. So-7 / 2came out as-3.5in both targets (a float from a function annotated-> int), and Python gave1for-7 % 2instead of-1.Change
StandardLanguageGeneratornow has aGenerateBinaryhook, anIsIntegerArithmeticcheck built on the existingStaticTypeinference, and anOpenStaticTypeScopeguard.int(a / b). It isn't//, which floors.(a - b * int(a / b)). If an operand is anything other than a literal or a variable, it goes through(lambda a, b: …)(x, y)so it is evaluated only once.math.fmodisn't used because an expression can be generated outside a file that could carry the import.Math.trunc(a / b). JavaScript's%already truncates, so it is unchanged.Tests
IntegerDivisionTestsgenerates functions that divide integer parameters, integer literals, a typed local, and a call operand. It runs them under realpython3andnodewith negative and mixed-sign operands, and compares the results with C#'s/and%.intand that the call operand runs exactly once.Not covered here: compound assignment (
x /= 2on an int) has the same mismatch in Python. It goes throughAssignmentStatementrather thanBinaryExpression, and is left for a follow-up.🤖 Generated with Claude Code
https://claude.ai/code/session_01AXWSrf4nXKEZF5xVNTiVV5
Generated by Claude Code