Average Error: 31.0 → 9.8
Time: 13.3s
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 -2.47012258144473 \cdot 10^{+164}:\\ \;\;\;\;\frac{\left(-b\right) - b}{2 \cdot a}\\ \mathbf{elif}\;b \leq 3.100166812718591 \cdot 10^{-29}:\\ \;\;\;\;\frac{\mathsf{fma}\left(-1, b, \sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c}\right)}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \end{array} \]
\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 -2.47012258144473 \cdot 10^{+164}:\\
\;\;\;\;\frac{\left(-b\right) - b}{2 \cdot a}\\

\mathbf{elif}\;b \leq 3.100166812718591 \cdot 10^{-29}:\\
\;\;\;\;\frac{\mathsf{fma}\left(-1, b, \sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c}\right)}{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 -2.47012258144473e+164)
   (/ (- (- b) b) (* 2.0 a))
   (if (<= b 3.100166812718591e-29)
     (/ (fma -1.0 b (sqrt (- (* b b) (* (* a 4.0) c)))) (* 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 <= -2.47012258144473e+164) {
		tmp = (-b - b) / (2.0 * a);
	} else if (b <= 3.100166812718591e-29) {
		tmp = fma(-1.0, b, sqrt((b * b) - ((a * 4.0) * c))) / (2.0 * a);
	} else {
		tmp = -(c / b);
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original31.0
Target19.0
Herbie9.8
\[\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 < -2.47012258144473025e164

    1. Initial program 36.5

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

      \[\leadsto \frac{\left(-b\right) + \color{blue}{-1 \cdot b}}{2 \cdot a} \]
    3. Simplified1.6

      \[\leadsto \frac{\left(-b\right) + \color{blue}{\left(-b\right)}}{2 \cdot a} \]

    if -2.47012258144473025e164 < b < 3.1001668127185908e-29

    1. Initial program 14.1

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    2. Applied *-un-lft-identity_binary6414.1

      \[\leadsto \frac{\left(-\color{blue}{1 \cdot b}\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    3. Applied distribute-lft-neg-in_binary6414.1

      \[\leadsto \frac{\color{blue}{\left(-1\right) \cdot b} + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    4. Applied fma-def_binary6414.1

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

    if 3.1001668127185908e-29 < b

    1. Initial program 54.4

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

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

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

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

Reproduce

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