MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / quadratic_roots

Function quadratic_roots

maths/quadratic_equations_complex_numbers.py:6–29  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

4
5
6def 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
32def main():

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected