Average Error: 34.8 → 7.0
Time: 8.3s
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 \le -5.3805398059230384 \cdot 10^{75}:\\ \;\;\;\;-1 \cdot \frac{c}{b}\\ \mathbf{elif}\;b \le 2.8000549201992103 \cdot 10^{-266}:\\ \;\;\;\;\frac{2 \cdot c}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}\\ \mathbf{elif}\;b \le 6.2208854102381593 \cdot 10^{124}:\\ \;\;\;\;\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{1}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.8
Target21.6
Herbie7.0
\[\begin{array}{l} \mathbf{if}\;b \lt 0.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 < -5.3805398059230384e75

    1. Initial program 58.4

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Taylor expanded around -inf 2.6

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

    if -5.3805398059230384e75 < b < 2.8000549201992103e-266

    1. Initial program 30.1

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied div-inv30.2

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

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

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

      \[\leadsto \frac{0 + 1 \cdot \left(4 \cdot \left(a \cdot c\right)\right)}{\color{blue}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}} \cdot \frac{1}{2 \cdot a}\]
    8. Using strategy rm
    9. Applied associate-*l/16.5

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

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

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

    if 2.8000549201992103e-266 < b < 6.2208854102381593e124

    1. Initial program 9.7

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied div-inv9.8

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

    if 6.2208854102381593e124 < b

    1. Initial program 53.1

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Taylor expanded around inf 3.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -5.3805398059230384 \cdot 10^{75}:\\ \;\;\;\;-1 \cdot \frac{c}{b}\\ \mathbf{elif}\;b \le 2.8000549201992103 \cdot 10^{-266}:\\ \;\;\;\;\frac{2 \cdot c}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}\\ \mathbf{elif}\;b \le 6.2208854102381593 \cdot 10^{124}:\\ \;\;\;\;\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{1}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)\\ \end{array}\]

Reproduce

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

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

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