summaryrefslogtreecommitdiffstatshomepage
path: root/py/objint_mpz.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-03-22 20:26:17 +0000
committerDamien George <damien.p.george@gmail.com>2014-03-22 20:26:17 +0000
commita6d53188b7db85af9dc93186e4f36b7009084ea6 (patch)
tree38b2f947a5d714c5ee79a99f164d94aa780802a5 /py/objint_mpz.c
parent463997f638b1c3e5fdb3e0e8a9c4339b0e712f8a (diff)
parent56402796d87f75fbb9e42fc9e3c0adc027fb7c98 (diff)
downloadmicropython-a6d53188b7db85af9dc93186e4f36b7009084ea6.tar.gz
micropython-a6d53188b7db85af9dc93186e4f36b7009084ea6.zip
Merge pull request #359 from rjdowdall/master
Fixed some math functions and added more exceptions.
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 21e3202a95..39ea7ca115 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;
}