Skip to content

Commit ccc3cf1

Browse files
committed
Fixing some bugs
Make sure that other is an instance from Complex class for all operations
1 parent f5a82b7 commit ccc3cf1

2 files changed

Lines changed: 63 additions & 8 deletions

File tree

cmpx.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

main.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def main():
1414
number = Complex(42) # --> Real = 42, Imaginary = 0
1515
print(number)
1616
# Instanciation 3
17-
number = Complex(12, -3.2) # --> Real = 12, Imaginary = -1
17+
number = Complex(12, -3.2) # --> Real = 12, Imaginary = -3.2
1818
print(number)
1919
# Instanciation 4
20-
number = Complex(im=13.2, re=5)
20+
number = Complex(im=13.2, re=5) # --> Real = 5, Imaginary = 13.2
2121
print(number)
2222

2323
# Basic operations
@@ -33,10 +33,31 @@ def main():
3333
print('({}) / ({}) = {}'.format(num1, num2, num1 / num2))
3434
# Floor division
3535
print('({}) // ({}) = {}'.format(num1, num2, num1 // num2))
36+
## Affecting the result directly on the number
37+
# Summation
38+
print('({}) += ({})'.format(num1, 2), end=' --> ')
39+
num1 += 2
40+
print(num1)
41+
# Substraction
42+
print('({}) -= ({})'.format(num1, num2), end=' --> ')
43+
num1 -= num2
44+
print(num1)
45+
# Multiplication
46+
print('({}) *= ({})'.format(num1, 2), end=' --> ')
47+
num1 *= 2
48+
print(num1)
49+
# Division
50+
print('({}) /= ({})'.format(num1, num2), end=' --> ')
51+
num1 /= num2
52+
print(num1)
53+
# Floor division
54+
print('({}) //= ({})'.format(num1, 2), end=' --> ')
55+
num1 /= 2
56+
print(num1)
3657
# Congugated of a complex number
37-
print('con({}) = {}'.format(num1, num1.con()))
58+
print('con({}) = {}'.format(num2, num2.con()))
3859
# Module of a complex number
39-
print('mod({}) = {}'.format(num1, num1.mod()))
60+
print('mod({}) = {}'.format(num2, num2.mod()))
4061

4162
# Basic comparisons
4263
num1 = Complex(im=3.1, re=-5)

0 commit comments

Comments
 (0)