Average Error: 34.5 → 10.1
Time: 10.0s
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 -8.283334678121039 \cdot 10^{+98}:\\ \;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\ \mathbf{elif}\;b \leq -3.3740638410628572 \cdot 10^{-50}:\\ \;\;\;\;-0.5 \cdot \frac{\frac{4 \cdot \left(c \cdot a\right)}{b - \sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}}{a}\\ \mathbf{elif}\;b \leq 4.74489523208748 \cdot 10^{+75}:\\ \;\;\;\;-0.5 \cdot \frac{b + \sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}{a}\\ \mathbf{else}:\\ \;\;\;\;-0.5 \cdot \left(2 \cdot \left(\frac{b}{a} - \frac{c}{b}\right)\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 \leq -8.283334678121039 \cdot 10^{+98}:\\
\;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\

\mathbf{elif}\;b \leq -3.3740638410628572 \cdot 10^{-50}:\\
\;\;\;\;-0.5 \cdot \frac{\frac{4 \cdot \left(c \cdot a\right)}{b - \sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}}{a}\\

\mathbf{elif}\;b \leq 4.74489523208748 \cdot 10^{+75}:\\
\;\;\;\;-0.5 \cdot \frac{b + \sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}{a}\\

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

\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 -8.283334678121039e+98)
   (* -0.5 (* 2.0 (/ c b)))
   (if (<= b -3.3740638410628572e-50)
     (*
      -0.5
      (/ (/ (* 4.0 (* c a)) (- b (sqrt (- (* b b) (* 4.0 (* c a)))))) a))
     (if (<= b 4.74489523208748e+75)
       (* -0.5 (/ (+ b (sqrt (- (* b b) (* 4.0 (* c a))))) a))
       (* -0.5 (* 2.0 (- (/ b 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 <= -8.283334678121039e+98) {
		tmp = -0.5 * (2.0 * (c / b));
	} else if (b <= -3.3740638410628572e-50) {
		tmp = -0.5 * (((4.0 * (c * a)) / (b - sqrt((b * b) - (4.0 * (c * a))))) / a);
	} else if (b <= 4.74489523208748e+75) {
		tmp = -0.5 * ((b + sqrt((b * b) - (4.0 * (c * a)))) / a);
	} else {
		tmp = -0.5 * (2.0 * ((b / a) - (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.5
Target20.8
Herbie10.1
\[\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 < -8.28333467812103939e98

    1. Initial program 59.8

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

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

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

    if -8.28333467812103939e98 < b < -3.374063841062857e-50

    1. Initial program 45.0

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

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

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

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

    if -3.374063841062857e-50 < b < 4.74489523208747987e75

    1. Initial program 14.6

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

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

    if 4.74489523208747987e75 < b

    1. Initial program 42.3

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -8.283334678121039 \cdot 10^{+98}:\\ \;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\ \mathbf{elif}\;b \leq -3.3740638410628572 \cdot 10^{-50}:\\ \;\;\;\;-0.5 \cdot \frac{\frac{4 \cdot \left(c \cdot a\right)}{b - \sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}}{a}\\ \mathbf{elif}\;b \leq 4.74489523208748 \cdot 10^{+75}:\\ \;\;\;\;-0.5 \cdot \frac{b + \sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}{a}\\ \mathbf{else}:\\ \;\;\;\;-0.5 \cdot \left(2 \cdot \left(\frac{b}{a} - \frac{c}{b}\right)\right)\\ \end{array}\]

Reproduce

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