def quadformula(*, a, b, c):
import math
if a == 0:
raise ValueError("a cannot be 0")
# Calculate the discriminant
discriminant = b**2 - 4*a*c
# Check for real roots
if discriminant > 0:
root1 = (-b + math.sqrt(discriminant)) / (2*a)
root2 = (-b - math.sqrt(discriminant)) / (2*a)
return (format(root1, ".2f"), format(root2, ".2f"))
elif discriminant == 0:
root1 = -b / (2*a)
return (format(root1, ".2f"),)
else:
realPart = -b / (2*a)
imaginaryPart = math.sqrt(-discriminant) / (2*a)
return (format(realPart, ".2f") + " + " + format(imaginaryPart, ".2f") + "i",
format(realPart, ".2f") + " - " + format(imaginaryPart, ".2f") + "i")
print(quadformula(a=1, b=-5, c=-8))