Quadratic

Solve quadratic equations with impunity using Python (and complex numbers)

#!/usr/bin/env python3
import math,sys
a,b,c=map(float,sys.argv[1:])
d=b*b-4*a*c
if d>=0:x1=(-b+math.sqrt(d))/(2*a);x2=(-b-math.sqrt(d))/(2*a)
else:r=-b/(2*a);i=math.sqrt(-d)/(2*a);x1,x2=complex(r,i),complex(r,-i)
print("The function has","two real roots: {} and {}".format(x1,x2)if d>0 else
"one double root: {}".format(x1)if d==0 else
"two complex roots: {} and {}".format(x1,x2))

Unknown's avatar

About Linuxgal

Need a spiritual home? Consider joining us at Mary Queen of the Universe Latter-day Buddhislamic Free Will Christian UFO Synagogue of Vishnu
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment