From 50c119a503002b88cb32c7ad3781ec77444c292a Mon Sep 17 00:00:00 2001 From: Shitong Wen Date: Fri, 10 May 2019 14:21:09 +0800 Subject: [PATCH] fix floor div for int --- tests/snippets/ints.py | 4 ++++ vm/src/obj/objint.rs | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/snippets/ints.py b/tests/snippets/ints.py index ef9328d69..be41c3257 100644 --- a/tests/snippets/ints.py +++ b/tests/snippets/ints.py @@ -64,6 +64,10 @@ assert (2).__rtruediv__(1.0) == NotImplemented assert (2).__pow__(3.0) == NotImplemented assert (2).__rpow__(3.0) == NotImplemented +assert 10 // 4 == 2 +assert -10 // 4 == -3 +assert 10 // -4 == -3 +assert -10 // -4 == 2 assert int() == 0 assert int("101", 2) == 5 diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 21f50a182..403fd08ef 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -260,7 +260,8 @@ impl PyInt { if objtype::isinstance(&other, &vm.ctx.int_type()) { let v2 = get_value(&other); if *v2 != BigInt::zero() { - Ok(vm.ctx.new_int((&self.value) / v2)) + let modulo = (&self.value % v2 + v2) % v2; + Ok(vm.ctx.new_int((&self.value - modulo) / v2)) } else { Err(vm.new_zero_division_error("integer floordiv by zero".to_string())) }