# [Tutor] Complex roots

Dick Moores rdmoores at gmail.com
Thu Dec 9 14:42:09 CET 2004

```VERY helpful, Matt. Thanks.

One question: This seems to not compute accurately at all when the
imaginary part of number=complex() is other than 0. For example,
number=complex(5,5):

With
number=complex(5,5)
n=float(4)

the code produces
{0: (1.631, 0.0), 1: (0.0, 1.631), 2: (-1.631, 0.0), 3: (0.0, -1.631)}

And testing:
>>> (0.0 + 1.631j)**4
(7.0764565459210003+0j)

If that's the case, why the line
size=math.sqrt((number.real**2)+(number.imag**2)) ?

Dick

On Thu, 09 Dec 2004 11:52:15 +0000, Matt Williams
<matthew.williams at cancer.org.uk> wrote:
> Further to my previous post, please find some code below:
>
> Hope this helps,
> Matt
>
> #Complex number Roots
> #Matt Williams 9.12.04
>
> import math
>
> number=complex(0.5,0)
> n=float(3)
>
> size=math.sqrt((number.real**2)+(number.imag**2))
>
>
> root=1/n
>
> modulus=size**root
>
> theta=float(0)
> roots={}
> i=0
> while i<n:
>     y=round(modulus*(math.sin(theta)),3)
>     x=round(modulus*(math.cos(theta)),3)
>     roots[i]=(x,y)
>     theta=theta+arg
>     i=i+1
>
> print roots
>
```