Average Error: 26.8 → 13.4
Time: 18.9s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -3.123225292821243759768406761069460953408 \cdot 10^{168}:\\ \;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot b\\ \mathbf{elif}\;d \le 1.55743649473253234524252115480529742064 \cdot 10^{181}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, b \cdot d\right)}{\mathsf{hypot}\left(d, 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 -3.123225292821243759768406761069460953408 \cdot 10^{168}:\\
\;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot b\\

\mathbf{elif}\;d \le 1.55743649473253234524252115480529742064 \cdot 10^{181}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, b \cdot d\right)}{\mathsf{hypot}\left(d, 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 r4169177 = a;
        double r4169178 = c;
        double r4169179 = r4169177 * r4169178;
        double r4169180 = b;
        double r4169181 = d;
        double r4169182 = r4169180 * r4169181;
        double r4169183 = r4169179 + r4169182;
        double r4169184 = r4169178 * r4169178;
        double r4169185 = r4169181 * r4169181;
        double r4169186 = r4169184 + r4169185;
        double r4169187 = r4169183 / r4169186;
        return r4169187;
}

double f(double a, double b, double c, double d) {
        double r4169188 = d;
        double r4169189 = -3.1232252928212438e+168;
        bool r4169190 = r4169188 <= r4169189;
        double r4169191 = -1.0;
        double r4169192 = c;
        double r4169193 = hypot(r4169188, r4169192);
        double r4169194 = r4169191 / r4169193;
        double r4169195 = b;
        double r4169196 = r4169194 * r4169195;
        double r4169197 = 1.5574364947325323e+181;
        bool r4169198 = r4169188 <= r4169197;
        double r4169199 = a;
        double r4169200 = r4169195 * r4169188;
        double r4169201 = fma(r4169192, r4169199, r4169200);
        double r4169202 = r4169201 / r4169193;
        double r4169203 = r4169202 / r4169193;
        double r4169204 = r4169195 / r4169193;
        double r4169205 = r4169198 ? r4169203 : r4169204;
        double r4169206 = r4169190 ? r4169196 : r4169205;
        return r4169206;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.8
Target0.4
Herbie13.4
\[\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 < -3.1232252928212438e+168

    1. Initial program 44.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified44.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-sqrt44.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*44.9

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

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

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

      \[\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)}}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    10. Applied sqrt-prod44.9

      \[\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)}}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    11. Applied *-un-lft-identity44.9

      \[\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)}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    12. Applied times-frac44.9

      \[\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)}}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    13. Applied times-frac44.9

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

      \[\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)}}\]
    15. Simplified30.6

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

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

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

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

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

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

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

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

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

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

    if -3.1232252928212438e+168 < d < 1.5574364947325323e+181

    1. Initial program 21.3

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

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

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

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

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

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

      \[\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)}}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    10. Applied sqrt-prod21.2

      \[\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)}}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    11. Applied *-un-lft-identity21.2

      \[\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)}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    12. Applied times-frac21.2

      \[\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)}}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    13. Applied times-frac21.2

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{1}}}{\sqrt{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. Simplified21.2

      \[\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)}}\]
    15. Simplified13.3

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

    if 1.5574364947325323e+181 < d

    1. Initial program 44.5

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

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

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

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\color{blue}{1 \cdot \mathsf{fma}\left(d, d, c \cdot c\right)}}}\]
    8. Applied sqrt-prod44.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}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}\]
    9. Applied *-un-lft-identity44.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)}}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    10. Applied sqrt-prod44.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)}}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    11. Applied *-un-lft-identity44.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)}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    12. Applied times-frac44.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)}}}}{\sqrt{1} \cdot \sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\]
    13. Applied times-frac44.5

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -3.123225292821243759768406761069460953408 \cdot 10^{168}:\\ \;\;\;\;\frac{-1}{\mathsf{hypot}\left(d, c\right)} \cdot b\\ \mathbf{elif}\;d \le 1.55743649473253234524252115480529742064 \cdot 10^{181}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, b \cdot d\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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