Average Error: 34.1 → 8.8
Time: 17.2s
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 -4.4035815120185316 \cdot 10^{+145}:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{elif}\;b \leq 1.3458493856293548 \cdot 10^{-118}:\\ \;\;\;\;\frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{a \cdot 2}\\ \mathbf{elif}\;b \leq 2571.2555714359514:\\ \;\;\;\;\frac{\frac{a \cdot \left(c \cdot -4\right)}{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}}{a \cdot 2}\\ \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 -4.4035815120185316 \cdot 10^{+145}:\\
\;\;\;\;\frac{-b}{a}\\

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

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

\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 -4.4035815120185316e+145)
   (/ (- b) a)
   (if (<= b 1.3458493856293548e-118)
     (/ (- (sqrt (- (* b b) (* (* a 4.0) c))) b) (* a 2.0))
     (if (<= b 2571.2555714359514)
       (/
        (/ (* a (* c -4.0)) (+ b (sqrt (- (* b b) (* 4.0 (* a c))))))
        (* a 2.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 <= -4.4035815120185316e+145) {
		tmp = -b / a;
	} else if (b <= 1.3458493856293548e-118) {
		tmp = (sqrt((b * b) - ((a * 4.0) * c)) - b) / (a * 2.0);
	} else if (b <= 2571.2555714359514) {
		tmp = ((a * (c * -4.0)) / (b + sqrt((b * b) - (4.0 * (a * c))))) / (a * 2.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

Original34.1
Target20.9
Herbie8.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 4 regimes
  2. if b < -4.4035815120185316e145

    1. Initial program 60.6

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

      \[\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}{-1 \cdot \frac{b}{a}}\]
    4. Simplified2.9

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

    if -4.4035815120185316e145 < b < 1.3458493856293548e-118

    1. Initial program 11.5

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

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

    if 1.3458493856293548e-118 < b < 2571.2555714359514

    1. Initial program 35.3

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

      \[\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 add-cube-cbrt_binary64_250036.1

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

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

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

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

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

    if 2571.2555714359514 < b

    1. Initial program 55.9

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -4.4035815120185316 \cdot 10^{+145}:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{elif}\;b \leq 1.3458493856293548 \cdot 10^{-118}:\\ \;\;\;\;\frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{a \cdot 2}\\ \mathbf{elif}\;b \leq 2571.2555714359514:\\ \;\;\;\;\frac{\frac{a \cdot \left(c \cdot -4\right)}{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \end{array}\]

Reproduce

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