Skip to content

Commit ff54fc8

Browse files
Merge pull request RustPython#1547 from Writtic/writtic/module_modf
Improve modf module of math
2 parents d09c768 + 731186a commit ff54fc8

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

tests/snippets/stdlib_math.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,8 @@ def assertAllNotClose(examples, *args, **kwargs):
241241
assert math.modf(2.56) == (0.56, 2.0)
242242
assert math.modf(-2.56) == (-0.56, -2.0)
243243
assert math.modf(1) == (0.0, 1.0)
244+
assert math.modf(INF) == (0.0, INF)
245+
assert math.modf(NINF) == (-0.0, NINF)
246+
modf_nan = math.modf(NAN)
247+
assert math.isnan(modf_nan[0])
248+
assert math.isnan(modf_nan[1])

vm/src/stdlib/math.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ fn math_factorial(value: PyIntRef, vm: &VirtualMachine) -> PyResult<BigInt> {
264264

265265
fn math_modf(x: IntoPyFloat, _vm: &VirtualMachine) -> (f64, f64) {
266266
let x = x.to_f64();
267+
if !x.is_finite() {
268+
if x.is_infinite() {
269+
return (0.0_f64.copysign(x), x);
270+
} else if x.is_nan() {
271+
return (x, x);
272+
}
273+
}
274+
267275
(x.fract(), x.trunc())
268276
}
269277

0 commit comments

Comments
 (0)