Average Error: 34.1 → 7.0
Time: 4.5m
Precision: 64
Internal Precision: 128
\[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
\[\begin{array}{l} \mathbf{if}\;b \le -1.2275576669103459 \cdot 10^{+128}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \le -5.616574398330274 \cdot 10^{-296}:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b + a \cdot \left(-4 \cdot c\right)}}{a \cdot 2}\\ \mathbf{elif}\;b \le 5.1457556548524475 \cdot 10^{+64}:\\ \;\;\;\;\frac{c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}} \cdot 2\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.1
Target21.4
Herbie7.0
\[\begin{array}{l} \mathbf{if}\;b \lt 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 4 regimes
  2. if b < -1.2275576669103459e+128

    1. Initial program 51.2

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

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

      \[\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 3.2

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

    if -1.2275576669103459e+128 < b < -5.616574398330274e-296

    1. Initial program 9.2

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

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

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

    if -5.616574398330274e-296 < b < 5.1457556548524475e+64

    1. Initial program 30.5

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

      \[\leadsto \frac{\color{blue}{\frac{\left(-b\right) \cdot \left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}}{2 \cdot a}\]
    4. Applied associate-/l/35.4

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

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

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

      \[\leadsto \frac{c \cdot \left(4 \cdot a\right)}{\left(2 \cdot a\right) \cdot \left(\color{blue}{1 \cdot \left(-b\right)} - 1 \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}\]
    9. Applied distribute-lft-out--22.4

      \[\leadsto \frac{c \cdot \left(4 \cdot a\right)}{\left(2 \cdot a\right) \cdot \color{blue}{\left(1 \cdot \left(\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)\right)}}\]
    10. Applied associate-*r*22.4

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

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

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

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

    if 5.1457556548524475e+64 < b

    1. Initial program 57.4

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -1.2275576669103459 \cdot 10^{+128}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \le -5.616574398330274 \cdot 10^{-296}:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b + a \cdot \left(-4 \cdot c\right)}}{a \cdot 2}\\ \mathbf{elif}\;b \le 5.1457556548524475 \cdot 10^{+64}:\\ \;\;\;\;\frac{c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}} \cdot 2\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \end{array}\]

Reproduce

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

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

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