2013-06-03 30 views
-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) 

如何在此'(3.999999999999999 + 2.999999999999999j)'表格中打印出此复数?我试图蟒复数打印结果与shell输出不同

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

请帮助

+2

shell会打印十进制数字的'repr'输出。 [用浮漂并串转换奇怪的行为]的 –

+0

可能重复(http://stackoverflow.com/questions/13345334/strange-behaviour-with-floats-and-string-conversion) –

+0

因为'print'(实际上'浮动。 __str__'和'%15f'四舍五入的数字 – JBernardo

回答

1

由于意见的一个完美的建议print root11.__repr__()作品

1

您应该使用

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

通知存在15f.小数点后格式化精度。如果您没有.,则指定字段宽度。

在我的机器上(Python的2.7.3),其结果是:

3.999999999999999 2.999999999999999i 
2

您也可以使用新的格式语法,

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