use core::error use core::functions fn _qe_solution(a: A, b: B, c: B² / A, sign: Scalar) -> B / A = (-b + sign × sqrt(b² - 4 a c)) / 2 a @name("Solve quadratic equations") @url("https://en.wikipedia.org/wiki/Quadratic_equation") @description("Returns the solutions of the equation a x² + b x + c = 0") @example("quadratic_equation(2, -1, -1)", "Solve the equation $2x² -x -1 = 0$") fn quadratic_equation(a: A, b: B, c: B² / A) -> List = if a == 0 then if b == 0 then if c == 0 then error("infinitely many solutions") else [] else [-c / b] else if b² < 4 a c then [] else if b² == 4 a c then [-b / 2 a] else [_qe_solution(a, b, c, 1), _qe_solution(a, b, c, -1)]