Average Error: 33.7 → 6.5
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 -3.2218651897993396 \cdot 10^{+133}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 1.1570789262807629 \cdot 10^{-295}:\\ \;\;\;\;\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}}{a \cdot 2} - \frac{b}{a \cdot 2}\\ \mathbf{elif}\;b \leq 4.1783942655877156 \cdot 10^{+124}:\\ \;\;\;\;\frac{c \cdot -2}{b + \sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}}\\ \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 -3.2218651897993396 \cdot 10^{+133}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\

\mathbf{elif}\;b \leq 1.1570789262807629 \cdot 10^{-295}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}}{a \cdot 2} - \frac{b}{a \cdot 2}\\

\mathbf{elif}\;b \leq 4.1783942655877156 \cdot 10^{+124}:\\
\;\;\;\;\frac{c \cdot -2}{b + \sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}}\\

\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 -3.2218651897993396e+133)
   (- (/ c b) (/ b a))
   (if (<= b 1.1570789262807629e-295)
     (- (/ (sqrt (- (* b b) (* c (* a 4.0)))) (* a 2.0)) (/ b (* a 2.0)))
     (if (<= b 4.1783942655877156e+124)
       (/ (* c -2.0) (+ b (sqrt (- (* b b) (* c (* a 4.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 <= -3.2218651897993396e+133) {
		tmp = (c / b) - (b / a);
	} else if (b <= 1.1570789262807629e-295) {
		tmp = (sqrt((b * b) - (c * (a * 4.0))) / (a * 2.0)) - (b / (a * 2.0));
	} else if (b <= 4.1783942655877156e+124) {
		tmp = (c * -2.0) / (b + sqrt((b * b) - (c * (a * 4.0))));
	} else {
		tmp = -(c / b);
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original33.7
Target20.8
Herbie6.5
\[\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 4 regimes
  2. if b < -3.2218651897993396e133

    1. Initial program 55.8

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

      \[\leadsto \color{blue}{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{a \cdot 2}}\]
    3. Taylor expanded around -inf 2.9

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

    if -3.2218651897993396e133 < b < 1.15707892628076286e-295

    1. Initial program 8.5

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

      \[\leadsto \color{blue}{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{a \cdot 2}}\]
    3. Using strategy rm
    4. Applied div-sub_binary64_14478.5

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

    if 1.15707892628076286e-295 < b < 4.1783942655877156e124

    1. Initial program 33.9

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

      \[\leadsto \color{blue}{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{a \cdot 2}}\]
    3. Using strategy rm
    4. Applied flip--_binary64_141734.0

      \[\leadsto \frac{\color{blue}{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b \cdot b}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} + b}}}{a \cdot 2}\]
    5. Simplified16.3

      \[\leadsto \frac{\frac{\color{blue}{a \cdot \left(c \cdot -4\right)}}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} + b}}{a \cdot 2}\]
    6. Simplified16.3

      \[\leadsto \frac{\frac{a \cdot \left(c \cdot -4\right)}{\color{blue}{b + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}}{a \cdot 2}\]
    7. Using strategy rm
    8. Applied div-inv_binary64_143916.4

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

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

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

      \[\leadsto \left(c \cdot -4\right) \cdot \color{blue}{\frac{0.5}{b + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}\]
    12. Using strategy rm
    13. Applied associate-*r/_binary64_13848.2

      \[\leadsto \color{blue}{\frac{\left(c \cdot -4\right) \cdot 0.5}{b + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}\]
    14. Simplified8.2

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

    if 4.1783942655877156e124 < b

    1. Initial program 61.2

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

      \[\leadsto \color{blue}{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{a \cdot 2}}\]
    3. Taylor expanded around inf 2.6

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}}\]
    4. Simplified2.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -3.2218651897993396 \cdot 10^{+133}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 1.1570789262807629 \cdot 10^{-295}:\\ \;\;\;\;\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}}{a \cdot 2} - \frac{b}{a \cdot 2}\\ \mathbf{elif}\;b \leq 4.1783942655877156 \cdot 10^{+124}:\\ \;\;\;\;\frac{c \cdot -2}{b + \sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}}\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \end{array}\]

Reproduce

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