[Tutor] Value Error

Dave Angel davea at davea.name
Thu Jun 13 12:39:18 CEST 2013


On 06/13/2013 04:09 AM, eryksun wrote:
> On Wed, Jun 12, 2013 at 6:31 PM, Dave Angel <davea at davea.name> wrote:
>>
>>>>> i = complex(0,1)
>
>      >>> 1j
>      1j

I had forgotten that notation for a complex literal.  I knew the magic 
syntax had j in it, but didn't remember it needs to be part of the 
numeric token.  Of course the output of the debugger should have 
reminded me, but you did a better job.

>
> http://en.wikipedia.org/wiki/Imaginary_unit#Alternative_notations
>
>
>>>>> cmath.sqrt(float((math.e **(i * math.pi)).real))
>
> The real/imag attributes are already floats:
>
>      >>> from math import e, pi, sin, cos
>
>      >>> cos(pi / 3), (e ** (1j * pi / 3)).real
>      (0.5000000000000001, 0.5000000000000001)
>
>      >>> sin(pi / 3), (e ** (1j * pi / 3)).imag
>      (0.8660254037844386, 0.8660254037844386)
>

Yeah, I know.  I originally put the float in to get rid of the small bit 
of imaginary noise that the complex exponentiation created.  When that 
failed, (apparently you can't use float() to get the real portion of a 
complex value), I added the .real() and forgot to remove the float().

In case this wasn't obvious to everyone, I was just playing with the

"e to the I PI is minus one" trick, then feeding that -1 to square root.

-- 
DaveA


More information about the Tutor mailing list