Average Error: 33.6 → 6.7
Time: 6.7s
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.215444035007023 \cdot 10^{+149}:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{elif}\;b \leq -6.380181084755399 \cdot 10^{-238}:\\ \;\;\;\;\frac{\sqrt{b \cdot b + a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\ \mathbf{elif}\;b \leq 1.0477879590735767 \cdot 10^{+99}:\\ \;\;\;\;\frac{c \cdot -2}{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \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.215444035007023 \cdot 10^{+149}:\\
\;\;\;\;\frac{-b}{a}\\

\mathbf{elif}\;b \leq -6.380181084755399 \cdot 10^{-238}:\\
\;\;\;\;\frac{\sqrt{b \cdot b + a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\

\mathbf{elif}\;b \leq 1.0477879590735767 \cdot 10^{+99}:\\
\;\;\;\;\frac{c \cdot -2}{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}\\

\mathbf{else}:\\
\;\;\;\;-\frac{c}{b}\\

\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.215444035007023e+149)
   (/ (- b) a)
   (if (<= b -6.380181084755399e-238)
     (/ (- (sqrt (+ (* b b) (* a (* c -4.0)))) b) (* a 2.0))
     (if (<= b 1.0477879590735767e+99)
       (/ (* c -2.0) (+ b (sqrt (- (* b b) (* 4.0 (* a c))))))
       (- (/ 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.215444035007023e+149) {
		tmp = -b / a;
	} else if (b <= -6.380181084755399e-238) {
		tmp = (sqrt((b * b) + (a * (c * -4.0))) - b) / (a * 2.0);
	} else if (b <= 1.0477879590735767e+99) {
		tmp = (c * -2.0) / (b + sqrt((b * b) - (4.0 * (a * c))));
	} else {
		tmp = -(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

Original33.6
Target20.7
Herbie6.7
\[\begin{array}{l} \mathbf{if}\;b < 0:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{a \cdot \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.21544403500702319e149

    1. Initial program 61.9

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{b}{a}}\]
    4. Simplified3.1

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

    if -3.21544403500702319e149 < b < -6.38018108475539851e-238

    1. Initial program 7.2

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

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

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

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

    if -6.38018108475539851e-238 < b < 1.0477879590735767e99

    1. Initial program 30.8

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

      \[\leadsto \color{blue}{\frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}{a \cdot 2}}\]
    3. Using strategy rm
    4. Applied sub-neg_binary6430.8

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

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

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

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

      \[\leadsto \frac{\frac{a \cdot \left(c \cdot -4\right) + 0}{\color{blue}{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}}}{a \cdot 2}\]
    10. Using strategy rm
    11. Applied div-inv_binary6416.6

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

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

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

      \[\leadsto \left(1 \cdot \left(c \cdot -4\right)\right) \cdot \color{blue}{\frac{0.5}{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}}\]
    15. Using strategy rm
    16. Applied associate-*r/_binary6410.1

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

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

    if 1.0477879590735767e99 < b

    1. Initial program 59.2

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

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

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

      \[\leadsto \color{blue}{-\frac{c}{b}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification6.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -3.215444035007023 \cdot 10^{+149}:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{elif}\;b \leq -6.380181084755399 \cdot 10^{-238}:\\ \;\;\;\;\frac{\sqrt{b \cdot b + a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\ \mathbf{elif}\;b \leq 1.0477879590735767 \cdot 10^{+99}:\\ \;\;\;\;\frac{c \cdot -2}{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \end{array}\]

Alternatives

Reproduce

herbie shell --seed 2021118 
(FPCore (a b c)
  :name "quadp (p42, positive)"
  :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)))