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.
This commit is contained in:
Damien George
2017-07-25 11:49:22 +10:00
parent 4d1fb6107f
commit 04552ff71b
5 changed files with 19 additions and 4 deletions

View File

@@ -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;