Average Error: 34.1 → 10.0
Time: 9.7s
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.9695952621839593 \cdot 10^{+146}:\\ \;\;\;\;\frac{\left(-b\right) - b}{2 \cdot a}\\ \mathbf{elif}\;b \leq 1.4146895075350554 \cdot 10^{-29}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)} - b}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \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.9695952621839593 \cdot 10^{+146}:\\
\;\;\;\;\frac{\left(-b\right) - b}{2 \cdot a}\\

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

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.1
Target20.8
Herbie10.0
\[\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 < -1.96959526218395925e146

    1. Initial program 60.5

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

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

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

    if -1.96959526218395925e146 < b < 1.4146895075350554e-29

    1. Initial program 13.8

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
    2. Applied fma-neg_binary6413.8

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

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

    if 1.4146895075350554e-29 < b

    1. Initial program 55.1

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

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

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

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

Reproduce

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