Average Error: 33.9 → 10.1
Time: 7.5s
Precision: binary64
\[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
\[\begin{array}{l} \mathbf{if}\;b \leq -5.119040201792971 \cdot 10^{+152}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 1.0751418463289444 \cdot 10^{-13}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right) + \mathsf{fma}\left(-c, a \cdot 4, a \cdot \left(c \cdot 4\right)\right)} - b}{a \cdot 2}\\ \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 -5.119040201792971e+152)
   (- (/ c b) (/ b a))
   (if (<= b 1.0751418463289444e-13)
     (/
      (-
       (sqrt
        (+ (fma b b (* c (* a -4.0))) (fma (- c) (* a 4.0) (* a (* c 4.0)))))
       b)
      (* a 2.0))
     (/ (- 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 <= -5.119040201792971e+152) {
		tmp = (c / b) - (b / a);
	} else if (b <= 1.0751418463289444e-13) {
		tmp = (sqrt((fma(b, b, (c * (a * -4.0))) + fma(-c, (a * 4.0), (a * (c * 4.0))))) - b) / (a * 2.0);
	} else {
		tmp = -c / b;
	}
	return tmp;
}
function code(a, b, c)
	return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a))
end
function code(a, b, c)
	tmp = 0.0
	if (b <= -5.119040201792971e+152)
		tmp = Float64(Float64(c / b) - Float64(b / a));
	elseif (b <= 1.0751418463289444e-13)
		tmp = Float64(Float64(sqrt(Float64(fma(b, b, Float64(c * Float64(a * -4.0))) + fma(Float64(-c), Float64(a * 4.0), Float64(a * Float64(c * 4.0))))) - b) / Float64(a * 2.0));
	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[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_] := If[LessEqual[b, -5.119040201792971e+152], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.0751418463289444e-13], N[(N[(N[Sqrt[N[(N[(b * b + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[((-c) * N[(a * 4.0), $MachinePrecision] + N[(a * N[(c * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\begin{array}{l}
\mathbf{if}\;b \leq -5.119040201792971 \cdot 10^{+152}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\

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

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


\end{array}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

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

Derivation

  1. Split input into 3 regimes
  2. if b < -5.11904020179297066e152

    1. Initial program 63.3

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

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

    if -5.11904020179297066e152 < b < 1.0751418463289444e-13

    1. Initial program 14.0

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

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

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

    if 1.0751418463289444e-13 < b

    1. Initial program 55.6

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

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

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

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

Reproduce

herbie shell --seed 2022152 
(FPCore (a b c)
  :name "The quadratic formula (r1)"
  :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)))