Average Error: 26.2 → 14.7
Time: 18.0s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le 9.842459121637509 \cdot 10^{+163}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \le 9.842459121637509 \cdot 10^{+163}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{\mathsf{hypot}\left(d, c\right)}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r4473138 = a;
        double r4473139 = c;
        double r4473140 = r4473138 * r4473139;
        double r4473141 = b;
        double r4473142 = d;
        double r4473143 = r4473141 * r4473142;
        double r4473144 = r4473140 + r4473143;
        double r4473145 = r4473139 * r4473139;
        double r4473146 = r4473142 * r4473142;
        double r4473147 = r4473145 + r4473146;
        double r4473148 = r4473144 / r4473147;
        return r4473148;
}

double f(double a, double b, double c, double d) {
        double r4473149 = d;
        double r4473150 = 9.842459121637509e+163;
        bool r4473151 = r4473149 <= r4473150;
        double r4473152 = a;
        double r4473153 = c;
        double r4473154 = b;
        double r4473155 = r4473154 * r4473149;
        double r4473156 = fma(r4473152, r4473153, r4473155);
        double r4473157 = hypot(r4473149, r4473153);
        double r4473158 = r4473156 / r4473157;
        double r4473159 = r4473158 / r4473157;
        double r4473160 = r4473154 / r4473157;
        double r4473161 = r4473151 ? r4473159 : r4473160;
        return r4473161;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.2
Target0.5
Herbie14.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 2 regimes
  2. if d < 9.842459121637509e+163

    1. Initial program 23.5

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt23.5

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}\]
    5. Applied associate-/r*23.4

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}\]
    6. Using strategy rm
    7. Applied fma-udef23.4

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    8. Applied hypot-def23.4

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    9. Using strategy rm
    10. Applied fma-udef23.4

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def14.9

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}\]

    if 9.842459121637509e+163 < d

    1. Initial program 44.9

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt44.9

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}\]
    5. Applied associate-/r*44.9

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}\]
    6. Using strategy rm
    7. Applied fma-udef44.9

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    8. Applied hypot-def44.9

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    9. Using strategy rm
    10. Applied fma-udef44.9

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def29.3

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}\]
    12. Taylor expanded around 0 13.0

      \[\leadsto \frac{\color{blue}{b}}{\mathsf{hypot}\left(d, c\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification14.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le 9.842459121637509 \cdot 10^{+163}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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