Average Error: 26.3 → 13.1
Time: 8.6s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -6.75150761769019798 \cdot 10^{142}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{1}\\ \mathbf{elif}\;c \le 1.8955542797321017 \cdot 10^{121}:\\ \;\;\;\;\sqrt{1} \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{1} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -6.75150761769019798 \cdot 10^{142}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{1}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r115759 = a;
        double r115760 = c;
        double r115761 = r115759 * r115760;
        double r115762 = b;
        double r115763 = d;
        double r115764 = r115762 * r115763;
        double r115765 = r115761 + r115764;
        double r115766 = r115760 * r115760;
        double r115767 = r115763 * r115763;
        double r115768 = r115766 + r115767;
        double r115769 = r115765 / r115768;
        return r115769;
}

double f(double a, double b, double c, double d) {
        double r115770 = c;
        double r115771 = -6.751507617690198e+142;
        bool r115772 = r115770 <= r115771;
        double r115773 = a;
        double r115774 = -r115773;
        double r115775 = d;
        double r115776 = hypot(r115770, r115775);
        double r115777 = r115774 / r115776;
        double r115778 = 1.0;
        double r115779 = sqrt(r115778);
        double r115780 = r115777 * r115779;
        double r115781 = 1.8955542797321017e+121;
        bool r115782 = r115770 <= r115781;
        double r115783 = r115778 / r115776;
        double r115784 = b;
        double r115785 = r115775 * r115784;
        double r115786 = fma(r115773, r115770, r115785);
        double r115787 = r115786 / r115776;
        double r115788 = r115783 * r115787;
        double r115789 = r115779 * r115788;
        double r115790 = r115773 / r115776;
        double r115791 = r115779 * r115790;
        double r115792 = r115782 ? r115789 : r115791;
        double r115793 = r115772 ? r115780 : r115792;
        return r115793;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.3
Target0.4
Herbie13.1
\[\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 < -6.751507617690198e+142

    1. Initial program 43.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified43.0

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

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity43.0

      \[\leadsto \frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    6. Applied times-frac43.0

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

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

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

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(c, d\right)}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}\]
    11. Applied add-sqr-sqrt28.4

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

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

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

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

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

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

    if -6.751507617690198e+142 < c < 1.8955542797321017e+121

    1. Initial program 19.3

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

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

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity19.3

      \[\leadsto \frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    6. Applied times-frac19.3

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}\]
    9. Using strategy rm
    10. Applied *-un-lft-identity12.7

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(c, d\right)}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}\]
    11. Applied add-sqr-sqrt12.7

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

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

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

      \[\leadsto \frac{\sqrt{1}}{1} \cdot \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}}\]
    15. Using strategy rm
    16. Applied add-sqr-sqrt12.9

      \[\leadsto \frac{\sqrt{1}}{1} \cdot \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\color{blue}{\sqrt{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{\mathsf{hypot}\left(c, d\right)}}}\]
    17. Applied add-sqr-sqrt13.0

      \[\leadsto \frac{\sqrt{1}}{1} \cdot \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)}}}}{\sqrt{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{\mathsf{hypot}\left(c, d\right)}}\]
    18. Applied *-un-lft-identity13.0

      \[\leadsto \frac{\sqrt{1}}{1} \cdot \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)}}}{\sqrt{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{\mathsf{hypot}\left(c, d\right)}}\]
    19. Applied times-frac13.1

      \[\leadsto \frac{\sqrt{1}}{1} \cdot \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)}}}}{\sqrt{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{\mathsf{hypot}\left(c, d\right)}}\]
    20. Applied times-frac13.1

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

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

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

    if 1.8955542797321017e+121 < c

    1. Initial program 41.9

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

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

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity41.9

      \[\leadsto \frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    6. Applied times-frac41.9

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

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

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

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(c, d\right)}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}\]
    11. Applied add-sqr-sqrt27.9

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

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

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

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

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

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

Reproduce

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