Average Error: 26.4 → 0.9
Time: 4.6s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -4.2293107756407222 \cdot 10^{47} \lor \neg \left(c \le 9.94539393289035303 \cdot 10^{36}\right):\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{c}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, -\frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \left(\left(-\frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{b \cdot c}{\mathsf{hypot}\left(c, d\right)} - \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}}{\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.2293107756407222 \cdot 10^{47} \lor \neg \left(c \le 9.94539393289035303 \cdot 10^{36}\right):\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{c}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, -\frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \left(\left(-\frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right)}{\mathsf{hypot}\left(c, d\right)}\\

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

\end{array}
double f(double a, double b, double c, double d) {
        double r108713 = b;
        double r108714 = c;
        double r108715 = r108713 * r108714;
        double r108716 = a;
        double r108717 = d;
        double r108718 = r108716 * r108717;
        double r108719 = r108715 - r108718;
        double r108720 = r108714 * r108714;
        double r108721 = r108717 * r108717;
        double r108722 = r108720 + r108721;
        double r108723 = r108719 / r108722;
        return r108723;
}

double f(double a, double b, double c, double d) {
        double r108724 = c;
        double r108725 = -4.229310775640722e+47;
        bool r108726 = r108724 <= r108725;
        double r108727 = 9.945393932890353e+36;
        bool r108728 = r108724 <= r108727;
        double r108729 = !r108728;
        bool r108730 = r108726 || r108729;
        double r108731 = b;
        double r108732 = d;
        double r108733 = hypot(r108724, r108732);
        double r108734 = sqrt(r108733);
        double r108735 = r108731 / r108734;
        double r108736 = r108724 / r108734;
        double r108737 = r108732 / r108734;
        double r108738 = a;
        double r108739 = r108738 / r108734;
        double r108740 = r108737 * r108739;
        double r108741 = -r108740;
        double r108742 = fma(r108735, r108736, r108741);
        double r108743 = -r108739;
        double r108744 = r108743 + r108739;
        double r108745 = r108737 * r108744;
        double r108746 = r108742 + r108745;
        double r108747 = r108746 / r108733;
        double r108748 = r108731 * r108724;
        double r108749 = r108748 / r108733;
        double r108750 = r108733 / r108732;
        double r108751 = r108738 / r108750;
        double r108752 = r108749 - r108751;
        double r108753 = r108752 / r108733;
        double r108754 = r108730 ? r108747 : r108753;
        return r108754;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.4
Target0.4
Herbie0.9
\[\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 2 regimes
  2. if c < -4.229310775640722e+47 or 9.945393932890353e+36 < c

    1. Initial program 35.7

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt35.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-identity35.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-frac35.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. Simplified35.7

      \[\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. Simplified24.6

      \[\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/24.5

      \[\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. Simplified24.5

      \[\leadsto \frac{\color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right)}\]
    11. Using strategy rm
    12. Applied div-sub24.5

      \[\leadsto \frac{\color{blue}{\frac{b \cdot c}{\mathsf{hypot}\left(c, d\right)} - \frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right)}\]
    13. Using strategy rm
    14. Applied add-sqr-sqrt24.5

      \[\leadsto \frac{\frac{b \cdot c}{\mathsf{hypot}\left(c, d\right)} - \frac{a \cdot d}{\color{blue}{\sqrt{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{\mathsf{hypot}\left(c, d\right)}}}}{\mathsf{hypot}\left(c, d\right)}\]
    15. Applied times-frac18.9

      \[\leadsto \frac{\frac{b \cdot c}{\mathsf{hypot}\left(c, d\right)} - \color{blue}{\frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}}{\mathsf{hypot}\left(c, d\right)}\]
    16. Applied add-sqr-sqrt19.0

      \[\leadsto \frac{\frac{b \cdot c}{\color{blue}{\sqrt{\mathsf{hypot}\left(c, d\right)} \cdot \sqrt{\mathsf{hypot}\left(c, d\right)}}} - \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right)}\]
    17. Applied times-frac0.5

      \[\leadsto \frac{\color{blue}{\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{c}{\sqrt{\mathsf{hypot}\left(c, d\right)}}} - \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right)}\]
    18. Applied prod-diff0.5

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

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

    if -4.229310775640722e+47 < c < 9.945393932890353e+36

    1. Initial program 18.5

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

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

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

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

      \[\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. Simplified10.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 associate-*r/10.9

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

      \[\leadsto \frac{\color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right)}\]
    11. Using strategy rm
    12. Applied div-sub10.8

      \[\leadsto \frac{\color{blue}{\frac{b \cdot c}{\mathsf{hypot}\left(c, d\right)} - \frac{a \cdot d}{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right)}\]
    13. Using strategy rm
    14. Applied associate-/l*1.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -4.2293107756407222 \cdot 10^{47} \lor \neg \left(c \le 9.94539393289035303 \cdot 10^{36}\right):\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, \frac{c}{\sqrt{\mathsf{hypot}\left(c, d\right)}}, -\frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{d}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \left(\left(-\frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right) + \frac{a}{\sqrt{\mathsf{hypot}\left(c, d\right)}}\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{b \cdot c}{\mathsf{hypot}\left(c, d\right)} - \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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