Skip to content

Commit 50c119a

Browse files
author
Shitong Wen
committed
fix floor div for int
1 parent 86fcc97 commit 50c119a

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

tests/snippets/ints.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
assert (2).__pow__(3.0) == NotImplemented
6565
assert (2).__rpow__(3.0) == NotImplemented
6666

67+
assert 10 // 4 == 2
68+
assert -10 // 4 == -3
69+
assert 10 // -4 == -3
70+
assert -10 // -4 == 2
6771

6872
assert int() == 0
6973
assert int("101", 2) == 5

vm/src/obj/objint.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ impl PyInt {
260260
if objtype::isinstance(&other, &vm.ctx.int_type()) {
261261
let v2 = get_value(&other);
262262
if *v2 != BigInt::zero() {
263-
Ok(vm.ctx.new_int((&self.value) / v2))
263+
let modulo = (&self.value % v2 + v2) % v2;
264+
Ok(vm.ctx.new_int((&self.value - modulo) / v2))
264265
} else {
265266
Err(vm.new_zero_division_error("integer floordiv by zero".to_string()))
266267
}

0 commit comments

Comments
 (0)