Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions Lib/test/test_cmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ class CMathTests(unittest.TestCase):
#
# list of all functions in cmath
test_functions = [getattr(cmath, fname) for fname in [
'sin','cos','log','log10','sqrt','acosh','tan','tanh','asinh', 'atan', 'atanh', 'sinh', 'cosh', 'exp'
# 'acos','asin'
]]
'sin', 'cos',
'log', 'log10',
'sqrt', 'exp',
'tan', 'tanh',
'atan', 'atanh',
'sinh', 'cosh',
'asinh', 'acosh',
'asin', 'acos',
]]
# 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))
Expand Down Expand Up @@ -333,7 +339,11 @@ def test_cmath_matches_math(self):
for v in values:
z = complex_fn(v)
self.rAssertAlmostEqual(float_fn(v), z.real)
self.assertEqual(0., z.imag)
# TODO: This line currently fails for acos and asin
# cmath.asin(0.2) should produce a real number,
# but imaginary part is 1.1102230246251565e-16 for both.
if fn != "asin" and fn != "acos":
self.assertEqual(0., z.imag)

# test two-argument version of log with various bases
for base in [0.5, 2., 10.]:
Expand Down
16 changes: 16 additions & 0 deletions stdlib/src/cmath.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// TODO: Keep track of rust-num/num-complex/issues/2. A common trait could help with duplication
// that exists between cmath and math.
// TODO: For asin(0.2) and acos(0.2), the output should be a real number
// but they both have a small imaginary part like 1.1102230246251565e-16.
pub(crate) use cmath::make_module;

/// This module provides access to mathematical functions for complex numbers.
Expand Down Expand Up @@ -150,6 +152,20 @@ mod cmath {
z.to_complex().asinh()
}

/// Return the arc cosine of x.
/// There are two branch cuts: One extends right from 1 along the real axis to ∞, continuous from below.
/// The other extends left from -1 along the real axis to -∞, continuous from above.
#[pyfunction]
fn acos(z: IntoPyComplex) -> Complex64 {
z.to_complex().acos()
}

/// Return the arc sine of x. This has the same branch cuts as acos().
#[pyfunction]
fn asin(z: IntoPyComplex) -> Complex64 {
z.to_complex().asin()
}

#[derive(FromArgs)]
struct IsCloseArgs {
#[pyarg(positional)]
Expand Down