Average Error: 25.7 → 13.0
Time: 19.4s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -7.67400831190257 \cdot 10^{+96}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \le 4.711008584896772 \cdot 10^{+178}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, d, a \cdot 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 -7.67400831190257 \cdot 10^{+96}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;d \le 4.711008584896772 \cdot 10^{+178}:\\
\;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, d, a \cdot 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 r2605844 = a;
        double r2605845 = c;
        double r2605846 = r2605844 * r2605845;
        double r2605847 = b;
        double r2605848 = d;
        double r2605849 = r2605847 * r2605848;
        double r2605850 = r2605846 + r2605849;
        double r2605851 = r2605845 * r2605845;
        double r2605852 = r2605848 * r2605848;
        double r2605853 = r2605851 + r2605852;
        double r2605854 = r2605850 / r2605853;
        return r2605854;
}

double f(double a, double b, double c, double d) {
        double r2605855 = d;
        double r2605856 = -7.67400831190257e+96;
        bool r2605857 = r2605855 <= r2605856;
        double r2605858 = b;
        double r2605859 = -r2605858;
        double r2605860 = c;
        double r2605861 = hypot(r2605855, r2605860);
        double r2605862 = r2605859 / r2605861;
        double r2605863 = 4.711008584896772e+178;
        bool r2605864 = r2605855 <= r2605863;
        double r2605865 = 1.0;
        double r2605866 = a;
        double r2605867 = r2605866 * r2605860;
        double r2605868 = fma(r2605858, r2605855, r2605867);
        double r2605869 = r2605861 / r2605868;
        double r2605870 = r2605865 / r2605869;
        double r2605871 = r2605870 / r2605861;
        double r2605872 = r2605858 / r2605861;
        double r2605873 = r2605864 ? r2605871 : r2605872;
        double r2605874 = r2605857 ? r2605862 : r2605873;
        return r2605874;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.7
Target0.4
Herbie13.0
\[\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 d < -7.67400831190257e+96

    1. Initial program 38.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified38.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-sqrt38.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*38.8

      \[\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 *-un-lft-identity38.8

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

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

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

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

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

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{1}}}{1} \cdot \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)}}}\]
    13. Simplified38.8

      \[\leadsto \color{blue}{1} \cdot \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)}}\]
    14. Simplified25.9

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

      \[\leadsto 1 \cdot \frac{\color{blue}{-1 \cdot b}}{\mathsf{hypot}\left(d, c\right)}\]
    16. Simplified17.2

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

    if -7.67400831190257e+96 < d < 4.711008584896772e+178

    1. Initial program 19.6

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

      \[\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-sqrt19.6

      \[\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*19.5

      \[\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 *-un-lft-identity19.5

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

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

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

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

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

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{1}}}{1} \cdot \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)}}}\]
    13. Simplified19.5

      \[\leadsto \color{blue}{1} \cdot \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)}}\]
    14. Simplified12.1

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

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

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

    if 4.711008584896772e+178 < d

    1. Initial program 44.1

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

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

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

      \[\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 *-un-lft-identity44.1

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

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

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

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

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

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{1}}}{1} \cdot \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)}}}\]
    13. Simplified44.1

      \[\leadsto \color{blue}{1} \cdot \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)}}\]
    14. Simplified30.5

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

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

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

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

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

Reproduce

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