Problem calling math.cos()
Alex Martelli
aleaxit at yahoo.com
Sat Apr 22 15:48:13 EDT 2006
Sambo <sambo at void.com> wrote:
> 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 )
You're defining ac1, ac2, ac3 as tuples, each with two items. That's
silly: remove these three useless and confusing lines (the first two are
prety silly too). No damage, but, avoidable extra confusion.
> 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 "<stdin>", 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 ) ) )
[[some lines split to respect NNTP's constraint on 80-char lines]]
> NameError: global name 'cos' is not defined
> >>>
>
>
> global?? huh?
Weird -- I can't reproduce this; it's the kind of symptom you get when
mistakenly using a comma instead of a dot, for example, but I don't see
that error in your code.
What I _do_ see is an "AttributeError: 'module' object has no attribute
'abs'" on the amp3 assignment -- of course, because that's indeed the
fact (abs is a builtin, not a member to module math).
Most likely, you got a bit confused and didn't copy-and-paste exactly
what was going on.
> what does abs stand for? why is that not absolute value? hmmm.
abs does stand for absolute-value.
> Hmm, complex numbers, cool I don't even have any idea where C
> stands on this.
C has no stand on complex numbers.
Alex
More information about the Python-list
mailing list