Average Error: 26.0 → 13.9
Time: 10.5s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.77471575242913168 \cdot 10^{149}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, \sqrt{\frac{-1}{c}} \cdot c, d \cdot \left(\sqrt{\frac{-1}{c}} \cdot b\right)\right)}}\\ \mathbf{elif}\;c \le 6.02089915346211438 \cdot 10^{80}:\\ \;\;\;\;\frac{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{\left(-\frac{1}{2}\right)}}{\frac{\mathsf{hypot}\left(c, d\right)}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\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}\;c \le -1.77471575242913168 \cdot 10^{149}:\\
\;\;\;\;\frac{\frac{1}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, \sqrt{\frac{-1}{c}} \cdot c, d \cdot \left(\sqrt{\frac{-1}{c}} \cdot b\right)\right)}}\\

\mathbf{elif}\;c \le 6.02089915346211438 \cdot 10^{80}:\\
\;\;\;\;\frac{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{\left(-\frac{1}{2}\right)}}{\frac{\mathsf{hypot}\left(c, d\right)}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r160441 = a;
        double r160442 = c;
        double r160443 = r160441 * r160442;
        double r160444 = b;
        double r160445 = d;
        double r160446 = r160444 * r160445;
        double r160447 = r160443 + r160446;
        double r160448 = r160442 * r160442;
        double r160449 = r160445 * r160445;
        double r160450 = r160448 + r160449;
        double r160451 = r160447 / r160450;
        return r160451;
}

double f(double a, double b, double c, double d) {
        double r160452 = c;
        double r160453 = -1.7747157524291317e+149;
        bool r160454 = r160452 <= r160453;
        double r160455 = 1.0;
        double r160456 = d;
        double r160457 = hypot(r160452, r160456);
        double r160458 = sqrt(r160457);
        double r160459 = r160455 / r160458;
        double r160460 = a;
        double r160461 = -1.0;
        double r160462 = r160461 / r160452;
        double r160463 = sqrt(r160462);
        double r160464 = r160463 * r160452;
        double r160465 = b;
        double r160466 = r160463 * r160465;
        double r160467 = r160456 * r160466;
        double r160468 = fma(r160460, r160464, r160467);
        double r160469 = r160457 / r160468;
        double r160470 = r160459 / r160469;
        double r160471 = 6.020899153462114e+80;
        bool r160472 = r160452 <= r160471;
        double r160473 = 0.5;
        double r160474 = -r160473;
        double r160475 = pow(r160457, r160474);
        double r160476 = r160465 * r160456;
        double r160477 = fma(r160460, r160452, r160476);
        double r160478 = r160477 / r160458;
        double r160479 = r160457 / r160478;
        double r160480 = r160475 / r160479;
        double r160481 = r160457 * r160455;
        double r160482 = r160460 / r160481;
        double r160483 = r160472 ? r160480 : r160482;
        double r160484 = r160454 ? r160470 : r160483;
        return r160484;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.4
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 3 regimes
  2. if c < -1.7747157524291317e+149

    1. Initial program 42.7

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

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

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

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

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

      \[\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/27.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. Simplified27.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. Using strategy rm
    12. Applied add-sqr-sqrt27.4

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\sqrt{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{\mathsf{hypot}\left(c, d\right)}}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    13. Applied *-un-lft-identity27.4

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    14. Applied times-frac27.4

      \[\leadsto \frac{\color{blue}{\frac{1}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    15. Applied associate-/l*27.4

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

      \[\leadsto \frac{\frac{1}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}{\color{blue}{\frac{\mathsf{hypot}\left(c, d\right)}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}}}\]
    17. Taylor expanded around -inf 17.4

      \[\leadsto \frac{\frac{1}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}{\frac{\mathsf{hypot}\left(c, d\right)}{\color{blue}{a \cdot \left(\sqrt{\frac{-1}{c}} \cdot c\right) + d \cdot \left(\sqrt{\frac{-1}{c}} \cdot b\right)}}}\]
    18. Simplified17.4

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

    if -1.7747157524291317e+149 < c < 6.020899153462114e+80

    1. Initial program 19.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt19.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-identity19.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-frac19.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. Simplified19.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. Simplified12.0

      \[\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/12.0

      \[\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. Simplified11.9

      \[\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 add-sqr-sqrt12.2

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\sqrt{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{\mathsf{hypot}\left(c, d\right)}}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    13. Applied *-un-lft-identity12.2

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    14. Applied times-frac12.2

      \[\leadsto \frac{\color{blue}{\frac{1}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    15. Applied associate-/l*12.3

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

      \[\leadsto \frac{\frac{1}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}{\color{blue}{\frac{\mathsf{hypot}\left(c, d\right)}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}}}\]
    17. Using strategy rm
    18. Applied pow1/212.3

      \[\leadsto \frac{\frac{1}{\color{blue}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{\frac{1}{2}}}}}{\frac{\mathsf{hypot}\left(c, d\right)}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}}\]
    19. Applied pow-flip12.2

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

    if 6.020899153462114e+80 < c

    1. Initial program 37.5

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

      \[\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-identity37.5

      \[\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-frac37.5

      \[\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. Simplified37.5

      \[\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. Simplified25.5

      \[\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/25.5

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

      \[\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 inf 17.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.77471575242913168 \cdot 10^{149}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, \sqrt{\frac{-1}{c}} \cdot c, d \cdot \left(\sqrt{\frac{-1}{c}} \cdot b\right)\right)}}\\ \mathbf{elif}\;c \le 6.02089915346211438 \cdot 10^{80}:\\ \;\;\;\;\frac{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{\left(-\frac{1}{2}\right)}}{\frac{\mathsf{hypot}\left(c, d\right)}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \end{array}\]

Reproduce

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