diff options
author | Damien George <damien.p.george@gmail.com> | 2015-06-13 22:40:50 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-06-13 23:38:28 +0100 |
commit | e9ce00d874ef6c93a56e726bb1503341f0448bf9 (patch) | |
tree | 0ab159361f833c6af960fbf36728f8ec307a35e6 | |
parent | c5029bcbf37fd1a3fb145d9fa9e4a5094478f17b (diff) | |
download | micropython-e9ce00d874ef6c93a56e726bb1503341f0448bf9.tar.gz micropython-e9ce00d874ef6c93a56e726bb1503341f0448bf9.zip |
py: Implement divmod for mpz bignum.
-rw-r--r-- | py/objint_mpz.c | 11 | ||||
-rw-r--r-- | tests/basics/builtin_divmod.py | 6 |
2 files changed, 17 insertions, 0 deletions
diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 936e2cb2b9..27f1ddbfc8 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -262,6 +262,17 @@ mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { mpz_pow_inpl(&res->mpz, zlhs, zrhs); break; + case MP_BINARY_OP_DIVMOD: { + mp_obj_int_t *quo = mp_obj_int_new_mpz(); + mpz_divmod_inpl(&quo->mpz, &res->mpz, zlhs, zrhs); + // Check signs and do Python style modulo + if (zlhs->neg != zrhs->neg) { + mpz_add_inpl(&res->mpz, &res->mpz, zrhs); + } + mp_obj_t tuple[2] = {quo, res}; + return mp_obj_new_tuple(2, tuple); + } + default: return MP_OBJ_NULL; // op not supported } diff --git a/tests/basics/builtin_divmod.py b/tests/basics/builtin_divmod.py index f159691b7b..e3eff306be 100644 --- a/tests/basics/builtin_divmod.py +++ b/tests/basics/builtin_divmod.py @@ -14,3 +14,9 @@ try: except TypeError: print("TypeError") +# bignum +l = (1 << 65) + 123 +print(divmod(3, l)) +print(divmod(l, 5)) +print(divmod(l + 3, l)) +print(divmod(l * 20, l + 2)) |