Given the numerical coefficients a, b and c, calculates the roots for any quadratic equation of the form ax^2 + bx + c >>> quadratic_roots(a=1, b=3, c=-4) (1.0, -4.0) >>> quadratic_roots(5, 6, 1) (-0.2, -1.0) >>> quadratic_roots(1, -6, 25) ((3+4j), (3-4j))
(a: int, b: int, c: int)
| 4 | |
| 5 | |
| 6 | def quadratic_roots(a: int, b: int, c: int) -> tuple[complex, complex]: |
| 7 | """ |
| 8 | Given the numerical coefficients a, b and c, |
| 9 | calculates the roots for any quadratic equation of the form ax^2 + bx + c |
| 10 | |
| 11 | >>> quadratic_roots(a=1, b=3, c=-4) |
| 12 | (1.0, -4.0) |
| 13 | >>> quadratic_roots(5, 6, 1) |
| 14 | (-0.2, -1.0) |
| 15 | >>> quadratic_roots(1, -6, 25) |
| 16 | ((3+4j), (3-4j)) |
| 17 | """ |
| 18 | |
| 19 | if a == 0: |
| 20 | raise ValueError("Coefficient 'a' must not be zero.") |
| 21 | delta = b * b - 4 * a * c |
| 22 | |
| 23 | root_1 = (-b + sqrt(delta)) / (2 * a) |
| 24 | root_2 = (-b - sqrt(delta)) / (2 * a) |
| 25 | |
| 26 | return ( |
| 27 | root_1.real if not root_1.imag else root_1, |
| 28 | root_2.real if not root_2.imag else root_2, |
| 29 | ) |
| 30 | |
| 31 | |
| 32 | def main(): |