Average Error: 26.1 → 13.1
Time: 1.6m
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.2063458127550573 \cdot 10^{+100}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 4.054287420560557 \cdot 10^{+138}:\\ \;\;\;\;\frac{\frac{1}{\frac{1}{\frac{\mathsf{fma}\left(c, a, \left(d \cdot b\right)\right)}{\mathsf{hypot}\left(c, d\right)}}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\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 -2.2063458127550573 \cdot 10^{+100}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r33878397 = a;
        double r33878398 = c;
        double r33878399 = r33878397 * r33878398;
        double r33878400 = b;
        double r33878401 = d;
        double r33878402 = r33878400 * r33878401;
        double r33878403 = r33878399 + r33878402;
        double r33878404 = r33878398 * r33878398;
        double r33878405 = r33878401 * r33878401;
        double r33878406 = r33878404 + r33878405;
        double r33878407 = r33878403 / r33878406;
        return r33878407;
}

double f(double a, double b, double c, double d) {
        double r33878408 = c;
        double r33878409 = -2.2063458127550573e+100;
        bool r33878410 = r33878408 <= r33878409;
        double r33878411 = a;
        double r33878412 = -r33878411;
        double r33878413 = d;
        double r33878414 = hypot(r33878408, r33878413);
        double r33878415 = r33878412 / r33878414;
        double r33878416 = 4.054287420560557e+138;
        bool r33878417 = r33878408 <= r33878416;
        double r33878418 = 1.0;
        double r33878419 = b;
        double r33878420 = r33878413 * r33878419;
        double r33878421 = fma(r33878408, r33878411, r33878420);
        double r33878422 = r33878421 / r33878414;
        double r33878423 = r33878418 / r33878422;
        double r33878424 = r33878418 / r33878423;
        double r33878425 = r33878424 / r33878414;
        double r33878426 = r33878411 / r33878414;
        double r33878427 = r33878417 ? r33878425 : r33878426;
        double r33878428 = r33878410 ? r33878415 : r33878427;
        return r33878428;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.1
Target0.5
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 < -2.2063458127550573e+100

    1. Initial program 38.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt38.4

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied associate-/r*38.4

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Using strategy rm
    6. Applied hypot-def38.4

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

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

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

    if -2.2063458127550573e+100 < c < 4.054287420560557e+138

    1. Initial program 18.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt18.6

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied associate-/r*18.5

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Using strategy rm
    6. Applied hypot-def18.5

      \[\leadsto \frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}\]
    7. Using strategy rm
    8. Applied clear-num18.6

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

      \[\leadsto \frac{\frac{1}{\color{blue}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(c, a, \left(d \cdot b\right)\right)}}}}{\mathsf{hypot}\left(c, d\right)}\]
    10. Using strategy rm
    11. Applied clear-num11.8

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

    if 4.054287420560557e+138 < c

    1. Initial program 45.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt45.0

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied associate-/r*45.0

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Using strategy rm
    6. Applied hypot-def45.0

      \[\leadsto \frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}\]
    7. Taylor expanded around inf 14.5

      \[\leadsto \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 -2.2063458127550573 \cdot 10^{+100}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 4.054287420560557 \cdot 10^{+138}:\\ \;\;\;\;\frac{\frac{1}{\frac{1}{\frac{\mathsf{fma}\left(c, a, \left(d \cdot b\right)\right)}{\mathsf{hypot}\left(c, d\right)}}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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