Average Error: 25.9 → 13.0
Time: 10.6s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.10226154286646485 \cdot 10^{147}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 8.6334403648755915 \cdot 10^{78}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\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 -1.10226154286646485 \cdot 10^{147}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \le 8.6334403648755915 \cdot 10^{78}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\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 r64794 = b;
        double r64795 = c;
        double r64796 = r64794 * r64795;
        double r64797 = a;
        double r64798 = d;
        double r64799 = r64797 * r64798;
        double r64800 = r64796 - r64799;
        double r64801 = r64795 * r64795;
        double r64802 = r64798 * r64798;
        double r64803 = r64801 + r64802;
        double r64804 = r64800 / r64803;
        return r64804;
}

double f(double a, double b, double c, double d) {
        double r64805 = c;
        double r64806 = -1.1022615428664649e+147;
        bool r64807 = r64805 <= r64806;
        double r64808 = b;
        double r64809 = -r64808;
        double r64810 = d;
        double r64811 = hypot(r64805, r64810);
        double r64812 = r64809 / r64811;
        double r64813 = 8.633440364875592e+78;
        bool r64814 = r64805 <= r64813;
        double r64815 = a;
        double r64816 = r64810 * r64815;
        double r64817 = -r64816;
        double r64818 = fma(r64808, r64805, r64817);
        double r64819 = r64818 / r64811;
        double r64820 = r64819 / r64811;
        double r64821 = r64808 / r64811;
        double r64822 = r64814 ? r64820 : r64821;
        double r64823 = r64807 ? r64812 : r64822;
        return r64823;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.9
Target0.4
Herbie13.0
\[\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 < -1.1022615428664649e+147

    1. Initial program 43.9

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity43.9

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac43.9

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified43.9

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity28.9

      \[\leadsto \color{blue}{\left(1 \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}\right)} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}\]
    10. Applied associate-*l*28.9

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

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

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

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

    if -1.1022615428664649e+147 < c < 8.633440364875592e+78

    1. Initial program 18.4

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity18.4

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac18.4

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified18.4

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity11.7

      \[\leadsto \color{blue}{\left(1 \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}\right)} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}\]
    10. Applied associate-*l*11.7

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

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

    if 8.633440364875592e+78 < c

    1. Initial program 38.7

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity38.7

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac38.7

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified38.7

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity27.0

      \[\leadsto \color{blue}{\left(1 \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}\right)} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}\]
    10. Applied associate-*l*27.0

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.10226154286646485 \cdot 10^{147}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 8.6334403648755915 \cdot 10^{78}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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