Average Error: 34.3 → 18.6
Time: 10.6s
Precision: binary64
\[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
\[\begin{array}{l} \mathbf{if}\;b \leq -1.329384622164809 \cdot 10^{+154}:\\ \;\;\;\;\frac{\sqrt{0.5} - b}{a \cdot 2}\\ \mathbf{elif}\;b \leq -2.0268161128108735 \cdot 10^{-255}:\\ \;\;\;\;\frac{1}{a} \cdot \frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c}{\frac{b + \sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c}}{-4}}}{2}\\ \end{array}\]
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\begin{array}{l}
\mathbf{if}\;b \leq -1.329384622164809 \cdot 10^{+154}:\\
\;\;\;\;\frac{\sqrt{0.5} - b}{a \cdot 2}\\

\mathbf{elif}\;b \leq -2.0268161128108735 \cdot 10^{-255}:\\
\;\;\;\;\frac{1}{a} \cdot \frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{2}\\

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

\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 -1.329384622164809e+154)
   (/ (- (sqrt 0.5) b) (* a 2.0))
   (if (<= b -2.0268161128108735e-255)
     (* (/ 1.0 a) (/ (- (sqrt (- (* b b) (* (* a 4.0) c))) b) 2.0))
     (/ (/ c (/ (+ b (sqrt (- (* b b) (* (* a 4.0) c)))) -4.0)) 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 <= -1.329384622164809e+154) {
		tmp = (sqrt(0.5) - b) / (a * 2.0);
	} else if (b <= -2.0268161128108735e-255) {
		tmp = (1.0 / a) * ((sqrt((b * b) - ((a * 4.0) * c)) - b) / 2.0);
	} else {
		tmp = (c / ((b + sqrt((b * b) - ((a * 4.0) * c))) / -4.0)) / 2.0;
	}
	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
Target21.2
Herbie18.6
\[\begin{array}{l} \mathbf{if}\;b < 0:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{a \cdot \frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}}\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if b < -1.329384622164809e154

    1. Initial program 64.0

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

      \[\leadsto \color{blue}{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{a \cdot 2}}\]
    3. Taylor expanded around 0 52.2

      \[\leadsto \frac{\sqrt{\color{blue}{0}} - b}{a \cdot 2}\]
    4. Simplified52.2

      \[\leadsto \frac{\sqrt{\color{blue}{0.5}} - b}{a \cdot 2}\]

    if -1.329384622164809e154 < b < -2.026816112810873e-255

    1. Initial program 8.2

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

      \[\leadsto \color{blue}{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{a \cdot 2}}\]
    3. Using strategy rm
    4. Applied *-un-lft-identity_binary648.2

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

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

    if -2.026816112810873e-255 < b

    1. Initial program 42.2

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

      \[\leadsto \color{blue}{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{a \cdot 2}}\]
    3. Using strategy rm
    4. Applied *-un-lft-identity_binary6442.2

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

      \[\leadsto \color{blue}{\frac{1}{a} \cdot \frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{2}}\]
    6. Using strategy rm
    7. Applied flip--_binary6442.2

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -1.329384622164809 \cdot 10^{+154}:\\ \;\;\;\;\frac{\sqrt{0.5} - b}{a \cdot 2}\\ \mathbf{elif}\;b \leq -2.0268161128108735 \cdot 10^{-255}:\\ \;\;\;\;\frac{1}{a} \cdot \frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c}{\frac{b + \sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c}}{-4}}}{2}\\ \end{array}\]

Reproduce

herbie shell --seed 2020268 
(FPCore (a b c)
  :name "The quadratic formula (r1)"
  :precision binary64

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

  (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))