Average Error: 26.1 → 13.4
Time: 14.9s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -8.305623690601149751122925407417973083209 \cdot 10^{163}:\\ \;\;\;\;-\frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 2.805404912411875912470168548725953456027 \cdot 10^{84}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, a \cdot \left(-d\right)\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{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -8.305623690601149751122925407417973083209 \cdot 10^{163}:\\
\;\;\;\;-\frac{b}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \le 2.805404912411875912470168548725953456027 \cdot 10^{84}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, a \cdot \left(-d\right)\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 r5563859 = b;
        double r5563860 = c;
        double r5563861 = r5563859 * r5563860;
        double r5563862 = a;
        double r5563863 = d;
        double r5563864 = r5563862 * r5563863;
        double r5563865 = r5563861 - r5563864;
        double r5563866 = r5563860 * r5563860;
        double r5563867 = r5563863 * r5563863;
        double r5563868 = r5563866 + r5563867;
        double r5563869 = r5563865 / r5563868;
        return r5563869;
}

double f(double a, double b, double c, double d) {
        double r5563870 = c;
        double r5563871 = -8.30562369060115e+163;
        bool r5563872 = r5563870 <= r5563871;
        double r5563873 = b;
        double r5563874 = d;
        double r5563875 = hypot(r5563874, r5563870);
        double r5563876 = r5563873 / r5563875;
        double r5563877 = -r5563876;
        double r5563878 = 2.805404912411876e+84;
        bool r5563879 = r5563870 <= r5563878;
        double r5563880 = a;
        double r5563881 = -r5563874;
        double r5563882 = r5563880 * r5563881;
        double r5563883 = fma(r5563873, r5563870, r5563882);
        double r5563884 = r5563883 / r5563875;
        double r5563885 = r5563884 / r5563875;
        double r5563886 = r5563879 ? r5563885 : r5563876;
        double r5563887 = r5563872 ? r5563877 : r5563886;
        return r5563887;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.1
Target0.4
Herbie13.4
\[\begin{array}{l} \mathbf{if}\;\left|d\right| \lt \left|c\right|:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-a\right) + b \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if c < -8.30562369060115e+163

    1. Initial program 45.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified45.0

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\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*45.0

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\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 fma-udef45.0

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

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

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

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

    if -8.30562369060115e+163 < c < 2.805404912411876e+84

    1. Initial program 19.4

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\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*19.4

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\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 fma-udef19.4

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

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

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

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

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

    if 2.805404912411876e+84 < c

    1. Initial program 38.3

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\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*38.3

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\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 fma-udef38.3

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

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

      \[\leadsto \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}\;c \le -8.305623690601149751122925407417973083209 \cdot 10^{163}:\\ \;\;\;\;-\frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 2.805404912411875912470168548725953456027 \cdot 10^{84}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, a \cdot \left(-d\right)\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 2019179 +o rules:numerics
(FPCore (a b c d)
  :name "Complex division, imag part"

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d)))))

  (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))