-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadratic_solver.py
More file actions
38 lines (35 loc) · 2.02 KB
/
quadratic_solver.py
File metadata and controls
38 lines (35 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'''@package quadratic_solver
This package contains a quadratic solver'''
import math
def quadratic_solver(a, b, c, complex_allowed=False):
'''A solver for quadratic equations in the form ax^2+bx+c=0
Will return any roots of the equation in a list
If complex roots are allowed, the returned values may be complex
If complex roots are not allowed and there are no non-complex roots then a ValueError will be returned
If both the quadratic and linear coefficients are zero then a ValueError will be returned
@param a (numeric) The quadratic coefficient
@param b (numeric) The linear coefficient
@param c (numeric) The constant
@param complex_allowed (optional) (bool) If True, complex roots will be returned (if appropriate). If False, a ValueError will be returned if the discriminant is negative'''
if a == 0:
if b == 0:
##If the quadratic and linear coefficients are both zero, raise a ValueError
raise ValueError("Both 'a' and 'b' were zero, meaning there was no defined value for 'x'")
else:
##If the quadratic coefficient is zero, return the constant divided by the linear coefficient
return([-c/b])
##@var discriminant (float) The discriminant of the equation
discriminant = b ** 2 - 4 * a * c
if discriminant < 0:
if complex_allowed:
##If the discriminant is negative and complex roots are allowed, return two complex roots
return([(b - 1j*math.sqrt( - discriminant))/(2 * a), (b + 1j * math.sqrt(-discriminant))/(2 * a)])
else:
##If the discriminant is negative and complex roots are not allowed, raise a ValueError
raise ValueError("The discriminant was negative and complex results were not allowed")
elif discriminant == 0:
##If the discriminant is zero, return one real root
return([b / (2 * a)])
else:
##If the discriminant is positive, return two real roots
return([(b-math.sqrt(discriminant))/(2*a), (b+math.sqrt(discriminant))/(2*a)])