Created
February 17, 2015 19:04
-
-
Save chuckbjones/0374a2854643c8beafe6 to your computer and use it in GitHub Desktop.
Quadratic Formula in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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