It solves quadratic equations, for both real and complex roots.
Please answer the pol to let me know how I'm doing.
This function just needs the python 3.x environment, no modules needed.
Function that solves quadratic equations
"""ADEGOKE OBASA, adegokeobasa@yahoo.com"""
"""this functions solves Quadratic equations
using the quadratic formula"""
from math import*
def quad(a,b,c):
"""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
#print(quad(1,3,4))
#>>>
#((-0.17712434446770464+0.5j), (-0.17712434446770464-0.5j))
#print(quad(1,9,4))
#>>>
#(-0.46887112585072543, -8.5311288741492746)
e-papa 13 Posting Pro in Training
TrustyTony 888 pyMod Team Colleague Featured Poster
e-papa 13 Posting Pro in Training
e-papa 13 Posting Pro in Training
e-papa 13 Posting Pro in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.