Open the context
Choose the project folder and name the files or behavior involved.
Compute real roots of a quadratic using scaled coefficients and a cancellation-resistant formula. Test a repeated root and widely separated roots, rejecting a zero leading coefficient or unrepresentable results and returning no roots for a negative discriminant.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from math import isfinite, sqrt, copysign
def real_roots(a, b, c):
original = (a, b, c)
if not all(type(v) in (int, float) and isfinite(v) for v in original) or a == 0:
raise ValueError("finite quadratic coefficients")
if c == 0:
other = -b / a
if not isfinite(other) or (b != 0 and other == 0):
raise ValueError("unrepresentable root")
return [0.0] if b == 0 else sorted((0.0, other))
scale = max(map(abs, original))
a, b, c = (v / scale for v in original)
if any(v != 0 and s == 0 for v, s in zip(original, (a,b,c))):
raise ValueError("coefficient underflow")
d = b*b - 4*a*c
if d < 0: return []
if d == 0: roots = [-b / (2*a)]
else:
q = -0.5 * (b + copysign(sqrt(d), b))
if q == 0: raise ValueError("root underflow")
roots = sorted((q/a, c/q))
if not all(map(isfinite, roots)) or (original[2] != 0 and 0 in roots):
raise ValueError("unrepresentable roots")
return rootsThe example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.