Average Error: 25.3 → 13.0
Time: 1.5m
Precision: 64
Internal Precision: 576
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -2.1203256376020936 \cdot 10^{+104}:\\ \;\;\;\;\frac{-b}{\sqrt{d^2 + c^2}^*}\\ \mathbf{if}\;d \le -1.5311629338677103 \cdot 10^{-192}:\\ \;\;\;\;\frac{1}{\sqrt{d^2 + c^2}^*} \cdot \left(\frac{1}{\sqrt{\sqrt{d^2 + c^2}^*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{\sqrt{d^2 + c^2}^*}}\right)\\ \mathbf{if}\;d \le 2.2280832042887506 \cdot 10^{-81}:\\ \;\;\;\;(\left(\frac{\frac{b \cdot d}{\sqrt{d^2 + c^2}^*}}{\left|\sqrt[3]{\sqrt{d^2 + c^2}^*}\right|}\right) \cdot \left(\sqrt[3]{\frac{1}{c \cdot c}}\right) + \left(\frac{a \cdot \frac{\sqrt[3]{c}}{\sqrt{d^2 + c^2}^*}}{\left|\sqrt[3]{\sqrt{d^2 + c^2}^*}\right|}\right))_*\\ \mathbf{if}\;d \le 5.302451626529533 \cdot 10^{+165}:\\ \;\;\;\;\frac{1}{\sqrt{d^2 + c^2}^*} \cdot \left(\frac{1}{\sqrt{\sqrt{d^2 + c^2}^*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{\sqrt{d^2 + c^2}^*}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{d^2 + c^2}^*}\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.3
Target0.5
Herbie13.0
\[\begin{array}{l} \mathbf{if}\;\left|d\right| \lt \left|c\right|:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array}\]

Derivation

  1. Split input into 4 regimes
  2. if d < -2.1203256376020936e+104

    1. Initial program 39.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Applied simplify39.4

      \[\leadsto \color{blue}{\frac{(b \cdot d + \left(c \cdot a\right))_*}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt39.4

      \[\leadsto \frac{(b \cdot d + \left(c \cdot a\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied *-un-lft-identity39.4

      \[\leadsto \frac{\color{blue}{1 \cdot (b \cdot d + \left(c \cdot a\right))_*}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    6. Applied times-frac39.4

      \[\leadsto \color{blue}{\frac{1}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    7. Applied simplify39.4

      \[\leadsto \color{blue}{\frac{1}{\sqrt{d^2 + c^2}^*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    8. Applied simplify25.5

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \color{blue}{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{d^2 + c^2}^*}}\]
    9. Taylor expanded around -inf 17.1

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \color{blue}{\left(-1 \cdot b\right)}\]
    10. Applied simplify17.0

      \[\leadsto \color{blue}{\frac{-b}{\sqrt{d^2 + c^2}^*}}\]

    if -2.1203256376020936e+104 < d < -1.5311629338677103e-192 or 2.2280832042887506e-81 < d < 5.302451626529533e+165

    1. Initial program 17.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Applied simplify17.3

      \[\leadsto \color{blue}{\frac{(b \cdot d + \left(c \cdot a\right))_*}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt17.3

      \[\leadsto \frac{(b \cdot d + \left(c \cdot a\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied *-un-lft-identity17.3

      \[\leadsto \frac{\color{blue}{1 \cdot (b \cdot d + \left(c \cdot a\right))_*}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    6. Applied times-frac17.3

      \[\leadsto \color{blue}{\frac{1}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    7. Applied simplify17.3

      \[\leadsto \color{blue}{\frac{1}{\sqrt{d^2 + c^2}^*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    8. Applied simplify12.1

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \color{blue}{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{d^2 + c^2}^*}}\]
    9. Using strategy rm
    10. Applied add-sqr-sqrt12.3

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\color{blue}{\sqrt{\sqrt{d^2 + c^2}^*} \cdot \sqrt{\sqrt{d^2 + c^2}^*}}}\]
    11. Applied *-un-lft-identity12.3

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \frac{\color{blue}{1 \cdot (b \cdot d + \left(c \cdot a\right))_*}}{\sqrt{\sqrt{d^2 + c^2}^*} \cdot \sqrt{\sqrt{d^2 + c^2}^*}}\]
    12. Applied times-frac12.3

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

    if -1.5311629338677103e-192 < d < 2.2280832042887506e-81

    1. Initial program 20.7

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Applied simplify20.7

      \[\leadsto \color{blue}{\frac{(b \cdot d + \left(c \cdot a\right))_*}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt20.7

      \[\leadsto \frac{(b \cdot d + \left(c \cdot a\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied *-un-lft-identity20.7

      \[\leadsto \frac{\color{blue}{1 \cdot (b \cdot d + \left(c \cdot a\right))_*}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    6. Applied times-frac20.7

      \[\leadsto \color{blue}{\frac{1}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    7. Applied simplify20.7

      \[\leadsto \color{blue}{\frac{1}{\sqrt{d^2 + c^2}^*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    8. Applied simplify11.5

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \color{blue}{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{d^2 + c^2}^*}}\]
    9. Using strategy rm
    10. Applied add-sqr-sqrt11.7

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\color{blue}{\sqrt{\sqrt{d^2 + c^2}^*} \cdot \sqrt{\sqrt{d^2 + c^2}^*}}}\]
    11. Applied *-un-lft-identity11.7

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \frac{\color{blue}{1 \cdot (b \cdot d + \left(c \cdot a\right))_*}}{\sqrt{\sqrt{d^2 + c^2}^*} \cdot \sqrt{\sqrt{d^2 + c^2}^*}}\]
    12. Applied times-frac11.7

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \color{blue}{\left(\frac{1}{\sqrt{\sqrt{d^2 + c^2}^*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{\sqrt{d^2 + c^2}^*}}\right)}\]
    13. Using strategy rm
    14. Applied add-cube-cbrt11.9

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \left(\frac{1}{\sqrt{\sqrt{d^2 + c^2}^*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{\color{blue}{\left(\sqrt[3]{\sqrt{d^2 + c^2}^*} \cdot \sqrt[3]{\sqrt{d^2 + c^2}^*}\right) \cdot \sqrt[3]{\sqrt{d^2 + c^2}^*}}}}\right)\]
    15. Applied sqrt-prod11.9

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \left(\frac{1}{\sqrt{\sqrt{d^2 + c^2}^*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\color{blue}{\sqrt{\sqrt[3]{\sqrt{d^2 + c^2}^*} \cdot \sqrt[3]{\sqrt{d^2 + c^2}^*}} \cdot \sqrt{\sqrt[3]{\sqrt{d^2 + c^2}^*}}}}\right)\]
    16. Applied *-un-lft-identity11.9

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \left(\frac{1}{\sqrt{\sqrt{d^2 + c^2}^*}} \cdot \frac{\color{blue}{1 \cdot (b \cdot d + \left(c \cdot a\right))_*}}{\sqrt{\sqrt[3]{\sqrt{d^2 + c^2}^*} \cdot \sqrt[3]{\sqrt{d^2 + c^2}^*}} \cdot \sqrt{\sqrt[3]{\sqrt{d^2 + c^2}^*}}}\right)\]
    17. Applied times-frac11.9

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \left(\frac{1}{\sqrt{\sqrt{d^2 + c^2}^*}} \cdot \color{blue}{\left(\frac{1}{\sqrt{\sqrt[3]{\sqrt{d^2 + c^2}^*} \cdot \sqrt[3]{\sqrt{d^2 + c^2}^*}}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{\sqrt[3]{\sqrt{d^2 + c^2}^*}}}\right)}\right)\]
    18. Applied simplify11.9

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \left(\frac{1}{\sqrt{\sqrt{d^2 + c^2}^*}} \cdot \left(\color{blue}{\frac{1}{\left|\sqrt[3]{\sqrt{d^2 + c^2}^*}\right|}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{\sqrt[3]{\sqrt{d^2 + c^2}^*}}}\right)\right)\]
    19. Taylor expanded around 0 38.9

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \color{blue}{\left(\frac{b \cdot d}{\left|\sqrt[3]{\sqrt{d^2 + c^2}^*}\right|} \cdot {\left(\frac{1}{{c}^{2}}\right)}^{\frac{1}{3}} + {c}^{\frac{1}{3}} \cdot \frac{a}{\left|\sqrt[3]{\sqrt{d^2 + c^2}^*}\right|}\right)}\]
    20. Applied simplify11.0

      \[\leadsto \color{blue}{(\left(\frac{\frac{b \cdot d}{\sqrt{d^2 + c^2}^*}}{\left|\sqrt[3]{\sqrt{d^2 + c^2}^*}\right|}\right) \cdot \left(\sqrt[3]{\frac{1}{c \cdot c}}\right) + \left(\frac{a \cdot \frac{\sqrt[3]{c}}{\sqrt{d^2 + c^2}^*}}{\left|\sqrt[3]{\sqrt{d^2 + c^2}^*}\right|}\right))_*}\]

    if 5.302451626529533e+165 < d

    1. Initial program 45.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Applied simplify45.2

      \[\leadsto \color{blue}{\frac{(b \cdot d + \left(c \cdot a\right))_*}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt45.2

      \[\leadsto \frac{(b \cdot d + \left(c \cdot a\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied *-un-lft-identity45.2

      \[\leadsto \frac{\color{blue}{1 \cdot (b \cdot d + \left(c \cdot a\right))_*}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    6. Applied times-frac45.2

      \[\leadsto \color{blue}{\frac{1}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    7. Applied simplify45.2

      \[\leadsto \color{blue}{\frac{1}{\sqrt{d^2 + c^2}^*}} \cdot \frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    8. Applied simplify30.4

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \color{blue}{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{d^2 + c^2}^*}}\]
    9. Taylor expanded around inf 14.5

      \[\leadsto \frac{1}{\sqrt{d^2 + c^2}^*} \cdot \color{blue}{b}\]
    10. Applied simplify14.3

      \[\leadsto \color{blue}{\frac{b}{\sqrt{d^2 + c^2}^*}}\]
  3. Recombined 4 regimes into one program.

Runtime

Time bar (total: 1.5m)Debug logProfile

herbie shell --seed 2018206 +o rules:numerics
(FPCore (a b c d)
  :name "Complex division, real part"

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d)))))

  (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))