Python 中模数如何与复数配合使用?

pythonserver side programmingprogramming更新于 2025/8/13 17:48:17

在 Python 3.x 中,不允许对复数使用向下取整和模数运算符(分别为 // 和 %)。但是,这些操作在 Python 2.7.x 中针对复数进行了定义

Python 3

>>> x=9+2j
>>> y=2+1j
>>> x%y
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
x%y
TypeError: can't mod complex numbers.

Python 2.7

>>> x=9+2j
>>> y=2+1j
>>> x%y
(1-2j)

复数操作数的模数返回其底除法乘以分母

>>> x-(x//y)*y
(1-2j)

相关文章


有用资源