Average Error: 26.1 → 26.4
Time: 15.7s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.68397146889473803549156226978795498139 \cdot 10^{152}:\\ \;\;\;\;e^{\mathsf{fma}\left(-2, \log \left(\mathsf{hypot}\left(d, c\right)\right), \log \left(\mathsf{fma}\left(c, a, d \cdot b\right)\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot 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.68397146889473803549156226978795498139 \cdot 10^{152}:\\
\;\;\;\;e^{\mathsf{fma}\left(-2, \log \left(\mathsf{hypot}\left(d, c\right)\right), \log \left(\mathsf{fma}\left(c, a, d \cdot b\right)\right)\right)}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r4493474 = a;
        double r4493475 = c;
        double r4493476 = r4493474 * r4493475;
        double r4493477 = b;
        double r4493478 = d;
        double r4493479 = r4493477 * r4493478;
        double r4493480 = r4493476 + r4493479;
        double r4493481 = r4493475 * r4493475;
        double r4493482 = r4493478 * r4493478;
        double r4493483 = r4493481 + r4493482;
        double r4493484 = r4493480 / r4493483;
        return r4493484;
}

double f(double a, double b, double c, double d) {
        double r4493485 = c;
        double r4493486 = -1.683971468894738e+152;
        bool r4493487 = r4493485 <= r4493486;
        double r4493488 = -2.0;
        double r4493489 = d;
        double r4493490 = hypot(r4493489, r4493485);
        double r4493491 = log(r4493490);
        double r4493492 = a;
        double r4493493 = b;
        double r4493494 = r4493489 * r4493493;
        double r4493495 = fma(r4493485, r4493492, r4493494);
        double r4493496 = log(r4493495);
        double r4493497 = fma(r4493488, r4493491, r4493496);
        double r4493498 = exp(r4493497);
        double r4493499 = fma(r4493492, r4493485, r4493494);
        double r4493500 = r4493485 * r4493485;
        double r4493501 = fma(r4493489, r4493489, r4493500);
        double r4493502 = sqrt(r4493501);
        double r4493503 = r4493499 / r4493502;
        double r4493504 = r4493503 / r4493502;
        double r4493505 = r4493487 ? r4493498 : r4493504;
        return r4493505;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.1
Target0.4
Herbie26.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 2 regimes
  2. if c < -1.683971468894738e+152

    1. Initial program 44.8

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

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

      \[\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. Using strategy rm
    6. Applied add-exp-log44.9

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot \color{blue}{e^{\log \left(\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}\right)}}}\]
    7. Applied add-exp-log44.9

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{e^{\log \left(\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}\right)}} \cdot e^{\log \left(\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}\right)}}\]
    8. Applied prod-exp44.9

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{e^{\log \left(\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}\right) + \log \left(\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}\right)}}}\]
    9. Applied add-exp-log54.6

      \[\leadsto \frac{\color{blue}{e^{\log \left(\mathsf{fma}\left(a, c, b \cdot d\right)\right)}}}{e^{\log \left(\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}\right) + \log \left(\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}\right)}}\]
    10. Applied div-exp54.6

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(a, c, b \cdot d\right)\right) - \left(\log \left(\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}\right) + \log \left(\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}\right)\right)}}\]
    11. Simplified48.1

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

    if -1.683971468894738e+152 < c

    1. Initial program 23.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified23.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-sqrt23.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*23.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)}}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification26.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.68397146889473803549156226978795498139 \cdot 10^{152}:\\ \;\;\;\;e^{\mathsf{fma}\left(-2, \log \left(\mathsf{hypot}\left(d, c\right)\right), \log \left(\mathsf{fma}\left(c, a, d \cdot b\right)\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, c \cdot c\right)}}\\ \end{array}\]

Reproduce

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