So, I'm trying to write an algorithm croot(k, n), that returns the kth root of unity with n == n. I'm getting mostly the right answer, however it's giving me really weird representations that seem wrong for certain numbers. Here is an example.
import cmath
def croot(k, n):
if n<=0:
return None
return cmath.exp((2 * cmath.pi * 1j * k) / n)
for k in range(8):
print croot(k, 8)
Output is:
(1+0j)
(0.70710...+0.70710...j)
(6.12323399574e-17+1j)
Whoa whoa whoa. So the root when k = 2 and n = 8 is wrong, as it should just be i, which would be represented like 1j, or j, or 1.00000j, etc. Could somebody help me here? I'm doing this because I'm trying to write an FFT algorithm. I'm not very experienced with complex numbers and Python so I could very well be making a simple mistake.
Thanks,
If you guys need any additional information just ask.