summaryrefslogtreecommitdiff
path: root/py/objint_mpz.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-03-22 20:25:55 +0000
committerDamien George <damien.p.george@gmail.com>2014-03-22 20:25:55 +0000
commit365274da13cc701e3e8f6c72a24dd4eb4083a88d (patch)
tree110170545eae9de4a584f08dd34d259cef0b70a0 /py/objint_mpz.c
parent0119fc7532c573bd596fb6173b4d36ef5260027a (diff)
parenta6d53188b7db85af9dc93186e4f36b7009084ea6 (diff)
Merge branch 'master' of github.com:micropython/micropython
Diffstat (limited to 'py/objint_mpz.c')
-rw-r--r--py/objint_mpz.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index 21e3202a9..39ea7ca11 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -1,5 +1,6 @@
#include <stdint.h>
#include <string.h>
+#include <stdio.h>
#include "nlr.h"
#include "misc.h"
@@ -97,15 +98,24 @@ mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
mpz_t rem; mpz_init_zero(&rem);
mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
+ if (zlhs->neg != zrhs->neg) {
+ if (!mpz_is_zero(&rem)) {
+ mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
+ mpz_add_inpl(&res->mpz, &res->mpz, &mpzone);
+ }
+ }
mpz_deinit(&rem);
break;
}
case RT_BINARY_OP_MODULO:
case RT_BINARY_OP_INPLACE_MODULO: {
- // TODO check that this operation matches the CPython operation
mpz_t quo; mpz_init_zero(&quo);
mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
mpz_deinit(&quo);
+ // Check signs and do Python style modulo
+ if (zlhs->neg != zrhs->neg) {
+ mpz_add_inpl(&res->mpz, &res->mpz, zrhs);
+ }
break;
}