How does modulus work with complex numbers in Python?

PythonServer Side ProgrammingProgramming

Floor and modulus operators (// and % respectively) are not allowed to be used on complex number in Python 3.x. However, these operations are defined for complex numbers in 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)

Modulus of complex number operands returns their floor division multiplied by denominator

>>> x-(x//y)*y
(1-2j)
raja
Published on 05-Mar-2018 18:31:42
Advertisements