473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

minor bug in cmath.asin?


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 2105
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
Erik Max Francis <ma*@alcyone.com> 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
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
Erik Max Francis <ma*@alcyone.com> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: EKL | last post by:
What do I have to do to guarantee that the objects that my code creates are reclaimed by only minor garbage collection runs? In other words, what do I have to do to only generate garbage for minor...
6
by: Christian Seberino | last post by:
I am looking at the ELSE home page and trying to figure out if I should invest the time to learn about the ELSE minor mode for Emacs. Is there any programmer out there using ELSE that is getting...
4
by: Michael C# | last post by:
Given a DateTime variable, what's the best way to check "minor" status (i.e., less than age 18 years) based on today's date? I came up with this: Dim minor As Boolean = true 'dtDOB is a...
0
by: outofstep | last post by:
Hit me up on AIM/YIM (oipaloi). I'm not a code monkey and would like to get some advice. Or hit up this thread right here. I'm using DEV C++ as my compiler. I'm trying to calculate...
0
by: Kszu | last post by:
Hi. I need a little help with cmath.h I wan't to make a dComplex number from 2 doubles Here are some lines from my program: #include <vcl.h> #pragma hdrstop #include <iostream.h> #include...
1
by: Mark Dickinson | last post by:
I was a little surprised by the following behaviour: Python 2.5 (r25, Oct 30 2006, 20:50:32) on darwin Type "help", "copyright", "credits" or "license" for more information. .... def...
11
by: The Cool Giraffe | last post by:
I have trouble making the following line compile. int a = round (2.6); I have, of course, included <cmathand i can go flooring and ceiling with no problems. However, the rounding seems...
16
by: giaro | last post by:
Hi, I'm doing some maths and I've just found that the standard library does not provide a standard PI value. In older post I see everyone says you must define it by yourself, but as some of the...
4
by: Peng Yu | last post by:
Hi, I need to look up the constants defined in cmath. Although I could read math.h directly, I'm wondering if there is a man page for it on linux system? Thanks, Peng
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.