Average Error: 34.0 → 10.2
Time: 8.2s
Precision: binary64
\[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
\[\begin{array}{l} \mathbf{if}\;b \leq -1.9 \cdot 10^{-118}:\\ \;\;\;\;\frac{-c}{b}\\ \mathbf{elif}\;b \leq 9 \cdot 10^{+124}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b + \left(c \cdot a\right) \cdot -4}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;-\frac{b}{a}\\ \end{array} \]
(FPCore (a b c)
 :precision binary64
 (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))
(FPCore (a b c)
 :precision binary64
 (if (<= b -1.9e-118)
   (/ (- c) b)
   (if (<= b 9e+124)
     (/ (- (- b) (sqrt (+ (* b b) (* (* c a) -4.0)))) (* a 2.0))
     (- (/ b a)))))
double code(double a, double b, double c) {
	return (-b - sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
}
double code(double a, double b, double c) {
	double tmp;
	if (b <= -1.9e-118) {
		tmp = -c / b;
	} else if (b <= 9e+124) {
		tmp = (-b - sqrt(((b * b) + ((c * a) * -4.0)))) / (a * 2.0);
	} else {
		tmp = -(b / a);
	}
	return tmp;
}
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    code = (-b - sqrt(((b * b) - (4.0d0 * (a * c))))) / (2.0d0 * a)
end function
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8) :: tmp
    if (b <= (-1.9d-118)) then
        tmp = -c / b
    else if (b <= 9d+124) then
        tmp = (-b - sqrt(((b * b) + ((c * a) * (-4.0d0))))) / (a * 2.0d0)
    else
        tmp = -(b / a)
    end if
    code = tmp
end function
public static double code(double a, double b, double c) {
	return (-b - Math.sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
}
public static double code(double a, double b, double c) {
	double tmp;
	if (b <= -1.9e-118) {
		tmp = -c / b;
	} else if (b <= 9e+124) {
		tmp = (-b - Math.sqrt(((b * b) + ((c * a) * -4.0)))) / (a * 2.0);
	} else {
		tmp = -(b / a);
	}
	return tmp;
}
def code(a, b, c):
	return (-b - math.sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a)
def code(a, b, c):
	tmp = 0
	if b <= -1.9e-118:
		tmp = -c / b
	elif b <= 9e+124:
		tmp = (-b - math.sqrt(((b * b) + ((c * a) * -4.0)))) / (a * 2.0)
	else:
		tmp = -(b / a)
	return tmp
function code(a, b, c)
	return Float64(Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c))))) / Float64(2.0 * a))
end
function code(a, b, c)
	tmp = 0.0
	if (b <= -1.9e-118)
		tmp = Float64(Float64(-c) / b);
	elseif (b <= 9e+124)
		tmp = Float64(Float64(Float64(-b) - sqrt(Float64(Float64(b * b) + Float64(Float64(c * a) * -4.0)))) / Float64(a * 2.0));
	else
		tmp = Float64(-Float64(b / a));
	end
	return tmp
end
function tmp = code(a, b, c)
	tmp = (-b - sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
end
function tmp_2 = code(a, b, c)
	tmp = 0.0;
	if (b <= -1.9e-118)
		tmp = -c / b;
	elseif (b <= 9e+124)
		tmp = (-b - sqrt(((b * b) + ((c * a) * -4.0)))) / (a * 2.0);
	else
		tmp = -(b / a);
	end
	tmp_2 = tmp;
end
code[a_, b_, c_] := N[(N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_] := If[LessEqual[b, -1.9e-118], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 9e+124], N[(N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], (-N[(b / a), $MachinePrecision])]]
\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\begin{array}{l}
\mathbf{if}\;b \leq -1.9 \cdot 10^{-118}:\\
\;\;\;\;\frac{-c}{b}\\

\mathbf{elif}\;b \leq 9 \cdot 10^{+124}:\\
\;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b + \left(c \cdot a\right) \cdot -4}}{a \cdot 2}\\

\mathbf{else}:\\
\;\;\;\;-\frac{b}{a}\\


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original34.0
Target20.9
Herbie10.2
\[\begin{array}{l} \mathbf{if}\;b < 0:\\ \;\;\;\;\frac{c}{a \cdot \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\\ \end{array} \]

Derivation

  1. Split input into 3 regimes
  2. if b < -1.9e-118

    1. Initial program 51.5

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
    2. Simplified51.5

      \[\leadsto \color{blue}{\left(b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}\right) \cdot \frac{-0.5}{a}} \]
    3. Taylor expanded in b around -inf 11.6

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
    4. Simplified11.6

      \[\leadsto \color{blue}{\frac{-c}{b}} \]

    if -1.9e-118 < b < 9.0000000000000008e124

    1. Initial program 11.3

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]

    if 9.0000000000000008e124 < b

    1. Initial program 53.3

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
    2. Simplified53.4

      \[\leadsto \color{blue}{\left(b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}\right) \cdot \frac{-0.5}{a}} \]
    3. Applied egg-rr36.0

      \[\leadsto \color{blue}{\frac{1}{\frac{a}{\left(b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)\right) \cdot -0.5}}} \]
    4. Taylor expanded in a around 0 2.5

      \[\leadsto \color{blue}{-1 \cdot \frac{b}{a}} \]
    5. Simplified2.5

      \[\leadsto \color{blue}{\frac{-b}{a}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification10.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -1.9 \cdot 10^{-118}:\\ \;\;\;\;\frac{-c}{b}\\ \mathbf{elif}\;b \leq 9 \cdot 10^{+124}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b + \left(c \cdot a\right) \cdot -4}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;-\frac{b}{a}\\ \end{array} \]

Reproduce

herbie shell --seed 2022209 
(FPCore (a b c)
  :name "The quadratic formula (r2)"
  :precision binary64

  :herbie-target
  (if (< b 0.0) (/ c (* a (/ (+ (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))) (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))

  (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))