Average Error: 34.0 → 6.5
Time: 4.3s
Precision: 64
\[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
\[\begin{array}{l} \mathbf{if}\;b \le -1.3298367139250566 \cdot 10^{153}:\\ \;\;\;\;-1 \cdot \frac{c}{b}\\ \mathbf{elif}\;b \le 5.9493827268354929 \cdot 10^{-251}:\\ \;\;\;\;\left(4 \cdot c\right) \cdot \frac{\frac{1}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}}{2}\\ \mathbf{elif}\;b \le 1.70350465332629402 \cdot 10^{111}:\\ \;\;\;\;\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{1}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \left(\frac{c}{b} - \frac{b}{a}\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 \le -1.3298367139250566 \cdot 10^{153}:\\
\;\;\;\;-1 \cdot \frac{c}{b}\\

\mathbf{elif}\;b \le 5.9493827268354929 \cdot 10^{-251}:\\
\;\;\;\;\left(4 \cdot c\right) \cdot \frac{\frac{1}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}}{2}\\

\mathbf{elif}\;b \le 1.70350465332629402 \cdot 10^{111}:\\
\;\;\;\;\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{1}{2 \cdot a}\\

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

\end{array}
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 VAR;
	if ((b <= -1.3298367139250566e+153)) {
		VAR = (-1.0 * (c / b));
	} else {
		double VAR_1;
		if ((b <= 5.949382726835493e-251)) {
			VAR_1 = ((4.0 * c) * ((1.0 / (sqrt(((b * b) - (4.0 * (a * c)))) - b)) / 2.0));
		} else {
			double VAR_2;
			if ((b <= 1.703504653326294e+111)) {
				VAR_2 = ((-b - sqrt(((b * b) - (4.0 * (a * c))))) * (1.0 / (2.0 * a)));
			} else {
				VAR_2 = (1.0 * ((c / b) - (b / a)));
			}
			VAR_1 = VAR_2;
		}
		VAR = VAR_1;
	}
	return VAR;
}

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.0
Target21.0
Herbie6.5
\[\begin{array}{l} \mathbf{if}\;b \lt 0.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 4 regimes
  2. if b < -1.3298367139250566e+153

    1. Initial program 63.9

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Taylor expanded around -inf 1.7

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

    if -1.3298367139250566e+153 < b < 5.949382726835493e-251

    1. Initial program 32.8

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

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

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

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, 4 \cdot c, 0\right)}{\color{blue}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}}}{2 \cdot a}\]
    6. Using strategy rm
    7. Applied *-commutative15.8

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, 4 \cdot c, 0\right)}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}}{\color{blue}{a \cdot 2}}\]
    8. Applied div-inv15.9

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, 4 \cdot c, 0\right)}{a} \cdot \frac{\frac{1}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}}{2}}\]
    10. Taylor expanded around 0 8.5

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

    if 5.949382726835493e-251 < b < 1.703504653326294e+111

    1. Initial program 8.4

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied div-inv8.6

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

    if 1.703504653326294e+111 < b

    1. Initial program 49.5

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Taylor expanded around inf 3.0

      \[\leadsto \color{blue}{1 \cdot \frac{c}{b} - 1 \cdot \frac{b}{a}}\]
    3. Simplified3.0

      \[\leadsto \color{blue}{1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification6.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -1.3298367139250566 \cdot 10^{153}:\\ \;\;\;\;-1 \cdot \frac{c}{b}\\ \mathbf{elif}\;b \le 5.9493827268354929 \cdot 10^{-251}:\\ \;\;\;\;\left(4 \cdot c\right) \cdot \frac{\frac{1}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}}{2}\\ \mathbf{elif}\;b \le 1.70350465332629402 \cdot 10^{111}:\\ \;\;\;\;\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{1}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2020071 +o rules:numerics
(FPCore (a b c)
  :name "The quadratic formula (r2)"
  :precision binary64

  :herbie-target
  (if (< b 0.0) (/ c (* a (/ (+ (- b) (sqrt (- (* b b) (* 4 (* a c))))) (* 2 a)))) (/ (- (- b) (sqrt (- (* b b) (* 4 (* a c))))) (* 2 a)))

  (/ (- (- b) (sqrt (- (* b b) (* 4 (* a c))))) (* 2 a)))