Average Error: 33.6 → 9.4
Time: 1.4m
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 -4.1796980969696804 \cdot 10^{+30}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \le -7.946489207896435 \cdot 10^{-107}:\\ \;\;\;\;\frac{4 \cdot \left(c \cdot a\right)}{\left(a \cdot 2\right) \cdot \left(\sqrt{(\left(c \cdot a\right) \cdot -4 + \left(b \cdot b\right))_*} + \left(-b\right)\right)}\\ \mathbf{elif}\;b \le 5.2381578698044655 \cdot 10^{+62}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{(\left(c \cdot a\right) \cdot -4 + \left(b \cdot b\right))_*}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original33.6
Target20.8
Herbie9.4
\[\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 < -4.1796980969696804e+30

    1. Initial program 56.6

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

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

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

    if -4.1796980969696804e+30 < b < -7.946489207896435e-107

    1. Initial program 37.8

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

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

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

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

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

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

      \[\leadsto \frac{\color{blue}{\left(-b\right) - \sqrt{(\left(a \cdot c\right) \cdot -4 + \left(b \cdot b\right))_*}}}{2 \cdot a}\]
    11. Using strategy rm
    12. Applied flip--37.9

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

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

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

    if -7.946489207896435e-107 < b < 5.2381578698044655e+62

    1. Initial program 12.3

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

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

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

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

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

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

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

    if 5.2381578698044655e+62 < b

    1. Initial program 37.6

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -4.1796980969696804 \cdot 10^{+30}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \le -7.946489207896435 \cdot 10^{-107}:\\ \;\;\;\;\frac{4 \cdot \left(c \cdot a\right)}{\left(a \cdot 2\right) \cdot \left(\sqrt{(\left(c \cdot a\right) \cdot -4 + \left(b \cdot b\right))_*} + \left(-b\right)\right)}\\ \mathbf{elif}\;b \le 5.2381578698044655 \cdot 10^{+62}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{(\left(c \cdot a\right) \cdot -4 + \left(b \cdot b\right))_*}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \end{array}\]

Runtime

Time bar (total: 1.4m)Debug logProfile

herbie shell --seed 2018273 +o rules:numerics
(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)))