Average Error: 34.0 → 10.0
Time: 11.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 -8.184097496269323 \cdot 10^{-116}:\\ \;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\ \mathbf{elif}\;b \leq 2.112249961483768 \cdot 10^{+119}:\\ \;\;\;\;-0.5 \cdot \frac{b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}}{a}\\ \mathbf{else}:\\ \;\;\;\;-0.5 \cdot \left(2 \cdot \left(\frac{b}{a} - \frac{c}{b}\right)\right)\\ \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 -8.184097496269323 \cdot 10^{-116}:\\
\;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\

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

\mathbf{else}:\\
\;\;\;\;-0.5 \cdot \left(2 \cdot \left(\frac{b}{a} - \frac{c}{b}\right)\right)\\


\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 -8.184097496269323e-116)
   (* -0.5 (* 2.0 (/ c b)))
   (if (<= b 2.112249961483768e+119)
     (* -0.5 (/ (+ b (sqrt (fma a (* c -4.0) (* b b)))) a))
     (* -0.5 (* 2.0 (- (/ b 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 <= -8.184097496269323e-116) {
		tmp = -0.5 * (2.0 * (c / b));
	} else if (b <= 2.112249961483768e+119) {
		tmp = -0.5 * ((b + sqrt(fma(a, (c * -4.0), (b * b)))) / a);
	} else {
		tmp = -0.5 * (2.0 * ((b / a) - (c / b)));
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.0
Target21.1
Herbie10.0
\[\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 < -8.18409749626932303e-116

    1. Initial program 52.1

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

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

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

    if -8.18409749626932303e-116 < b < 2.1122499614837679e119

    1. Initial program 11.5

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

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

    if 2.1122499614837679e119 < b

    1. Initial program 50.4

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

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

      \[\leadsto -0.5 \cdot \color{blue}{\left(2 \cdot \frac{b}{a} - 2 \cdot \frac{c}{b}\right)} \]
    4. Simplified2.6

      \[\leadsto -0.5 \cdot \color{blue}{\left(2 \cdot \left(\frac{b}{a} - \frac{c}{b}\right)\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification10.0

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

Reproduce

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