Skip to content

Commit b7c7591

Browse files
committed
Fix float.__trunc__ to handle abnormal values
1 parent 4c8f655 commit b7c7591

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

tests/snippets/floats.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@
111111
assert 1.2.__trunc__() == 1
112112
assert int(1.2) == 1
113113
assert float(1.2) == 1.2
114-
# assert math.trunc(1.2) == 1
114+
assert math.trunc(1.2) == 1
115+
assert_raises(OverflowError, float('inf').__trunc__)
116+
assert_raises(ValueError, float('nan').__trunc__)
115117

116118
assert (1.7).real == 1.7
117119
assert (1.3).is_integer() == False

vm/src/obj/objfloat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ impl PyFloat {
377377
}
378378

379379
#[pymethod(name = "__trunc__")]
380-
fn trunc(&self, _vm: &VirtualMachine) -> BigInt {
381-
self.value.to_bigint().unwrap()
380+
fn trunc(&self, vm: &VirtualMachine) -> PyResult<BigInt> {
381+
try_to_bigint(self.value, vm)
382382
}
383383

384384
#[pymethod(name = "__round__")]
@@ -413,7 +413,7 @@ impl PyFloat {
413413
}
414414

415415
#[pymethod(name = "__int__")]
416-
fn int(&self, vm: &VirtualMachine) -> BigInt {
416+
fn int(&self, vm: &VirtualMachine) -> PyResult<BigInt> {
417417
self.trunc(vm)
418418
}
419419

0 commit comments

Comments
 (0)