Average Error: 26.1 → 14.0
Time: 5.0s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -4.28661918211577314 \cdot 10^{139}:\\ \;\;\;\;1 \cdot \frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le -1.01190047403502618 \cdot 10^{-272}:\\ \;\;\;\;\frac{b}{\frac{\mathsf{fma}\left(c, c, d \cdot d\right)}{c}} - \frac{a}{\frac{\mathsf{fma}\left(c, c, d \cdot d\right)}{d}}\\ \mathbf{elif}\;c \le 6.80234349703359644 \cdot 10^{174}:\\ \;\;\;\;1 \cdot \frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{b \cdot c - a \cdot d}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \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 -4.28661918211577314 \cdot 10^{139}:\\
\;\;\;\;1 \cdot \frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\

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

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r120731 = b;
        double r120732 = c;
        double r120733 = r120731 * r120732;
        double r120734 = a;
        double r120735 = d;
        double r120736 = r120734 * r120735;
        double r120737 = r120733 - r120736;
        double r120738 = r120732 * r120732;
        double r120739 = r120735 * r120735;
        double r120740 = r120738 + r120739;
        double r120741 = r120737 / r120740;
        return r120741;
}

double f(double a, double b, double c, double d) {
        double r120742 = c;
        double r120743 = -4.286619182115773e+139;
        bool r120744 = r120742 <= r120743;
        double r120745 = 1.0;
        double r120746 = -1.0;
        double r120747 = b;
        double r120748 = r120746 * r120747;
        double r120749 = d;
        double r120750 = hypot(r120742, r120749);
        double r120751 = r120748 / r120750;
        double r120752 = r120745 * r120751;
        double r120753 = -1.0119004740350262e-272;
        bool r120754 = r120742 <= r120753;
        double r120755 = r120749 * r120749;
        double r120756 = fma(r120742, r120742, r120755);
        double r120757 = r120756 / r120742;
        double r120758 = r120747 / r120757;
        double r120759 = a;
        double r120760 = r120756 / r120749;
        double r120761 = r120759 / r120760;
        double r120762 = r120758 - r120761;
        double r120763 = 6.8023434970335964e+174;
        bool r120764 = r120742 <= r120763;
        double r120765 = r120747 * r120742;
        double r120766 = r120759 * r120749;
        double r120767 = r120765 - r120766;
        double r120768 = r120750 / r120767;
        double r120769 = r120745 / r120768;
        double r120770 = r120769 / r120750;
        double r120771 = r120745 * r120770;
        double r120772 = r120747 / r120750;
        double r120773 = r120745 * r120772;
        double r120774 = r120764 ? r120771 : r120773;
        double r120775 = r120754 ? r120762 : r120774;
        double r120776 = r120744 ? r120752 : r120775;
        return r120776;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.1
Target0.5
Herbie14.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 4 regimes
  2. if c < -4.286619182115773e+139

    1. Initial program 43.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt43.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-identity43.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-frac43.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. Simplified43.4

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

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

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

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

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

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

    if -4.286619182115773e+139 < c < -1.0119004740350262e-272

    1. Initial program 19.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied div-sub19.0

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

      \[\leadsto \color{blue}{\frac{b}{\frac{\mathsf{fma}\left(c, c, d \cdot d\right)}{c}}} - \frac{a \cdot d}{c \cdot c + d \cdot d}\]
    5. Simplified15.7

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

    if -1.0119004740350262e-272 < c < 6.8023434970335964e+174

    1. Initial program 21.0

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

      \[\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-identity21.0

      \[\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-frac21.0

      \[\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. Simplified21.0

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

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

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

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

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

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

    if 6.8023434970335964e+174 < c

    1. Initial program 43.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt43.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-identity43.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-frac43.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. Simplified43.4

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -4.28661918211577314 \cdot 10^{139}:\\ \;\;\;\;1 \cdot \frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le -1.01190047403502618 \cdot 10^{-272}:\\ \;\;\;\;\frac{b}{\frac{\mathsf{fma}\left(c, c, d \cdot d\right)}{c}} - \frac{a}{\frac{\mathsf{fma}\left(c, c, d \cdot d\right)}{d}}\\ \mathbf{elif}\;c \le 6.80234349703359644 \cdot 10^{174}:\\ \;\;\;\;1 \cdot \frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{b \cdot c - a \cdot d}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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