Skip to content

Commit 547084d

Browse files
committed
Bug fix
1 parent 38f8971 commit 547084d

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

cmpx/equations/solve.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
from cmpx import Complex
33
from cmpx import print_err
44
from math import sqrt
5-
6-
# Function to solve a second degree (or linear) equation with passing arguments seperatly
5+
"""
6+
Function to solve a second degree (or linear) equation with passing arguments seperatly
7+
eq = a * x**2 + b * x + c
8+
"""
79
def solve(a, b, c=0):
810
try:
9-
if a or a and b == 0 :
11+
if a and b == 0:
1012
raise ValueError('At least give a number to the first two coefficents')
11-
if c != 0:
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)))
1218
delta = b**2 - 4 * a * c
1319
if(delta < 0):
1420
solution = (Complex(-b/2*a, -sqrt(-delta)/2*a),
@@ -19,7 +25,5 @@ def solve(a, b, c=0):
1925
solution = (Complex((-b - sqrt(delta))/2*a),
2026
Complex((-b + sqrt(delta))/2*a))
2127
return solution
22-
else:
23-
return (Complex(-b/a))
2428
except (ZeroDivisionError, ValueError) as err:
2529
print_err(err)

0 commit comments

Comments
 (0)