如何舍入一个复数? [英] How to round up a complex number?

查看:137
本文介绍了如何舍入一个复数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个复数(例如 1.9999999999999998-2j )舍入为 2-2j

当我尝试使用

  print(round(x,2)) 

显示

 追踪(最近一次通话最近):
文件 C:\Python34\FFT.py,第22行,在< module>中
print(round(x,2))
TypeError:类型复杂未定义__round__方法


解决方案

分别将实部和虚部进行舍入并将其组合:

 >>> num = 1.9999999999999998-2j 
>> round(数字实数,2)+ round(数字imag,2)* 1j
(2-2j)


How can I round up a complex number (e.g. 1.9999999999999998-2j) as 2-2j?

When I tried using

print(round(x,2))

it showed

Traceback (most recent call last):
  File "C:\Python34\FFT.py", line 22, in <module>
    print(round(x,2))
TypeError: type complex doesn't define __round__ method

解决方案

Round real part and imaginary part separately and combine them:

>>> num = 1.9999999999999998-2j
>>> round(num.real, 2) + round(num.imag, 2) * 1j
(2-2j)

这篇关于如何舍入一个复数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆