Average Error: 25.6 → 14.2
Time: 29.2s
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 4.1995575871588165 \cdot 10^{+213}:\\ \;\;\;\;\frac{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{d^2 + c^2}^*}}{\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
Herbie14.2
\[\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 2 regimes
  2. if d < 4.1995575871588165e+213

    1. Initial program 24.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified24.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-sqrt24.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*24.1

      \[\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-udef24.1

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

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

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

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

    if 4.1995575871588165e+213 < d

    1. Initial program 41.1

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

      \[\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.1

      \[\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.1

      \[\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.1

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le 4.1995575871588165 \cdot 10^{+213}:\\ \;\;\;\;\frac{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{d^2 + c^2}^*}}{\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))))