Average Error: 25.9 → 13.3
Time: 11.3s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.912207042542648550669797463577655559985 \cdot 10^{128}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 5.396385525097289126320674450075267015308 \cdot 10^{222}:\\ \;\;\;\;\frac{\frac{-\mathsf{fma}\left(d, b, a \cdot c\right)}{\mathsf{hypot}\left(d, c\right)}}{-\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\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}\;c \le -1.912207042542648550669797463577655559985 \cdot 10^{128}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \le 5.396385525097289126320674450075267015308 \cdot 10^{222}:\\
\;\;\;\;\frac{\frac{-\mathsf{fma}\left(d, b, a \cdot c\right)}{\mathsf{hypot}\left(d, c\right)}}{-\mathsf{hypot}\left(d, c\right)}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r79858 = a;
        double r79859 = c;
        double r79860 = r79858 * r79859;
        double r79861 = b;
        double r79862 = d;
        double r79863 = r79861 * r79862;
        double r79864 = r79860 + r79863;
        double r79865 = r79859 * r79859;
        double r79866 = r79862 * r79862;
        double r79867 = r79865 + r79866;
        double r79868 = r79864 / r79867;
        return r79868;
}

double f(double a, double b, double c, double d) {
        double r79869 = c;
        double r79870 = -1.9122070425426486e+128;
        bool r79871 = r79869 <= r79870;
        double r79872 = a;
        double r79873 = -r79872;
        double r79874 = d;
        double r79875 = hypot(r79874, r79869);
        double r79876 = r79873 / r79875;
        double r79877 = 5.396385525097289e+222;
        bool r79878 = r79869 <= r79877;
        double r79879 = b;
        double r79880 = r79872 * r79869;
        double r79881 = fma(r79874, r79879, r79880);
        double r79882 = -r79881;
        double r79883 = r79882 / r79875;
        double r79884 = -r79875;
        double r79885 = r79883 / r79884;
        double r79886 = r79872 / r79875;
        double r79887 = r79878 ? r79885 : r79886;
        double r79888 = r79871 ? r79876 : r79887;
        return r79888;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.9
Target0.4
Herbie13.3
\[\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.9122070425426486e+128

    1. Initial program 39.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified39.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-sqrt39.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-identity39.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-frac39.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. Simplified39.9

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

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

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

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

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

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

    if -1.9122070425426486e+128 < c < 5.396385525097289e+222

    1. Initial program 21.6

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

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

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

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

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

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

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

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

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}}{\mathsf{hypot}\left(d, c\right)}\]
    12. Using strategy rm
    13. Applied div-inv13.5

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(a, c, d \cdot b\right) \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}}}{\mathsf{hypot}\left(d, c\right)}\]
    14. Using strategy rm
    15. Applied frac-2neg13.5

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

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

    if 5.396385525097289e+222 < c

    1. Initial program 41.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified41.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-sqrt41.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-identity41.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-frac41.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. Simplified41.3

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.912207042542648550669797463577655559985 \cdot 10^{128}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 5.396385525097289126320674450075267015308 \cdot 10^{222}:\\ \;\;\;\;\frac{\frac{-\mathsf{fma}\left(d, b, a \cdot c\right)}{\mathsf{hypot}\left(d, c\right)}}{-\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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