@@ -13,16 +13,24 @@ def __init__(self, re=0, im=0):
1313 self .im = im
1414 # Operator overloading 1 : +
1515 def __add__ (self , other ):
16+ if not isinstance (other , Complex ):
17+ other = Complex (other )
1618 return Complex (self .re + other .re , self .im + other .im )
1719 # Operator overloading 2 : -
1820 def __sub__ (self , other ):
21+ if not isinstance (other , Complex ):
22+ other = Complex (other )
1923 return Complex (self .re - other .re , self .im - other .im )
2024 # Operator overloading 3 : *
2125 def __mul__ (self , other ):
26+ if not isinstance (other , Complex ):
27+ other = Complex (other )
2228 return Complex (self .re * other .re - self .im * other .im , self .re * other .im + self .im * other .re )
2329 # Operator overloading 4 : /
2430 def __truediv__ (self , other ):
2531 try :
32+ if not isinstance (other , Complex ):
33+ other = Complex (other )
2634 den = other * other .con ()
2735 num = self * other .con ()
2836 return Complex (num .re / den .re , num .im / den .re )
@@ -31,56 +39,85 @@ def __truediv__(self, other):
3139 # Operator overloading 5 : //
3240 def __floordiv__ (self , other ):
3341 try :
42+ if not isinstance (other , Complex ):
43+ other = Complex (other )
3444 den = other * other .con ()
3545 num = self * other .con ()
3646 return Complex (num .re // den .re , num .im // den .re )
3747 except ZeroDivisionError as err :
3848 print ('Error: {}' .format (err ))
3949 def __gt__ (self , other ):
50+ if not isinstance (other , Complex ):
51+ other = Complex (other )
4052 return self .mod () > other .mod ()
4153 # Operator overloading 7 : >=
4254 def __ge__ (self , other ):
55+ if not isinstance (other , Complex ):
56+ other = Complex (other )
4357 return self .mod () >= other .mod ()
4458 # Operator overloading 8: <
4559 def __lt__ (self , other ):
60+ if not isinstance (other , Complex ):
61+ other = Complex (other )
4662 return not self >= other
4763 # Operator overloading 9: <=
4864 def __le__ (self , other ):
65+ if not isinstance (other , Complex ):
66+ other = Complex (other )
4967 return not self > other
5068 # Operator overloading 10: ==
5169 def __eq__ (self , other ):
70+ if not isinstance (other , Complex ):
71+ other = Complex (other )
5272 return (self .re == other .re ) and (self .im == other .im )
5373 # Operator overloading 11: !=
5474 def __ne__ (self , other ):
75+ if not isinstance (other , Complex ):
76+ other = Complex (other )
5577 return not self == other
5678 # Operator overloading 12: +=
5779 def __iadd__ (self , other ):
80+ if not isinstance (other , Complex ):
81+ other = Complex (other )
5882 self .re += other .re
5983 self .im += other .im
84+ return Complex (self .re , self .im )
6085 # Operator overloading 13: -=
6186 def __isub__ (self , other ):
87+ if not isinstance (other , Complex ):
88+ other = Complex (other )
6289 self .re -= other .re
6390 self .im -= other .im
91+ return Complex (self .re , self .im )
6492 # Operator overloading 14: *=
6593 def __imul__ (self , other ):
94+ if not isinstance (other , Complex ):
95+ other = Complex (other )
6696 self .re = self .re * other .re - self .im * other .im
6797 self .im = self .re * other .im + self .im * other .re
98+ return Complex (self .re , self .im )
6899 # Operator overloading 15: /=
69100 def __idiv__ (self , other ):
70101 try :
102+ if not isinstance (other , Complex ):
103+ other = Complex (other )
71104 den = other * Complex (other .re , - other .im )
72105 num = self * Complex (other .re , - other .im )
73106 self .re = num .re / den .re
74107 self .im = num .im / den .re
108+ return Complex (self .re , self .im )
75109 except ZeroDivisionError as err :
76110 print ('Error: {}' .format (err ))
77111 # Operator overloading 16: //=
78112 def __ifloordiv__ (self , other ):
79113 try :
114+ if not isinstance (other , Complex ):
115+ other = Complex (other )
80116 den = other * Complex (other .re , - other .im )
81117 num = self * Complex (other .re , - other .im )
82118 self .re = num .re // den .re
83119 self .im = num .im // den .re
120+ return Complex (self .re , self .im )
84121 except ZeroDivisionError as err :
85122 print ('Error: {}' .format (err ))
86123 ## Helper functions
@@ -94,7 +131,4 @@ def con(self):
94131 def __str__ (self ):
95132 output = str (self .re ) + ' + ' + str (self .im ) + 'j' if (self .im != 0 ) else str (self .re )
96133 output = str (self .re ) + ' - ' + str (- self .im ) + 'j' if (self .im < 0 ) else output
97- return output
98- # Callable module
99- def __call__ (self ):
100- return self
134+ return output
0 commit comments