![]() |
|
#1
|
|||
|
|||
complex number raised to a complexokay, one more question about this complex number class. i'm doing a method to raise a complex number to another complex number. In this instance, I thought I had the correct code but there seems to be a problem. It does work when the complex number is raised to itself, but otherwise, it doesn't. here's the code:
CPP / C++ / C Code:
why does it only work on complex numbers raised to themselves? how can I correct this problem? again, thanks in advance..... |
|
#2
|
|||
|
|||
Re: complex number raised to a complexQuote:
What works and what doesn't? Give examples. What do you mean by "complex number is raised to itself"? Do you mean that you only get the right answer when you give it "z to the power z" or what? Well, the end is in sight. You obviously have implemented several functions and operators to get this far: Complex log, Complex exp, overloaded '*' operator. How about testing them? Are you sure your complex functions and operators work up to this point? If you have tested the others thoroughly, then test the complex_power function. In particular, what input values are you giving your complex_power function. What did the program give as an output? What did you expect? Give us a specific example where the program gave the wrong answer. Since you have code that looks very much as if it implements the formula, the best way to debug is to put some easy things in it and see if it gives you the expected output. I always start with the "degenerate" cases. For complex numbers use (1, 0), (2, 0) etc. raise (2, 0) to the (3, 0) power (You should get (8, 0), right? --- possibly plus or minus roundoff error). Next try raising a complex number to the (2, 0) power. (1, 1) to the (2, 0) power should give (0, 2), right? Raise it to the (-1, 0) power. In other words, do some easy stuff for which you can easily test the answers. Raise a complex number to the (0, 1) power; to the (0, 2) power, etc. (A little less intuitive for us real-number-weenies, but still not too tough to check results). I wouldn't dream of submitting the problem of complex number raised to a complex power until I had thoroughly debugged and tested the simpler cases. Debug by printing out intermediate values in the various functions or... If you have overloaded the insertion and extraction operators ,<< and >>, for your class then your main program could have something like the following to check the steps in calculating complex_power. CPP / C++ / C Code:
Code:
The last one is e to the power(i * pi), which, as we all know, is exactly equal to -1. I haven't tried to dress up my output functions, since I just use them for debugging. In a finished program I would have more interesting ways to format the output, but, that's good enough for now. Regards, Dave Last edited by davekw7x : 05-Nov-2005 at 15:10.
|
|
#3
|
|||
|
|||
Re: complex number raised to a complexQuote:
|
|
#4
|
|||
|
|||
Re: complex number raised to a complexokay, i just realized what it's doing - the signs are reversed for the imaginary part.
it seems to be working now - I switched the line CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: complex number raised to a complexQuote:
What do you think the answer should be? Well, I have a little free time on this rainy Northern California Sunday afternoon, so let's break out the old slide rule. Now where did I put that thing? Ah, here it is. Using the old slide rule (Post Versalog, doncha know), I get: Code:
(Where all intermediate terms were rounded to about 3 sig figs --- slide rule accuracy, you know, subject to eyeball parallex and somewhat out-of-practice equipment operator.) So what did your program give? Did you print out the intermediate values as I suggested in the last post? You might see something like this: Code:
I guess there's some life in the old slide rule after all, and it kind of gives you a hint of the effects of roundoff error (assuming the computer answer is the "right" answer to the five or six significant digits shown). Now, if you get the same results from your program that I showed, but you don't think that's the right answer, then let's talk. (I could be wrong, you know --- it has happened.) If you get a different answer from the computer than what I showed, then I suggest that you print out the intermediate results in your power member function and see if they agree with my slide-rule answers or my sample program output. Somewhere in there there is a way to get to the bottom of things. Regards, Dave "The purpose of computing is insight, not numbers." ---Richard W. Hamming. "If you know the right answer, you can get the right answer." ---An anonymous "Dave" in his first EE lab at Louisiana State University in 19xx (don't ask) |
|
#6
|
|||
|
|||
Re: complex number raised to a complexwell, it seems that if either of the real parts are negative, the imaginary part must be declared as negative. if both the real parts are positive, the imaginary part is declared as positive. i'm hoping there are no other little nuances like that that i need to account for.....
|
|
#7
|
|||
|
|||
Re: complex number raised to a complexQuote:
Huh? Can you give an example that gives the wrong answer? (You must also tell me what you think the right answer is; I'm not going to do all the work.) I'm not saying your program is perfect (I don't even have your program to run). What I am saying is: test with whatever examples you want. If you get the wrong answer, fix it. If you can't see how to fix it then show me your code, show me your input, and show me your output and ask your questions. "It seems that..." is not really a question is it? What nuances are you talking about? The only restrictions are mathemetical: the absolute value of the base number must not be zero (or so close to zero that it looks like zero to the ::log function0 and the arguments of atan2 be legal (you can figure it out). Regards, Dave Last edited by davekw7x : 06-Nov-2005 at 20:59.
|
Recent GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need Help with my Cards Program (C++) | krisopotamus | C++ Forum | 2 | 06-Oct-2005 16:48 |
| Knights Tour - Reloaded . | kobi_hikri | C Programming Language | 12 | 03-Oct-2005 12:15 |
| Roman to decimal to roman | SpudNuts | C++ Forum | 2 | 16-Feb-2005 19:44 |
| Anyone can write a program code for this??? | chriskan76 | C Programming Language | 1 | 19-Oct-2004 20:25 |
| complex number | tinzi | C++ Forum | 2 | 01-Jun-2004 14:47 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The