Skip to content

Commit a0ad436

Browse files
committed
Add float.pow OverflowError handling
1 parent a318e4e commit a0ad436

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

tests/snippets/floats.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@
113113
assert float(1.2) == 1.2
114114
# assert math.trunc(1.2) == 1
115115

116+
assert 1.2 ** 2 == 1.44
117+
assert_raises(OverflowError, lambda: 1.2 ** (10 ** 1000))
118+
116119
assert (1.7).real == 1.7
117120
assert (1.3).is_integer() == False
118121
assert (1.0).is_integer() == True

vm/src/obj/objfloat.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,11 @@ impl PyFloat {
262262
}
263263

264264
#[pymethod(name = "__pow__")]
265-
fn pow(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
266-
let v1 = self.value;
267-
if objtype::isinstance(&other, &vm.ctx.float_type()) {
268-
vm.ctx.new_float(v1.powf(get_value(&other)))
269-
} else if objtype::isinstance(&other, &vm.ctx.int_type()) {
270-
let result = v1.powf(objint::get_value(&other).to_f64().unwrap());
271-
vm.ctx.new_float(result)
272-
} else {
273-
vm.ctx.not_implemented()
274-
}
265+
fn pow(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
266+
try_float(&other, vm)?.map_or_else(
267+
|| Ok(vm.ctx.not_implemented()),
268+
|other| self.value.powf(other).into_pyobject(vm),
269+
)
275270
}
276271

277272
#[pymethod(name = "__sub__")]

0 commit comments

Comments
 (0)