Modulus 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: 2020-03-02T09:58:19+05:30

269 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements