Skip to content

Instantly share code, notes, and snippets.

@chuckbjones
Created February 17, 2015 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckbjones/0374a2854643c8beafe6 to your computer and use it in GitHub Desktop.
Save chuckbjones/0374a2854643c8beafe6 to your computer and use it in GitHub Desktop.
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")
else:
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