How does modulus work with complex numbers in Python?


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)

Updated on: 02-Mar-2020

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements