# Quadratic
import math
def num_roots(a: float, b: float, c: float) -> int:
"""Return the number of roots a quadratic function has
in the form of f(x) = ax^2 + bx + c"""
discriminant = b**2 - (4*a*c)
if discriminant > 0:
return(2)
elif discriminant == 0:
return(1)
else:
return(0)
def quadratic(a: float, b: float, c: float) -> float:
"""Return the root using addition of a quadratic in the form
f(x) = ax^2 + bx + c if it has real roots, otherwise return 0"""
number_roots = num_roots(a, b, c)
if number_roots == 0:
return 0
else:
return (-b + math.sqrt(b**2 - (4*a*c))) / (2*a)
assert num_roots(1,4,4) == 1, "num_roots perfect square"
assert num_roots(1,2,3) == 0, "num_roots imaginary"
assert num_roots(1,-8,12) == 2, "num_roots real"
assert abs(quadratic(1,4,4) + 2.0) < 0.0001, "quadratic perfect square"
assert quadratic(1,2,3) == 0, "quadratic imaginary"
assert abs(quadratic(1,-8,12) - 6.0) < 0.0001, "quadratic real"