From 6b79200f41de2107c65410b108346f0a07dd06ac Mon Sep 17 00:00:00 2001 From: Connor Troy Date: Thu, 16 Sep 2021 15:51:23 -0600 Subject: [PATCH 1/4] Add cmath log() and log10() methods --- vm/src/stdlib/cmath.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/vm/src/stdlib/cmath.rs b/vm/src/stdlib/cmath.rs index 530b6041b3..7a5bc44a61 100644 --- a/vm/src/stdlib/cmath.rs +++ b/vm/src/stdlib/cmath.rs @@ -89,6 +89,22 @@ mod cmath { z.to_complex().cos() } + /// Return the logarithm of z with respect to base. Default of base is 'e' + #[pyfunction] + fn log(z: IntoPyComplex, base: OptionalArg) -> Complex64 { + z.to_complex().log( + base.into_option() + .map(|base| base.to_f64()) + .unwrap_or(std::f64::consts::E), + ) + } + + /// Return the logarithm of z with respect to 10 + #[pyfunction] + fn log10(z: IntoPyComplex) -> Complex64 { + z.to_complex().log(10.0) + } + #[derive(FromArgs)] struct IsCloseArgs { #[pyarg(positional)] From 14c9744050aefeee6e8116e9d31d450c12f9a1a7 Mon Sep 17 00:00:00 2001 From: Connor Troy Date: Thu, 16 Sep 2021 16:54:58 -0600 Subject: [PATCH 2/4] Update test_cmath for log and log10 additions --- Lib/test/test_cmath.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_cmath.py b/Lib/test/test_cmath.py index 787b826680..7b23222bd4 100644 --- a/Lib/test/test_cmath.py +++ b/Lib/test/test_cmath.py @@ -51,11 +51,15 @@ complex_nans = [complex(x, y) for x, y in [ class CMathTests(unittest.TestCase): # TODO: RUSTPYTHON: - # Uncomment when functions have been added. Temporarily + # Uncomment when functions have been added. Temporarily # commented out to allow incremented addition of functions. # # list of all functions in cmath - test_functions = [getattr(cmath, fname) for fname in ['sqrt','sin','cos']] + test_functions = [getattr(cmath, fname) for fname in [ + 'sin','cos','log','log10','sqrt', + # 'exp','acos','acosh','asin','asinh', + # 'atan','atanh','sinh','cosh','tan','tanh' + ]] # test first and second arguments independently for 2-argument log # test_functions.append(lambda x : cmath.log(x, 1729. + 0j)) # test_functions.append(lambda x : cmath.log(14.-27j, x)) @@ -286,8 +290,6 @@ class CMathTests(unittest.TestCase): for arg in ["a", "long_string", "0", "1j", ""]: self.assertRaises(TypeError, f, arg) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_cmath_matches_math(self): # check that corresponding cmath and math functions are equal # for floats in the appropriate range From 9013505818d789edf7a19065ba7dadb98fe131a4 Mon Sep 17 00:00:00 2001 From: Connor Troy Date: Thu, 16 Sep 2021 23:36:54 -0600 Subject: [PATCH 3/4] Update cmath log docstring to match CPython Co-authored-by: Jim Fasarakis-Hilliard --- vm/src/stdlib/cmath.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vm/src/stdlib/cmath.rs b/vm/src/stdlib/cmath.rs index 7a5bc44a61..92381254c7 100644 --- a/vm/src/stdlib/cmath.rs +++ b/vm/src/stdlib/cmath.rs @@ -89,7 +89,9 @@ mod cmath { z.to_complex().cos() } - /// Return the logarithm of z with respect to base. Default of base is 'e' + /// log(z[, base]) -> the logarithm of z to the given base. + /// + /// If the base not specified, returns the natural logarithm (base e) of z. #[pyfunction] fn log(z: IntoPyComplex, base: OptionalArg) -> Complex64 { z.to_complex().log( From 36fb58def62e6693105a1ac5f2f0c57b4cfa103e Mon Sep 17 00:00:00 2001 From: Connor Troy Date: Thu, 16 Sep 2021 23:40:45 -0600 Subject: [PATCH 4/4] Update cmath log10 docstring to match CPython Co-authored-by: Jim Fasarakis-Hilliard --- vm/src/stdlib/cmath.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/stdlib/cmath.rs b/vm/src/stdlib/cmath.rs index 92381254c7..06e979e8c3 100644 --- a/vm/src/stdlib/cmath.rs +++ b/vm/src/stdlib/cmath.rs @@ -101,7 +101,7 @@ mod cmath { ) } - /// Return the logarithm of z with respect to 10 + /// Return the base-10 logarithm of z. #[pyfunction] fn log10(z: IntoPyComplex) -> Complex64 { z.to_complex().log(10.0)