Average Error: 34.2 → 9.7
Time: 5.5s
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 -4.3 \cdot 10^{+148}:\\ \;\;\;\;\frac{b \cdot -2}{2 \cdot a}\\ \mathbf{elif}\;b \leq 3.05 \cdot 10^{-55}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(a \cdot -4, c, b \cdot b\right)} - b}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \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 -4.3e+148)
   (/ (* b -2.0) (* 2.0 a))
   (if (<= b 3.05e-55)
     (/ (- (sqrt (fma (* a -4.0) c (* b b))) b) (* 2.0 a))
     (/ (- c) b))))
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 <= -4.3e+148) {
		tmp = (b * -2.0) / (2.0 * a);
	} else if (b <= 3.05e-55) {
		tmp = (sqrt(fma((a * -4.0), c, (b * b))) - b) / (2.0 * a);
	} else {
		tmp = -c / b;
	}
	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 <= -4.3e+148)
		tmp = Float64(Float64(b * -2.0) / Float64(2.0 * a));
	elseif (b <= 3.05e-55)
		tmp = Float64(Float64(sqrt(fma(Float64(a * -4.0), c, Float64(b * b))) - b) / Float64(2.0 * a));
	else
		tmp = Float64(Float64(-c) / b);
	end
	return 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, -4.3e+148], N[(N[(b * -2.0), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 3.05e-55], N[(N[(N[Sqrt[N[(N[(a * -4.0), $MachinePrecision] * c + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $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 -4.3 \cdot 10^{+148}:\\
\;\;\;\;\frac{b \cdot -2}{2 \cdot a}\\

\mathbf{elif}\;b \leq 3.05 \cdot 10^{-55}:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(a \cdot -4, c, b \cdot b\right)} - b}{2 \cdot a}\\

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


\end{array}

Error

Target

Original34.2
Target20.6
Herbie9.7
\[\begin{array}{l} \mathbf{if}\;b < 0:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{a \cdot \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 < -4.3000000000000002e148

    1. Initial program 62.1

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
    2. Applied egg-rr62.1

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

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

      \[\leadsto \frac{\color{blue}{b \cdot -2}}{2 \cdot a} \]

    if -4.3000000000000002e148 < b < 3.0500000000000001e-55

    1. Initial program 12.5

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
    2. Applied egg-rr12.7

      \[\leadsto \frac{\left(-b\right) + \color{blue}{{\left(\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)\right)}^{0.25} \cdot {\left(\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)\right)}^{0.25}}}{2 \cdot a} \]
    3. Applied egg-rr12.5

      \[\leadsto \frac{\left(-b\right) + \color{blue}{\sqrt{\mathsf{fma}\left(-4 \cdot a, c, b \cdot b\right)}}}{2 \cdot a} \]

    if 3.0500000000000001e-55 < b

    1. Initial program 53.9

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
    2. Taylor expanded in b around inf 8.3

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
    3. Simplified8.3

      \[\leadsto \color{blue}{\frac{-c}{b}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification9.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -4.3 \cdot 10^{+148}:\\ \;\;\;\;\frac{b \cdot -2}{2 \cdot a}\\ \mathbf{elif}\;b \leq 3.05 \cdot 10^{-55}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(a \cdot -4, c, b \cdot b\right)} - b}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \]

Reproduce

herbie shell --seed 2022190 
(FPCore (a b c)
  :name "quadp (p42, positive)"
  :precision binary64

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

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