Average Error: 31.2 → 8.0
Time: 10.4s
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 -4.068668780742819 \cdot 10^{+85}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := 4 \cdot \left(c \cdot a\right)\\ t_1 := \sqrt{b \cdot b - t_0}\\ \mathbf{if}\;b \leq -2.2515171662899013 \cdot 10^{-173}:\\ \;\;\;\;\frac{t_0 \cdot \frac{0.5}{a}}{t_1 - b}\\ \mathbf{elif}\;b \leq 1.6598215864349702 \cdot 10^{+99}:\\ \;\;\;\;\frac{-b}{a \cdot 2} - \frac{t_1}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) - b}{a \cdot 2}\\ \end{array}\\ \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 -4.068668780742819 \cdot 10^{+85}:\\
\;\;\;\;-\frac{c}{b}\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_0 := 4 \cdot \left(c \cdot a\right)\\
t_1 := \sqrt{b \cdot b - t_0}\\
\mathbf{if}\;b \leq -2.2515171662899013 \cdot 10^{-173}:\\
\;\;\;\;\frac{t_0 \cdot \frac{0.5}{a}}{t_1 - b}\\

\mathbf{elif}\;b \leq 1.6598215864349702 \cdot 10^{+99}:\\
\;\;\;\;\frac{-b}{a \cdot 2} - \frac{t_1}{a \cdot 2}\\

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


\end{array}\\


\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.068668780742819e+85)
   (- (/ c b))
   (let* ((t_0 (* 4.0 (* c a))) (t_1 (sqrt (- (* b b) t_0))))
     (if (<= b -2.2515171662899013e-173)
       (/ (* t_0 (/ 0.5 a)) (- t_1 b))
       (if (<= b 1.6598215864349702e+99)
         (- (/ (- b) (* a 2.0)) (/ t_1 (* a 2.0)))
         (/ (- (- b) b) (* a 2.0)))))))
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.068668780742819e+85) {
		tmp = -(c / b);
	} else {
		double t_0 = 4.0 * (c * a);
		double t_1 = sqrt((b * b) - t_0);
		double tmp_1;
		if (b <= -2.2515171662899013e-173) {
			tmp_1 = (t_0 * (0.5 / a)) / (t_1 - b);
		} else if (b <= 1.6598215864349702e+99) {
			tmp_1 = (-b / (a * 2.0)) - (t_1 / (a * 2.0));
		} else {
			tmp_1 = (-b - b) / (a * 2.0);
		}
		tmp = tmp_1;
	}
	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

Original31.2
Target19.1
Herbie8.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 4 regimes
  2. if b < -4.06866878074281868e85

    1. Initial program 58.3

      \[\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.5

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

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

    if -4.06866878074281868e85 < b < -2.2515171662899013e-173

    1. Initial program 38.3

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

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

      \[\leadsto \frac{1}{\frac{2 \cdot a}{\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)}}}}} \]
    4. Applied associate-/r/_binary6438.6

      \[\leadsto \frac{1}{\color{blue}{\frac{2 \cdot a}{\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)}} \cdot \left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right)}} \]
    5. Applied associate-/r*_binary6438.7

      \[\leadsto \color{blue}{\frac{\frac{1}{\frac{2 \cdot a}{\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)}}} \]
    6. Simplified16.2

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

    if -2.2515171662899013e-173 < b < 1.6598215864349702e99

    1. Initial program 10.6

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

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

    if 1.6598215864349702e99 < b

    1. Initial program 29.7

      \[\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.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -4.068668780742819 \cdot 10^{+85}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \leq -2.2515171662899013 \cdot 10^{-173}:\\ \;\;\;\;\frac{\left(4 \cdot \left(c \cdot a\right)\right) \cdot \frac{0.5}{a}}{\sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)} - b}\\ \mathbf{elif}\;b \leq 1.6598215864349702 \cdot 10^{+99}:\\ \;\;\;\;\frac{-b}{a \cdot 2} - \frac{\sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) - b}{a \cdot 2}\\ \end{array} \]

Reproduce

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