Average Error: 25.9 → 13.7
Time: 24.6s
Precision: 64
Internal Precision: 128
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -2.3876592550692504 \cdot 10^{+96}:\\ \;\;\;\;\frac{-b}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;d \le 1.4830868861678625 \cdot 10^{+72}:\\ \;\;\;\;\frac{\frac{1}{\frac{\sqrt{d^2 + c^2}^*}{(b \cdot d + \left(a \cdot c\right))_*}}}{\sqrt{d^2 + c^2}^*}\\ \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.9
Target0.5
Herbie13.7
\[\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 3 regimes
  2. if d < -2.3876592550692504e+96

    1. Initial program 38.2

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

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

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied associate-/r*38.2

      \[\leadsto \color{blue}{\frac{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    6. Using strategy rm
    7. Applied fma-udef38.2

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

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

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

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

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

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

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

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

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

      \[\leadsto 1 \cdot \frac{\color{blue}{-1 \cdot b}}{\sqrt{d^2 + c^2}^*}\]
    18. Simplified17.4

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

    if -2.3876592550692504e+96 < d < 1.4830868861678625e+72

    1. Initial program 17.8

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified17.8

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

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied associate-/r*17.7

      \[\leadsto \color{blue}{\frac{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    6. Using strategy rm
    7. Applied fma-udef17.7

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

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

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

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

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

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

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

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

      \[\leadsto 1 \cdot \color{blue}{\frac{\frac{(b \cdot d + \left(a \cdot c\right))_*}{\sqrt{d^2 + c^2}^*}}{\sqrt{d^2 + c^2}^*}}\]
    17. Using strategy rm
    18. Applied clear-num10.9

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

    if 1.4830868861678625e+72 < d

    1. Initial program 38.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified38.6

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

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied associate-/r*38.6

      \[\leadsto \color{blue}{\frac{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    6. Using strategy rm
    7. Applied fma-udef38.6

      \[\leadsto \frac{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    8. Applied hypot-def38.6

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -2.3876592550692504 \cdot 10^{+96}:\\ \;\;\;\;\frac{-b}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;d \le 1.4830868861678625 \cdot 10^{+72}:\\ \;\;\;\;\frac{\frac{1}{\frac{\sqrt{d^2 + c^2}^*}{(b \cdot d + \left(a \cdot c\right))_*}}}{\sqrt{d^2 + c^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{d^2 + c^2}^*}\\ \end{array}\]

Reproduce

herbie shell --seed 2019053 +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))))