mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Fix math.log to pass some failed tests
Now math.log with less than or equal to zero should raise ValueError with "math domain error" message if `x` fit in f64
This commit is contained in:
1 parent
ba3cafc932
commit
3d8f6cf08d
2 files changed
+12
-6
No files matched your search
Vendored
-4
@@ -2133,8 +2133,6 @@ class TestGeometricMean(unittest.TestCase):
|
||||
self.assertTrue(math.isclose(small_gm, 36.0 * small))
|
||||
self.assertNotEqual(small_gm, 0.0)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_error_cases(self):
|
||||
geometric_mean = statistics.geometric_mean
|
||||
StatisticsError = statistics.StatisticsError
|
||||
@@ -2155,8 +2153,6 @@ class TestGeometricMean(unittest.TestCase):
|
||||
with self.assertRaises(TypeError):
|
||||
geometric_mean([10, 20, 60], 70) # too many arguments
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_special_values(self):
|
||||
# Rules for special values are inherited from math.fsum()
|
||||
geometric_mean = statistics.geometric_mean
|
||||
|
||||
+12
-2
@@ -130,8 +130,18 @@ mod math {
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn log(x: ArgIntoFloat, base: OptionalArg<ArgIntoFloat>) -> f64 {
|
||||
base.map_or_else(|| x.to_f64().ln(), |base| x.to_f64().log(base.to_f64()))
|
||||
fn log(x: ArgIntoFloat, base: OptionalArg<ArgIntoFloat>, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = x.to_f64();
|
||||
base.map_or_else(
|
||||
|| {
|
||||
if x.is_nan() || x > 0.0_f64 {
|
||||
Ok(x.ln())
|
||||
} else {
|
||||
Err(vm.new_value_error("math domain error".to_owned()))
|
||||
}
|
||||
},
|
||||
|base| Ok(x.log(base.to_f64())),
|
||||
)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
|
||||
Reference in new issue
Block a user