aboutsummaryrefslogtreecommitdiff
path: root/py/objint_mpz.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-07-25 11:49:22 +1000
committerDamien George <damien.p.george@gmail.com>2017-07-25 11:49:22 +1000
commit04552ff71b6c722b21597d93481f024c72457cef (patch)
treefdbc66843e079a78cbf964aa6308ab5db78a42d1 /py/objint_mpz.c
parent4d1fb6107fdedb0dda8dfb1491c033bf731222c6 (diff)
py: Implement raising a big-int to a negative power.
Before this patch raising a big-int to a negative power would just return 0. Now it returns a floating-point number with the correct value.
Diffstat (limited to 'py/objint_mpz.c')
-rw-r--r--py/objint_mpz.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index d818b6f40..26492aab4 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -290,6 +290,13 @@ mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
case MP_BINARY_OP_POWER:
case MP_BINARY_OP_INPLACE_POWER:
+ if (mpz_is_neg(zrhs)) {
+ #if MICROPY_PY_BUILTINS_FLOAT
+ return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
+ #else
+ mp_raise_ValueError("negative power with no float support");
+ #endif
+ }
mpz_pow_inpl(&res->mpz, zlhs, zrhs);
break;