Average Error: 34.2 → 10.3
Time: 1.2m
Precision: 64
Internal Precision: 128
\[\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 -6.614644252084147 \cdot 10^{-35}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \le -1.180471078121508 \cdot 10^{-156}:\\ \;\;\;\;\frac{\left(a \cdot c\right) \cdot 4}{\left(2 \cdot a\right) \cdot \left(\sqrt{b \cdot b - \left(a \cdot c\right) \cdot 4} + \left(-b\right)\right)}\\ \mathbf{elif}\;b \le -4.380258891361565 \cdot 10^{-166}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \le 5.1457556548524475 \cdot 10^{+64}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{\sqrt{a \cdot \left(c \cdot -4\right) + b \cdot b}} \cdot \sqrt{\sqrt{a \cdot \left(c \cdot -4\right) + b \cdot b}}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.2
Target21.5
Herbie10.3
\[\begin{array}{l} \mathbf{if}\;b \lt 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 < -6.614644252084147e-35 or -1.180471078121508e-156 < b < -4.380258891361565e-166

    1. Initial program 53.3

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

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

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

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

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

    if -6.614644252084147e-35 < b < -1.180471078121508e-156

    1. Initial program 30.2

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

      \[\leadsto \frac{\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)}}}}{2 \cdot a}\]
    4. Applied associate-/l/33.8

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

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

    if -4.380258891361565e-166 < b < 5.1457556548524475e+64

    1. Initial program 11.9

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

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

      \[\leadsto \frac{\left(-b\right) - \sqrt{\color{blue}{b \cdot b + \left(c \cdot -4\right) \cdot a}}}{2 \cdot a}\]
    4. Using strategy rm
    5. Applied add-sqr-sqrt11.9

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

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

    if 5.1457556548524475e+64 < b

    1. Initial program 38.5

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -6.614644252084147 \cdot 10^{-35}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \le -1.180471078121508 \cdot 10^{-156}:\\ \;\;\;\;\frac{\left(a \cdot c\right) \cdot 4}{\left(2 \cdot a\right) \cdot \left(\sqrt{b \cdot b - \left(a \cdot c\right) \cdot 4} + \left(-b\right)\right)}\\ \mathbf{elif}\;b \le -4.380258891361565 \cdot 10^{-166}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \le 5.1457556548524475 \cdot 10^{+64}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{\sqrt{a \cdot \left(c \cdot -4\right) + b \cdot b}} \cdot \sqrt{\sqrt{a \cdot \left(c \cdot -4\right) + b \cdot b}}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \end{array}\]

Reproduce

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

  :herbie-target
  (if (< b 0) (/ c (* a (/ (+ (- b) (sqrt (- (* b b) (* 4 (* a c))))) (* 2 a)))) (/ (- (- b) (sqrt (- (* b b) (* 4 (* a c))))) (* 2 a)))

  (/ (- (- b) (sqrt (- (* b b) (* 4 (* a c))))) (* 2 a)))