Average Error: 25.6 → 12.5
Time: 20.5s
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 -5.105055576389387 \cdot 10^{+217}:\\ \;\;\;\;\frac{-b}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;d \le 7.65041160355757 \cdot 10^{+208}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{d^2 + c^2}^*} \cdot (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.6
Target0.4
Herbie12.5
\[\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 < -5.105055576389387e+217

    1. Initial program 42.4

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

      \[\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-sqrt42.4

      \[\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*42.4

      \[\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-udef42.4

      \[\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-def42.4

      \[\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-identity42.4

      \[\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-identity42.4

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

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

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

      \[\leadsto \frac{\color{blue}{\frac{1}{\sqrt{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}^*}\]
    15. Applied times-frac42.4

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{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}^*}}\]
    16. Simplified42.4

      \[\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}^*}\]
    17. Simplified30.6

      \[\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}^*}}\]
    18. Taylor expanded around -inf 10.5

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

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

    if -5.105055576389387e+217 < d < 7.65041160355757e+208

    1. Initial program 22.3

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

      \[\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-sqrt22.3

      \[\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*22.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-udef22.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-def22.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-identity22.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-identity22.2

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

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

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

      \[\leadsto \frac{\color{blue}{\frac{1}{\sqrt{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}^*}\]
    15. Applied times-frac22.2

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{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}^*}}\]
    16. Simplified22.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}^*}\]
    17. Simplified13.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}^*}}\]
    18. Using strategy rm
    19. Applied div-inv13.0

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

    if 7.65041160355757e+208 < d

    1. Initial program 41.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified41.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-sqrt41.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*41.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-udef41.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-def41.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. Taylor expanded around 0 9.4

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -5.105055576389387 \cdot 10^{+217}:\\ \;\;\;\;\frac{-b}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;d \le 7.65041160355757 \cdot 10^{+208}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{d^2 + c^2}^*} \cdot (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 2019089 +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))))