-1
>>> a=4.
>>> b=3.
r = sqrt(a ** 2 + b ** 2)
x = atan(b/a)
a = r * cos(x)
b = r * sin(x)
k = 0
y = (2 * pi * k + x) /3

root1 = r ** (1./3) * ( cos(y)+ 1j * sin(y) )
root11 = root1**4/root1
>>> root11
(3.999999999999999+2.999999999999999j)
>>> print root11
(4+3j)

How do I print out this complex number in this '(3.999999999999999+2.999999999999999j)' form? I tried

>>> print '%15f %15fi' % (root11.real, root11.imag)
4.000000        3.000000i

please help

pythoniku
  • 3,532
  • 6
  • 23
  • 30

3 Answers3

2

You may also use the new format syntax,

print "{0:.15f}+{1:.15f}i".format(root11.real, root11.imag)
phil1710
  • 51
  • 4
1

You should use

print '%.15f %.15fi' % (root11.real, root11.imag)

Notice there is a . before the 15f to format the precision after the decimal. If you do not have the ., you are specifying the field width.

In my machine (Python 2.7.3), The result is:

3.999999999999999 2.999999999999999i
dawg
  • 98,345
  • 23
  • 131
  • 206
Tanky Woo
  • 4,906
  • 9
  • 44
  • 75
1

As one of the comments suggest print root11.__repr__() works perfectly

pythoniku
  • 3,532
  • 6
  • 23
  • 30