Average Error: 26.2 → 13.1
Time: 28.3s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.1105748288996943 \cdot 10^{+133}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 7.527112129916618 \cdot 10^{+115}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\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.1105748288996943 \cdot 10^{+133}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \le 7.527112129916618 \cdot 10^{+115}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\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 r23299673 = a;
        double r23299674 = c;
        double r23299675 = r23299673 * r23299674;
        double r23299676 = b;
        double r23299677 = d;
        double r23299678 = r23299676 * r23299677;
        double r23299679 = r23299675 + r23299678;
        double r23299680 = r23299674 * r23299674;
        double r23299681 = r23299677 * r23299677;
        double r23299682 = r23299680 + r23299681;
        double r23299683 = r23299679 / r23299682;
        return r23299683;
}

double f(double a, double b, double c, double d) {
        double r23299684 = c;
        double r23299685 = -1.1105748288996943e+133;
        bool r23299686 = r23299684 <= r23299685;
        double r23299687 = a;
        double r23299688 = -r23299687;
        double r23299689 = d;
        double r23299690 = hypot(r23299689, r23299684);
        double r23299691 = r23299688 / r23299690;
        double r23299692 = 7.527112129916618e+115;
        bool r23299693 = r23299684 <= r23299692;
        double r23299694 = b;
        double r23299695 = r23299689 * r23299694;
        double r23299696 = fma(r23299687, r23299684, r23299695);
        double r23299697 = r23299696 / r23299690;
        double r23299698 = r23299697 / r23299690;
        double r23299699 = r23299687 / r23299690;
        double r23299700 = r23299693 ? r23299698 : r23299699;
        double r23299701 = r23299686 ? r23299691 : r23299700;
        return r23299701;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.2
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 < -1.1105748288996943e+133

    1. Initial program 42.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified42.1

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt42.1

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    5. Applied associate-/r*42.1

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    6. Using strategy rm
    7. Applied fma-udef42.1

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

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

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{hypot}\left(d, c\right)}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def27.1

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

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

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

    if -1.1105748288996943e+133 < c < 7.527112129916618e+115

    1. Initial program 19.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified19.1

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt19.1

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    5. Applied associate-/r*19.0

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    6. Using strategy rm
    7. Applied fma-udef19.0

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    8. Applied hypot-def19.0

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    9. Using strategy rm
    10. Applied fma-udef19.0

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{hypot}\left(d, c\right)}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def11.8

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

    if 7.527112129916618e+115 < c

    1. Initial program 41.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified41.1

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt41.1

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    5. Applied associate-/r*41.1

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    6. Using strategy rm
    7. Applied fma-udef41.1

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

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

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{hypot}\left(d, c\right)}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def27.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.1105748288996943 \cdot 10^{+133}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 7.527112129916618 \cdot 10^{+115}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\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 2019121 +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))))