use math.divide_floored for floor division to match python semantics#37
Merged
medvednikov merged 1 commit intovlang:masterfrom Feb 20, 2026
Merged
Conversation
Contributor
Author
|
Closing for revision — will resubmit after review. |
69ec13a to
f98b5b0
Compare
f98b5b0 to
8adef78
Compare
90c446d to
bc9741d
Compare
python's // operator performs floor division (toward -inf), while V's / operator truncates toward zero. this causes incorrect results for negative numbers, e.g. -7 // 2 should be -4 but V gives -3. use math.divide_floored() for integer floor division and math.floor() for float floor division in both binary expressions and augmented assignments (//=). add test coverage for negative floor division cases.
bc9741d to
897a4d9
Compare
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.
Python's
//(floor division) rounds toward negative infinity, but V's/truncates toward zero. They produce different results for negative operands:-7 // 2 = -4in Python vs-7 / 2 = -3in V.Uses
math.divide_floored()from V's stdlib for integer operands (returns floored quotient directly) andmath.floor(left / right)for float operands.Changes
visit_binop()intranspiler.v: FloorDiv handler with type-aware dispatch tomath.divide_floored(a, b).quot(int) ormath.floor(a / b)(float)visit_aug_assign()intranspiler.v: FloorDiv handler for//=augmented assignment, same dispatch logictypes.v: clarifying comment on FloorDiv entry inop_to_symbolnumeric_ops,multiple_return)//=augmented assignment with negative numbersAll 110 tests pass.