Average Error: 34.9 → 7.2
Time: 4.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 -3.8933131748243174 \cdot 10^{+118}:\\ \;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\ \mathbf{elif}\;b \leq 6.117566134779201 \cdot 10^{-229}:\\ \;\;\;\;-0.5 \cdot \left(4 \cdot \frac{c}{b - \sqrt{b \cdot b + a \cdot \left(c \cdot -4\right)}}\right)\\ \mathbf{elif}\;b \leq 2.6178552751939085 \cdot 10^{+28}:\\ \;\;\;\;-0.5 \cdot \frac{b + \sqrt{b \cdot b + a \cdot \left(c \cdot -4\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 -3.8933131748243174 \cdot 10^{+118}:\\
\;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\

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

\mathbf{elif}\;b \leq 2.6178552751939085 \cdot 10^{+28}:\\
\;\;\;\;-0.5 \cdot \frac{b + \sqrt{b \cdot b + a \cdot \left(c \cdot -4\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 -3.8933131748243174e+118)
   (* -0.5 (* 2.0 (/ c b)))
   (if (<= b 6.117566134779201e-229)
     (* -0.5 (* 4.0 (/ c (- b (sqrt (+ (* b b) (* a (* c -4.0))))))))
     (if (<= b 2.6178552751939085e+28)
       (* -0.5 (/ (+ b (sqrt (+ (* b b) (* a (* c -4.0))))) 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 <= -3.8933131748243174e+118) {
		tmp = -0.5 * (2.0 * (c / b));
	} else if (b <= 6.117566134779201e-229) {
		tmp = -0.5 * (4.0 * (c / (b - sqrt((b * b) + (a * (c * -4.0))))));
	} else if (b <= 2.6178552751939085e+28) {
		tmp = -0.5 * ((b + sqrt((b * b) + (a * (c * -4.0)))) / 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.9
Target21.7
Herbie7.2
\[\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.8933131748243174e118

    1. Initial program 60.8

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Simplified60.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.3

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

    if -3.8933131748243174e118 < b < 6.117566134779201e-229

    1. Initial program 31.0

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Simplified31.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-+_binary6431.1

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

      \[\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 *-un-lft-identity_binary6416.0

      \[\leadsto -0.5 \cdot \frac{\frac{4 \cdot \left(a \cdot c\right)}{b - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}}{\color{blue}{1 \cdot a}}\]
    8. Applied *-un-lft-identity_binary6416.0

      \[\leadsto -0.5 \cdot \frac{\frac{4 \cdot \left(a \cdot c\right)}{\color{blue}{1 \cdot \left(b - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right)}}}{1 \cdot a}\]
    9. Applied times-frac_binary6416.1

      \[\leadsto -0.5 \cdot \frac{\color{blue}{\frac{4}{1} \cdot \frac{a \cdot c}{b - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}}}{1 \cdot a}\]
    10. Applied times-frac_binary6416.1

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

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

      \[\leadsto -0.5 \cdot \left(4 \cdot \color{blue}{\left(1 \cdot \frac{c}{b - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}\right)}\right)\]
    13. Using strategy rm
    14. Applied sub-neg_binary649.4

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

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

    if 6.117566134779201e-229 < b < 2.61785527519390851e28

    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.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 sub-neg_binary649.7

      \[\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.61785527519390851e28 < b

    1. Initial program 37.2

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Simplified37.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.9

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

      \[\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 simplification7.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -3.8933131748243174 \cdot 10^{+118}:\\ \;\;\;\;-0.5 \cdot \left(2 \cdot \frac{c}{b}\right)\\ \mathbf{elif}\;b \leq 6.117566134779201 \cdot 10^{-229}:\\ \;\;\;\;-0.5 \cdot \left(4 \cdot \frac{c}{b - \sqrt{b \cdot b + a \cdot \left(c \cdot -4\right)}}\right)\\ \mathbf{elif}\;b \leq 2.6178552751939085 \cdot 10^{+28}:\\ \;\;\;\;-0.5 \cdot \frac{b + \sqrt{b \cdot b + a \cdot \left(c \cdot -4\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 2020224 
(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)))