It solves quadratic equations, for both real and complex roots.
This function just needs the python 3.x environment, no modules needed.

``````"""ADEGOKE OBASA, adegokeobasa@yahoo.com"""
from math import*
"""solves quadratic equations of the form
aX^2+bX+c, inputs a,b,c,
works for all roots(real or complex)"""
root=b**2-4*a*c
if root <0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
return x1,x2
else:
x1=(-b+sqrt(root))/2*a
x2=(-b-sqrt(root))/2*a
return x1,x2

#example
#>>>
#((-0.17712434446770464+0.5j), (-0.17712434446770464-0.5j))
#>>>
#(-0.46887112585072543, -8.5311288741492746)``````

Please i need the plaudits in the house to scrutinize this snippet.

``````from __future__ import print_function, division
from cmath import *
delta = 1e-9

form aX^2+bX+c, inputs a,b,c,
works for all roots(real or complex)

"""
discriminant = (b ** 2) -  4  * a * c
print('Discriminant', discriminant) # debug
if discriminant > 0:
return (-b + sqrt(discriminant)) / (2 * a), (-b - sqrt(discriminant)) / (2 * a)
elif discriminant == 0:
return (-b +  sqrt(discriminant)) / (2 * a)
else:
return (-b +  sqrt(discriminant))  / (2 * a) , (-b -  sqrt(discriminant))  / (2 * a)

"""solves quadratic equations of the form
aX^2+bX+c, inputs a,b,c,
works for all roots(real or complex)"""
root=b**2-4*a*c
if root <0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
return x1,x2
else:
x1=(-b+sqrt(root))/2*a
x2=(-b-sqrt(root))/2*a
return x1,x2
#example
for vals in (2, 4, -30), (1,3,4), (1,9,4):
print(40 * '-')
print('Testing function %r' % test.__name__)
solutions = test(*vals)
for solution in solutions:
a,b,c = vals
print(vals, 'solution', solution)
print('%s < %f' % ((a * (solution ** 2) + b * solution + c), delta))
assert abs(a * (solution ** 2) + b * solution + c) < delta``````

This is one rule that I am still not yet internalized from the formatting of the doc strings:

- PEP 257 describes good docstring conventions. Note that most
importantly, the """ that ends a multiline docstring should be on a line
by itself, and preferably preceded by a blank line, e.g.:

"""Return a foobang

Optional plotz says to frobnicate the bizbaz first.

"""

- For one liner docstrings, it's okay to keep the closing """ on the same
line.

I'm very sorry, will debug and do as you said.

``````from __future__ import print_function, division
from cmath import *
delta = 1e-9

form aX^2+bX+c, inputs a,b,c,
works for all roots(real or complex)

"""
discriminant = (b ** 2) -  4  * a * c
print('Discriminant', discriminant) # debug
if discriminant > 0:
return (-b + sqrt(discriminant)) / (2 * a), (-b - sqrt(discriminant)) / (2 * a)
elif discriminant == 0:
return (-b +  sqrt(discriminant)) / (2 * a)
else:
return (-b +  sqrt(discriminant))  / (2 * a) , (-b -  sqrt(discriminant))  / (2 * a)

"""solves quadratic equations of the form
aX^2+bX+c, inputs a,b,c,
works for all roots(real or complex)"""
root=b**2-4*a*c
if root <0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
return x1,x2
else:
x1=(-b+sqrt(root))/2*a
x2=(-b-sqrt(root))/2*a
return x1,x2
#example
for vals in (2, 4, -30), (1,3,4), (1,9,4):
print(40 * '-')
print('Testing function %r' % test.__name__)
solutions = test(*vals)
for solution in solutions:
a,b,c = vals
print(vals, 'solution', solution)
print('%s < %f' % ((a * (solution ** 2) + b * solution + c), delta))
assert abs(a * (solution ** 2) + b * solution + c) < delta``````

This is one rule that I am still not yet internalized from the formatting of the doc strings:

Thanks for seeing the flaws in my program, actually you really helped me because your post in my topic

What does math domain error mean

really helped, here is the quadratic equation, back and better. Yet again i will like you to also debug and test with various values, I believe there is more. This is the new function using the knowledge of cmath module in python.

``````"""ADEGOKE OBASA, adegokeobasa@yahoo.com"""
import math
import cmath
"""solves quadratic equations of the form
aX^2+bX+c, inputs a,b,c,
works for all roots(real or complex)"""
D=b**2-4*a*c
if D <0:
x1=(-b+cmath.sqrt(D))/2*a
x2=(-b-cmath.sqrt(D))/2*a
return x1,x2
else:
x1=(-b+math.sqrt(D))/2*a
x2=(-b-math.sqrt(D))/2*a
return x1,x2

#example
#>>>
#(3j, -3j)
#>>>
#(2.0, -2.0)``````

Thanks.

``````>>> 1./2*2
1.0
>>> 1./(2*2)
0.25``````

You can borrow the test cases from my alternative Quadratic Equation solver I posted today as example for posting code snippets.
I recommend to do install improved Python module installer

``easy_install pip``

and install program pep8 for checking correct form of Python program. Very educating and helps to learn good habits.