Merge pull request #3075 from ConnorTroy/cmath-log-log10

Add cmath log() and log10() methods
This commit is contained in:
Jeong YunWon
2021-09-18 13:12:36 +09:00
committed by GitHub
2 changed files with 24 additions and 4 deletions

View File

@@ -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

View File

@@ -89,6 +89,24 @@ mod cmath {
z.to_complex().cos()
}
/// 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<IntoPyFloat>) -> Complex64 {
z.to_complex().log(
base.into_option()
.map(|base| base.to_f64())
.unwrap_or(std::f64::consts::E),
)
}
/// Return the base-10 logarithm of z.
#[pyfunction]
fn log10(z: IntoPyComplex) -> Complex64 {
z.to_complex().log(10.0)
}
#[derive(FromArgs)]
struct IsCloseArgs {
#[pyarg(positional)]