From c88c69a993050d950bca9daecbd83f38defca32e Mon Sep 17 00:00:00 2001 From: Tetramad Date: Mon, 25 Oct 2021 11:10:34 +0900 Subject: [PATCH] Fix math.log1p to pass some failed tests Now math.log1p with less than or equal to negative one should raise ValueError with "math domain error" message --- Lib/test/test_math.py | 2 -- stdlib/src/math.rs | 9 +++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 6b421bc55..c90c063cd 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -1063,8 +1063,6 @@ class MathTests(unittest.TestCase): self.assertEqual(math.log(INF), INF) self.assertTrue(math.isnan(math.log(NAN))) - # TODO: RUSTPYTHON - @unittest.expectedFailure def testLog1p(self): self.assertRaises(TypeError, math.log1p) for n in [2, 2**90, 2**300]: diff --git a/stdlib/src/math.rs b/stdlib/src/math.rs index b2437563a..c8e0c12b5 100644 --- a/stdlib/src/math.rs +++ b/stdlib/src/math.rs @@ -145,8 +145,13 @@ mod math { } #[pyfunction] - fn log1p(x: ArgIntoFloat) -> f64 { - (x.to_f64() + 1.0).ln() + fn log1p(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult { + let x = x.to_f64(); + if x.is_nan() || x > -1.0_f64 { + Ok((x + 1.0_f64).ln()) + } else { + Err(vm.new_value_error("math domain error".to_owned())) + } } #[pyfunction]