Skip to content

Instantly share code, notes, and snippets.

Created Feb 17, 2015
What would you like to do?
Quadratic Formula in Python
#!/usr/bin/env python
import math
import sys
def quad(a, b, c):
disc = b**2 - 4*a*c
if disc < 0:
return None, None
disc_sqrt = math.sqrt(disc)
res_p = ((-b + disc_sqrt) / (2*a))
res_n = ((-b - disc_sqrt) / (2*a))
return res_p, res_n
if __name__ == "__main__":
if len(sys.argv) < 4:
sys.exit("usage: %s <a> <b> <c>" % sys.argv[0])
res_p, res_n = quad(float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]))
if res_p is None:
print("no solution")
print("%.3f, %.3f" % (res_p, res_n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment