455,530 Members | 1,571 Online Need help? Post your question and get tips & solutions from a community of 455,530 IT Pros & Developers. It's quick & easy.

# minor bug in cmath.asin?

 P: n/a 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/Element...ons/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, -- Edwin Jul 18 '05 #1
4 Replies

 P: n/a Edwin Young wrote: I think it should be 0+0j (+0 for both parts). The official formula is at http://functions.wolfram.com/Element...ons/ArcSin/02/ Well this is Mathematica's official formula ... :-). 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)) How is the original equation amiguous? -- __ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/ / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis \__/ You cannot step into the same river once. -- Cratylus Jul 18 '05 #2

 P: n/a Erik Max Francis writes: Edwin Young wrote: I think it should be 0+0j (+0 for both parts). The official formula is at http://functions.wolfram.com/Element...ons/ArcSin/02/ 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. 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)) 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. -- Edwin Jul 18 '05 #3

 P: n/a Edwin Young wrote: 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. -- __ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/ / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis \__/ Sit loosely in the saddle of life. -- Robert Louis Stevenson Jul 18 '05 #4

 P: n/a Erik Max Francis writes: Edwin Young wrote: 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. 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. -- Edwin Jul 18 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion. 