Average Error: 34.2 → 10.3
Time: 7.6s
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.0210080657580689 \cdot 10^{-59}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \leq 2.4459965781085926 \cdot 10^{+80}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(c \cdot a\right) \cdot -4\right)}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \end{array} \]
\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.0210080657580689 \cdot 10^{-59}:\\
\;\;\;\;-\frac{c}{b}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \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.0210080657580689e-59)
   (- (/ c b))
   (if (<= b 2.4459965781085926e+80)
     (/ (- (- b) (sqrt (fma b b (* (* c a) -4.0)))) (* a 2.0))
     (- (/ c b) (/ 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.0210080657580689e-59) {
		tmp = -(c / b);
	} else if (b <= 2.4459965781085926e+80) {
		tmp = (-b - sqrt(fma(b, b, ((c * a) * -4.0)))) / (a * 2.0);
	} else {
		tmp = (c / b) - (b / a);
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.2
Target21.3
Herbie10.3
\[\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.02100806575806888e-59

    1. Initial program 54.0

      \[\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.2

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

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

    if -1.02100806575806888e-59 < b < 2.4459965781085926e80

    1. Initial program 14.0

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

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

    if 2.4459965781085926e80 < b

    1. Initial program 43.3

      \[\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 5.0

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

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

Reproduce

herbie shell --seed 2022127 
(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)))