Average Error: 26.2 → 13.5
Time: 13.4s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -3.616216700651156031698756854769588375128 \cdot 10^{205}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 3.891679100942667758376302916106240739893 \cdot 10^{210}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(c, b, -d \cdot a\right)}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -3.616216700651156031698756854769588375128 \cdot 10^{205}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r78037 = b;
        double r78038 = c;
        double r78039 = r78037 * r78038;
        double r78040 = a;
        double r78041 = d;
        double r78042 = r78040 * r78041;
        double r78043 = r78039 - r78042;
        double r78044 = r78038 * r78038;
        double r78045 = r78041 * r78041;
        double r78046 = r78044 + r78045;
        double r78047 = r78043 / r78046;
        return r78047;
}

double f(double a, double b, double c, double d) {
        double r78048 = c;
        double r78049 = -3.616216700651156e+205;
        bool r78050 = r78048 <= r78049;
        double r78051 = b;
        double r78052 = -r78051;
        double r78053 = d;
        double r78054 = hypot(r78048, r78053);
        double r78055 = r78052 / r78054;
        double r78056 = 3.891679100942668e+210;
        bool r78057 = r78048 <= r78056;
        double r78058 = 1.0;
        double r78059 = a;
        double r78060 = r78053 * r78059;
        double r78061 = -r78060;
        double r78062 = fma(r78048, r78051, r78061);
        double r78063 = r78054 / r78062;
        double r78064 = r78058 / r78063;
        double r78065 = r78064 / r78054;
        double r78066 = r78051 / r78054;
        double r78067 = r78057 ? r78065 : r78066;
        double r78068 = r78050 ? r78055 : r78067;
        return r78068;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.2
Target0.5
Herbie13.5
\[\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 < -3.616216700651156e+205

    1. Initial program 42.6

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity42.6

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    6. Applied times-frac42.6

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\]
    9. Using strategy rm
    10. Applied associate-*r/30.8

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

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

      \[\leadsto \frac{\color{blue}{-1 \cdot b}}{\mathsf{hypot}\left(c, d\right)}\]
    13. Simplified11.2

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

    if -3.616216700651156e+205 < c < 3.891679100942668e+210

    1. Initial program 23.0

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity23.0

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    6. Applied times-frac23.0

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    7. Simplified22.9

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\]
    9. Using strategy rm
    10. Applied associate-*r/14.0

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

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

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

    if 3.891679100942668e+210 < c

    1. Initial program 42.4

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    5. Applied *-un-lft-identity42.4

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}\]
    6. Applied times-frac42.4

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}\]
    7. Simplified42.4

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}\]
    9. Using strategy rm
    10. Applied associate-*r/32.3

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -3.616216700651156031698756854769588375128 \cdot 10^{205}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 3.891679100942667758376302916106240739893 \cdot 10^{210}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(c, b, -d \cdot a\right)}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2019305 +o rules:numerics
(FPCore (a b c d)
  :name "Complex division, imag part"
  :precision binary64

  :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))))