Skip to content

Commit 6735d90

Browse files
committed
Add print_res+Bug fix in solve+Updating version to 0.8.1
print_res is a function that is called whenever a restore occured
1 parent 547084d commit 6735d90

File tree

6 files changed

+27
-30
lines changed

6 files changed

+27
-30
lines changed

cmpx/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from .number import Complex
2-
from .error import print_err
2+
from .error import print_err, print_res
33

44
__title__ = 'PythonComplex'
55
__author__ = 'Omar Belghaouti'
66
__maintainer__ = 'Omar Belghaouti'
77
__email__ = 'bel_omar18@yahoo.com'
8-
__version__ = '0.7.8'
8+
__version__ = '0.8.1'
99
__license__ = 'MIT'
1010
__all__ = [
1111
'Complex'

cmpx/equations/solve.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,23 @@
88
"""
99
def solve(a, b, c=0):
1010
try:
11-
if a and b == 0:
11+
if a == 0 and b == 0:
1212
raise ValueError('At least give a number to the first two coefficents')
13-
if a and b != 0:
14-
if a == 0:
15-
return (Complex(-c/b))
16-
if b == 0:
17-
return (Complex(sqrt(-c/a))) if -c/a >= 0 else (Complex(0, sqrt(c/a)))
13+
if a != 0 and b != 0:
1814
delta = b**2 - 4 * a * c
1915
if(delta < 0):
2016
solution = (Complex(-b/2*a, -sqrt(-delta)/2*a),
2117
Complex(-b/2*a, sqrt(-delta)/2*a))
22-
elif(delta == 0):
18+
elif delta == 0:
2319
solution = (Complex(-b/2*a))
2420
else:
2521
solution = (Complex((-b - sqrt(delta))/2*a),
2622
Complex((-b + sqrt(delta))/2*a))
2723
return solution
24+
else:
25+
if a == 0:
26+
return (Complex(-c/b))
27+
if b == 0:
28+
return (Complex(sqrt(-c/a))) if -c/a >= 0 else (Complex(0, sqrt(c/a)))
2829
except (ZeroDivisionError, ValueError) as err:
2930
print_err(err)

cmpx/error.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@
1616
# function to print the error message
1717
def print_err(err):
1818
print(Fore.RED + '{}: {}'.format(err.__class__.__name__, err))
19+
print(Style.RESET_ALL, end='')
20+
21+
# function to print whenever restore is true
22+
def print_res():
23+
print(Fore.RED + 'Float division by zero')
24+
print(Fore.GREEN + 'Restoring last number')
1925
print(Style.RESET_ALL, end='')

cmpx/number.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
55
@author: Omar Belghaouti
66
"""
7-
from colorama import Fore, Style
8-
from .error import print_err
7+
from .error import print_err, print_res
98
from math import sqrt
109

1110
# Complex class for complex number manipulations
@@ -79,9 +78,7 @@ def __truediv__(self, other):
7978
den = other * other.con()
8079
num = self * other.con()
8180
if den.re == 0 and self.restore:
82-
print(Fore.RED + 'Float division by zero')
83-
print(Fore.GREEN + 'Restoring last number')
84-
print(Style.RESET_ALL, end='')
81+
print_res()
8582
return Complex(self.re, self.im, self.restore)
8683
return Complex(num.re / den.re, num.im / den.re, self.restore)
8784
except (ZeroDivisionError, ValueError) as err:
@@ -96,9 +93,7 @@ def __floordiv__(self, other):
9693
den = other * other.con()
9794
num = self * other.con()
9895
if den.re == 0 and self.restore:
99-
print(Fore.RED + 'Float division by zero')
100-
print(Fore.GREEN + 'Restoring last number')
101-
print(Style.RESET_ALL, end='')
96+
print_res()
10297
return Complex(self.re, self.im, self.restore)
10398
return Complex(num.re // den.re, num.im // den.re, self.restore)
10499
except (ZeroDivisionError, ValueError) as err:
@@ -208,9 +203,7 @@ def __idiv__(self, other):
208203
den = other * other.con()
209204
num = self * other.con()
210205
if den.re == 0 and self.restore:
211-
print(Fore.RED + 'Float division by zero')
212-
print(Fore.GREEN + 'Restoring last number')
213-
print(Style.RESET_ALL, end='')
206+
print_res()
214207
return Complex(self.re, self.im, self.restore)
215208
self.re = num.re / den.re
216209
self.im = num.im / den.re
@@ -227,9 +220,7 @@ def __ifloordiv__(self, other):
227220
den = other * other.con()
228221
num = self * other.con()
229222
if den.re == 0 and self.restore:
230-
print(Fore.RED + 'Float division by zero')
231-
print(Fore.GREEN + 'Restoring last number')
232-
print(Style.RESET_ALL, end='')
223+
print_res()
233224
return Complex(self.re, self.im, self.restore)
234225
self.re = num.re // den.re
235226
self.im = num.im // den.re

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup_args = dict(
77
name='cmpx',
8-
version='0.7.8',
8+
version='0.8.1',
99
description='A package for different operations on complex numbers',
1010
long_description_content_type='text/markdown',
1111
long_description=README,

tests/main.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from cmpx import Complex
1+
from cmpx import print_err
22

33
def main():
4-
a = 4 + 3 - 5j
5-
print(a.real)
6-
print(a.imag)
7-
cmplx = Complex(a, 1)
8-
print(cmplx)
4+
try:
5+
raise ValueError('Just for testing purposes')
6+
except ValueError as err:
7+
print_err(err)
98

109
if __name__ == '__main__': main()

0 commit comments

Comments
 (0)