Average Error: 26.1 → 13.1
Time: 13.0s
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{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\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{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\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 r1747463 = a;
        double r1747464 = c;
        double r1747465 = r1747463 * r1747464;
        double r1747466 = b;
        double r1747467 = d;
        double r1747468 = r1747466 * r1747467;
        double r1747469 = r1747465 + r1747468;
        double r1747470 = r1747464 * r1747464;
        double r1747471 = r1747467 * r1747467;
        double r1747472 = r1747470 + r1747471;
        double r1747473 = r1747469 / r1747472;
        return r1747473;
}

double f(double a, double b, double c, double d) {
        double r1747474 = c;
        double r1747475 = -2.2063458127550573e+100;
        bool r1747476 = r1747474 <= r1747475;
        double r1747477 = a;
        double r1747478 = -r1747477;
        double r1747479 = d;
        double r1747480 = hypot(r1747474, r1747479);
        double r1747481 = r1747478 / r1747480;
        double r1747482 = 4.054287420560557e+138;
        bool r1747483 = r1747474 <= r1747482;
        double r1747484 = 1.0;
        double r1747485 = b;
        double r1747486 = r1747479 * r1747485;
        double r1747487 = fma(r1747477, r1747474, r1747486);
        double r1747488 = r1747480 / r1747487;
        double r1747489 = r1747484 / r1747488;
        double r1747490 = r1747489 / r1747480;
        double r1747491 = r1747477 / r1747480;
        double r1747492 = r1747483 ? r1747490 : r1747491;
        double r1747493 = r1747476 ? r1747481 : r1747492;
        return r1747493;
}

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. Using strategy rm
    8. Applied clear-num38.4

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

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

      \[\leadsto \frac{\color{blue}{-1 \cdot a}}{\mathsf{hypot}\left(c, d\right)}\]
    11. 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(a, c, \left(b \cdot d\right)\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. Using strategy rm
    8. Applied clear-num45.0

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

      \[\leadsto \frac{\frac{1}{\color{blue}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}}}}{\mathsf{hypot}\left(c, d\right)}\]
    10. 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{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\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))))