RRootEquinox
Workflows

Coding workflows · Stable quadratic roots

03
Built around your work

A concrete starting point

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.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Stable quadratic roots

A concrete starting point
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 roots

The 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.

AI coding workspace

Your next change starts here.

Get the Windows app ↗