Skip to content

use math.divide_floored for floor division to match python semantics#37

Merged
medvednikov merged 1 commit intovlang:masterfrom
ylluminate:fix-floor-division
Feb 20, 2026
Merged

use math.divide_floored for floor division to match python semantics#37
medvednikov merged 1 commit intovlang:masterfrom
ylluminate:fix-floor-division

Conversation

@ylluminate
Copy link
Contributor

@ylluminate ylluminate commented Feb 20, 2026

Python's // (floor division) rounds toward negative infinity, but V's / truncates toward zero. They produce different results for negative operands: -7 // 2 = -4 in Python vs -7 / 2 = -3 in V.

Uses math.divide_floored() from V's stdlib for integer operands (returns floored quotient directly) and math.floor(left / right) for float operands.

Changes

  • visit_binop() in transpiler.v: FloorDiv handler with type-aware dispatch to math.divide_floored(a, b).quot (int) or math.floor(a / b) (float)
  • visit_aug_assign() in transpiler.v: FloorDiv handler for //= augmented assignment, same dispatch logic
  • types.v: clarifying comment on FloorDiv entry in op_to_symbol
  • 2 test expected outputs updated (numeric_ops, multiple_return)
  • 1 new test case: //= augmented assignment with negative numbers

All 110 tests pass.

@ylluminate
Copy link
Contributor Author

Closing for revision — will resubmit after review.

@ylluminate ylluminate closed this Feb 20, 2026
@ylluminate ylluminate reopened this Feb 20, 2026
@ylluminate ylluminate changed the title fix: use math.floor() for floor division to match Python semantics use math.floor for floor division to match python semantics Feb 20, 2026
@ylluminate ylluminate changed the title use math.floor for floor division to match python semantics use math.divide_floored for floor division to match python semantics Feb 20, 2026
@ylluminate ylluminate force-pushed the fix-floor-division branch 2 times, most recently from 90c446d to bc9741d Compare February 20, 2026 19:16
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.
@medvednikov medvednikov merged commit 96a92ae into vlang:master Feb 20, 2026
2 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