Skip to content
Snippets Groups Projects
Commit e9ce00d8 authored by Damien George's avatar Damien George
Browse files

py: Implement divmod for mpz bignum.

parent c5029bcb
No related branches found
No related tags found
No related merge requests found
...@@ -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) { ...@@ -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); mpz_pow_inpl(&res->mpz, zlhs, zrhs);
break; 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: default:
return MP_OBJ_NULL; // op not supported return MP_OBJ_NULL; // op not supported
} }
......
...@@ -14,3 +14,9 @@ try: ...@@ -14,3 +14,9 @@ try:
except TypeError: except TypeError:
print("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))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment