# How can I calculate only the imaginary part of a complex number?

So I have the following function which the x will be the difference between two point:

def complexDis(x): return sqrt((x.real()^2)+((x.imag()*-i)^2))

But it seems like i can’t actually operate the .real or .imag number or at least I can’t print it out with N(conplexDis(b-a)) so im asking how can I achieve my goal?

edit retag close merge delete

## Comments

What goes wrong if you do n(complexDis(b-a).imag()), or the same with real()?

@John-Palmieri same both imag and real after squaring them they both doesn’t output real number

Hi andrewwyg

maybe there is something I'm missing, but i don't see where the problem is?

sqrt((2^2)+((3*-i)^2))= sqrt(-5) so it is not a real number ?

def complexDis(x):
return sqrt(((x.real())^2)+(((imag_part(x))*-i)^2))
var('z')
z=2+3*I
show("complexDis(z) :\t",complexDis(z),"\t, \t z norm :\t", z.norm())


I think we need more information: what are a and b, and what are you expecting for your output?

Note that x.imag() is a real number, so (x.imag()*-i) is purely imaginary. Its square will be negative, so your function may certainly end up computing the square root of a negative number.