Average Error: 26.2 → 13.2
Time: 4.3s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.5095625752469799 \cdot 10^{113}:\\ \;\;\;\;\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 1.04610986578791549 \cdot 10^{-126}:\\ \;\;\;\;\frac{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 4.02564099829727292 \cdot 10^{-53}:\\ \;\;\;\;\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 2.14277106081791793 \cdot 10^{130}:\\ \;\;\;\;\frac{\frac{b \cdot c - a \cdot d}{\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 -2.5095625752469799 \cdot 10^{113}:\\
\;\;\;\;\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\

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

\mathbf{elif}\;c \le 4.02564099829727292 \cdot 10^{-53}:\\
\;\;\;\;\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 2.14277106081791793 \cdot 10^{130}:\\
\;\;\;\;\frac{\frac{b \cdot c - a \cdot d}{\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 r112784 = b;
        double r112785 = c;
        double r112786 = r112784 * r112785;
        double r112787 = a;
        double r112788 = d;
        double r112789 = r112787 * r112788;
        double r112790 = r112786 - r112789;
        double r112791 = r112785 * r112785;
        double r112792 = r112788 * r112788;
        double r112793 = r112791 + r112792;
        double r112794 = r112790 / r112793;
        return r112794;
}

double f(double a, double b, double c, double d) {
        double r112795 = c;
        double r112796 = -2.50956257524698e+113;
        bool r112797 = r112795 <= r112796;
        double r112798 = -1.0;
        double r112799 = b;
        double r112800 = r112798 * r112799;
        double r112801 = d;
        double r112802 = hypot(r112795, r112801);
        double r112803 = r112800 / r112802;
        double r112804 = 1.0461098657879155e-126;
        bool r112805 = r112795 <= r112804;
        double r112806 = r112799 * r112795;
        double r112807 = a;
        double r112808 = r112807 * r112801;
        double r112809 = r112806 - r112808;
        double r112810 = r112809 / r112802;
        double r112811 = r112810 / r112802;
        double r112812 = 4.025640998297273e-53;
        bool r112813 = r112795 <= r112812;
        double r112814 = r112801 * r112801;
        double r112815 = fma(r112795, r112795, r112814);
        double r112816 = r112815 / r112795;
        double r112817 = r112799 / r112816;
        double r112818 = r112815 / r112801;
        double r112819 = r112807 / r112818;
        double r112820 = r112817 - r112819;
        double r112821 = 2.142771060817918e+130;
        bool r112822 = r112795 <= r112821;
        double r112823 = r112799 / r112802;
        double r112824 = r112822 ? r112811 : r112823;
        double r112825 = r112813 ? r112820 : r112824;
        double r112826 = r112805 ? r112811 : r112825;
        double r112827 = r112797 ? r112803 : r112826;
        return r112827;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.2
Target0.4
Herbie13.2
\[\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 < -2.50956257524698e+113

    1. Initial program 42.1

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

      \[\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-identity42.1

      \[\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-frac42.1

      \[\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. Simplified42.1

      \[\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. Simplified28.2

      \[\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 associate-*r/28.2

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

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

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

    if -2.50956257524698e+113 < c < 1.0461098657879155e-126 or 4.025640998297273e-53 < c < 2.142771060817918e+130

    1. Initial program 18.8

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

      \[\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.8

      \[\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.8

      \[\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.8

      \[\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.4

      \[\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 associate-*r/12.4

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

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

    if 1.0461098657879155e-126 < c < 4.025640998297273e-53

    1. Initial program 13.8

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

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

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

      \[\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 2.142771060817918e+130 < c

    1. Initial program 43.1

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

      \[\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.1

      \[\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.1

      \[\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.1

      \[\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.8

      \[\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 associate-*r/27.8

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -2.5095625752469799 \cdot 10^{113}:\\ \;\;\;\;\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 1.04610986578791549 \cdot 10^{-126}:\\ \;\;\;\;\frac{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 4.02564099829727292 \cdot 10^{-53}:\\ \;\;\;\;\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 2.14277106081791793 \cdot 10^{130}:\\ \;\;\;\;\frac{\frac{b \cdot c - a \cdot d}{\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 2020049 +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))))