Fix pow(0, exp) with negative exp to raise ZeroDivisionError

This commit is contained in:
Jeong YunWon
2019-10-05 00:03:31 +09:00
parent df6acaf163
commit 2153ebbc23
3 changed files with 19 additions and 8 deletions

View File

@@ -154,6 +154,15 @@ fn inner_gt_int(value: f64, other_int: &BigInt) -> bool {
}
}
pub fn float_pow(v1: f64, v2: f64, vm: &VirtualMachine) -> PyResult {
if v1.is_zero() {
let msg = format!("{} cannot be raised to a negative power", v1);
Err(vm.new_zero_division_error(msg))
} else {
v1.powf(v2).into_pyobject(vm)
}
}
#[pyimpl]
#[allow(clippy::trivially_copy_pass_by_ref)]
impl PyFloat {
@@ -359,7 +368,7 @@ impl PyFloat {
fn pow(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
try_float(&other, vm)?.map_or_else(
|| Ok(vm.ctx.not_implemented()),
|other| self.value.powf(other).into_pyobject(vm),
|other| float_pow(self.value, other, vm),
)
}
@@ -367,7 +376,7 @@ impl PyFloat {
fn rpow(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
try_float(&other, vm)?.map_or_else(
|| Ok(vm.ctx.not_implemented()),
|other| other.powf(self.value).into_pyobject(vm),
|other| float_pow(other, self.value, vm),
)
}