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

# Problem calling math.cos()

 P: n/a I have the following module: ------------------------------- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j ) ac3 = ( 0, 0j ) ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin( math.radians( ph1 ) ) ) ac2 = complex( amp2 * math.cos( math.radians( ph2 ) ), amp2 * math.sin( math.radians( ph2 ) ) ) ac3 = ac1 + ac2 amp3 = math.abs( ac3 ) ph3 = math.atan( ac3.imag / ac3.real ) return [amp3, ph3] -------------------------------------- when I import it (electronics) in python.exe in windows2000 and try to use it, it croaks. ??? import math import electronics print electronics.ac_add_a_ph( 10, 0 , 6 , 45 ) Traceback (most recent call last): File "", line 1, in ? File "f:\devel\python\electronics.py", line 10, in ac_add_a_ph ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin( math ..radians( ph1 ) ) ) NameError: global name 'cos' is not defined global?? huh? what does abs stand for? why is that not absolute value? hmmm. Hmm, complex numbers, cool I don't even have any idea where C stands on this. Apr 22 '06 #1
11 Replies

 P: n/a [Robert Kern] ... ph3 = math.atan( ac3.imag / ac3.real ) ... Don't do that: atan2 is the correct way to compute the angle, because the signs of both inputs are needed to determine the correct quadrant. So do: ph3 = math.atan2(ac3.imag, ac3.real) instead. Apr 22 '06 #6

 P: n/a In article , "Tim Peters" wrote: [Robert Kern] ... ph3 = math.atan( ac3.imag / ac3.real ) ... Don't do that: atan2 is the correct way to compute the angle, because the signs of both inputs are needed to determine the correct quadrant. So do: ph3 = math.atan2(ac3.imag, ac3.real) instead. I certainly agree about using atan2() instead of atan(), but I'm surprised there's not an easier way to get the phase of a complex, just like abs() gives you the modulus. I can see why you wouldn't want to pollute the global namespace with another built-in just for this purpose, but surely a complex.phase property wouldn't hurt? Apr 22 '06 #7

 P: n/a [Roy Smith] I certainly agree about using atan2() instead of atan(), but I'm surprised there's not an easier way to get the phase of a complex, just like abs() gives you the modulus. I can see why you wouldn't want to pollute the global namespace with another built-in just for this purpose, but surely a complex.phase property wouldn't hurt? Or method. "Does anyone care enough to do the work?" is the real question. I don't :-) Apr 22 '06 #8