🛠️ToolsShed

Quadratische Gleichung lösen

Löst quadratische Gleichungen ax² + bx + c = 0 mit schrittweisen Lösungen.

Enter coefficients

ax² + bx + c = 0

x = (−b ± √(b² − 4ac)) / 2a

Formula

  • Discriminant: Δ = b² − 4ac
  • Δ > 0: two distinct real roots
  • Δ = 0: one repeated root
  • Δ < 0: two complex conjugate roots

Häufig gestellte Fragen

Code-Implementierung

import cmath

def solve_quadratic(a: float, b: float, c: float):
    """Solve ax^2 + bx + c = 0. Returns two roots (may be complex)."""
    if a == 0:
        if b == 0:
            raise ValueError("Not an equation (a=0, b=0)")
        return (-c / b,)  # linear case
    disc = b**2 - 4*a*c
    sqrt_disc = cmath.sqrt(disc)
    x1 = (-b + sqrt_disc) / (2 * a)
    x2 = (-b - sqrt_disc) / (2 * a)
    return x1, x2

def format_root(r: complex) -> str:
    if r.imag == 0:
        return f"{r.real:.6g}"
    return f"{r.real:.4g} + {r.imag:.4g}i"

# Two real roots: x^2 - 5x + 6 = 0  ->  x = 3, 2
x1, x2 = solve_quadratic(1, -5, 6)
print(format_root(x1), format_root(x2))  # 3   2

# Complex roots: x^2 + 1 = 0  ->  x = ±i
x1, x2 = solve_quadratic(1, 0, 1)
print(format_root(x1), format_root(x2))  # 0 + 1i   0 + -1i

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.