Average Error: 26.0 → 13.9
Time: 4.1s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 2.846399672864898633139753001838080406359 \cdot 10^{282}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 2.846399672864898633139753001838080406359 \cdot 10^{282}:\\
\;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r101434 = a;
        double r101435 = c;
        double r101436 = r101434 * r101435;
        double r101437 = b;
        double r101438 = d;
        double r101439 = r101437 * r101438;
        double r101440 = r101436 + r101439;
        double r101441 = r101435 * r101435;
        double r101442 = r101438 * r101438;
        double r101443 = r101441 + r101442;
        double r101444 = r101440 / r101443;
        return r101444;
}

double f(double a, double b, double c, double d) {
        double r101445 = a;
        double r101446 = c;
        double r101447 = r101445 * r101446;
        double r101448 = b;
        double r101449 = d;
        double r101450 = r101448 * r101449;
        double r101451 = r101447 + r101450;
        double r101452 = r101446 * r101446;
        double r101453 = r101449 * r101449;
        double r101454 = r101452 + r101453;
        double r101455 = r101451 / r101454;
        double r101456 = 2.8463996728648986e+282;
        bool r101457 = r101455 <= r101456;
        double r101458 = 1.0;
        double r101459 = hypot(r101446, r101449);
        double r101460 = fma(r101445, r101446, r101450);
        double r101461 = r101459 / r101460;
        double r101462 = r101458 / r101461;
        double r101463 = r101459 * r101458;
        double r101464 = r101462 / r101463;
        double r101465 = r101448 / r101463;
        double r101466 = r101457 ? r101464 : r101465;
        return r101466;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.5
Herbie13.9
\[\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 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) < 2.8463996728648986e+282

    1. Initial program 14.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt14.1

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity14.1

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac14.1

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified14.1

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified2.9

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}\]
    8. Using strategy rm
    9. Applied associate-*r/2.8

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

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    11. Using strategy rm
    12. Applied clear-num2.8

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

    if 2.8463996728648986e+282 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

    1. Initial program 62.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt62.9

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity62.9

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac62.9

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified62.9

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified60.3

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}\]
    8. Using strategy rm
    9. Applied associate-*r/60.3

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

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

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

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

Reproduce

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

  :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))))