minor bug in cmath.asin?

E

Edwin Young

Hi,

I think there might (emphasize might) be a minor bug in Python's cmath library.

Basically, Python says cmath.asin(0) = 0-0j (+0 real part, -0 imaginary part).

I think it should be 0+0j (+0 for both parts). The official formula is at

http://functions.wolfram.com/ElementaryFunctions/ArcSin/02/

and is asin(z) = -i * log(i*z + sqrt(1 - z*z))

Which is slightly ambiguous. I read it as:

(+0 - 1i) * log(i*z + sqrt(1 - z*z))

But Python's implementation in cmathmodule.c is essentially:

-(i * log(i*z + sqrt(1 - z*z)))

Which is basically the same as = (-0 - 1i) * log(i*z + sqrt(1 - z*z))

The only difference is +0 vs -0, which not many people will care
about, but I thought I'd mention it.

Regards,
 
E

Edwin Young

Erik Max Francis said:
Well this is Mathematica's official formula ... :).

Mathworld doesn't document how Mathematica works, it's a repository of
generic mathetical info. "Official" is the wrong word, but I don't
have a more authoritative reference to hand.
How is the original equation amiguous?

It's not clear if the leading - sign affects the entire expression or
indicates that i is negative. Python's implementation uses the former
- I argue that the latter is more correct. The difference in result is
just whether you get +0 or -0.

I've also noticed that cmath.acos has the same issue, and that the
formula used for cmath.asinh is not quite accurate around 0 :
cmath.asinh(0) gives 4.44089e-16+0j, rather than 0 as expected.

Does anyone know the history of the cmath implementation? There are
some interesting formulas used for some of the trig functions and I'm
curious to know where they came from.
 
E

Erik Max Francis

Edwin said:
It's not clear if the leading - sign affects the entire expression or
indicates that i is negative. Python's implementation uses the former
- I argue that the latter is more correct.

What's the difference? Multiplication on complex numbers is
commutative.
I've also noticed that cmath.acos has the same issue, and that the
formula used for cmath.asinh is not quite accurate around 0 :
cmath.asinh(0) gives 4.44089e-16+0j, rather than 0 as expected.

Remember that these transcendental functions are usually computed by
approxmation. So it's not all that surprising that sin(0) isn't exactly
0.
 
E

Edwin Young

Erik Max Francis said:
What's the difference? Multiplication on complex numbers is
commutative.

Floating point math is weird. Among its oddities it has a positive
zero and a negative zero. The way Python implements asin, it produces
-0 in some circumstances when it should ideally produce +0, because it
multiples by (-0-1j) rather than (+0-1j). This in turn is because both
parts of the expression are negated, not just the imaginary part.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top