Write a python function that solves a quadratic equation.
- User
Write a python function that solves a quadratic equation.
Assistant
Sure! Here is a Python function that solves a quadratic equation:
def solve_quadratic(a, b, c): """ Solves a quadratic equation of the form ax^2 + bx + c = 0. Args: a (float): The coefficient of the x^2 term. b (float): The coefficient of the x term. c (float): The constant term. Returns: tuple: A tuple containing the solutions to the equation, if they exist. """ # Calculate the discriminant discriminant = b**2 - 4*a*c # If the discriminant is positive, there are two distinct solutions if discriminant > 0: root1 = (-b + sqrt(discriminant)) / (2*a) root2 = (-b - sqrt(discriminant)) / (2*a) return root1, root2 # If the discriminant is zero, there is one repeated solution elif discriminant == 0: root = -b / (2*a) return root, root # If the discriminant is negative, there are no real solutions else: return None, None
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.