Average Error: 34.3 → 6.6
Time: 6.9s
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 -3.0241087845909653 \cdot 10^{+99}:\\ \;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\ \mathbf{elif}\;b \leq -1.2708710508270122 \cdot 10^{-228}:\\ \;\;\;\;-0.5 \cdot \frac{c}{\frac{b - \sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}{4}}\\ \mathbf{elif}\;b \leq 2.763946614645106 \cdot 10^{+115}:\\ \;\;\;\;-0.5 \cdot \frac{b + \sqrt{b \cdot b + a \cdot \left(c \cdot -4\right)}}{a}\\ \mathbf{else}:\\ \;\;\;\;-0.5 \cdot \frac{b + b}{a}\\ \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 -3.0241087845909653 \cdot 10^{+99}:\\
\;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\

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

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

\mathbf{else}:\\
\;\;\;\;-0.5 \cdot \frac{b + b}{a}\\

\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.0241087845909653e+99)
   (* -0.5 (* 2.0 (/ c b)))
   (if (<= b -1.2708710508270122e-228)
     (* -0.5 (/ c (/ (- b (sqrt (- (* b b) (* 4.0 (* c a))))) 4.0)))
     (if (<= b 2.763946614645106e+115)
       (* -0.5 (/ (+ b (sqrt (+ (* b b) (* a (* c -4.0))))) a))
       (* -0.5 (/ (+ b b) a))))))
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.0241087845909653e+99) {
		tmp = -0.5 * (2.0 * (c / b));
	} else if (b <= -1.2708710508270122e-228) {
		tmp = -0.5 * (c / ((b - sqrt((b * b) - (4.0 * (c * a)))) / 4.0));
	} else if (b <= 2.763946614645106e+115) {
		tmp = -0.5 * ((b + sqrt((b * b) + (a * (c * -4.0)))) / a);
	} else {
		tmp = -0.5 * ((b + b) / a);
	}
	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.3
Target20.9
Herbie6.6
\[\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 < -3.024108784590965e99

    1. Initial program 59.7

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

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

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

    if -3.024108784590965e99 < b < -1.27087105082701218e-228

    1. Initial program 35.7

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

      \[\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-+_binary6435.7

      \[\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. Simplified16.3

      \[\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}\]
    6. Using strategy rm
    7. Applied clear-num_binary6416.4

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

      \[\leadsto -0.5 \cdot \frac{1}{\color{blue}{\frac{1}{c} \cdot \frac{b - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{4}}}\]
    9. Using strategy rm
    10. Applied associate-/r*_binary647.3

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

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

    if -1.27087105082701218e-228 < b < 2.7639466146451059e115

    1. Initial program 9.8

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

      \[\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 sub-neg_binary649.8

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

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

    if 2.7639466146451059e115 < b

    1. Initial program 51.9

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

      \[\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 3.5

      \[\leadsto -0.5 \cdot \frac{b + \color{blue}{b}}{a}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification6.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -3.0241087845909653 \cdot 10^{+99}:\\ \;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\ \mathbf{elif}\;b \leq -1.2708710508270122 \cdot 10^{-228}:\\ \;\;\;\;-0.5 \cdot \frac{c}{\frac{b - \sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}{4}}\\ \mathbf{elif}\;b \leq 2.763946614645106 \cdot 10^{+115}:\\ \;\;\;\;-0.5 \cdot \frac{b + \sqrt{b \cdot b + a \cdot \left(c \cdot -4\right)}}{a}\\ \mathbf{else}:\\ \;\;\;\;-0.5 \cdot \frac{b + b}{a}\\ \end{array}\]

Alternatives

Reproduce

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