Average Error: 26.0 → 12.6
Time: 11.9s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.7774429101464625 \cdot 10^{124}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 5.3202544266061094 \cdot 10^{154}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, -a \cdot d\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 -2.7774429101464625 \cdot 10^{124}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \le 5.3202544266061094 \cdot 10^{154}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, -a \cdot d\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 r101385 = b;
        double r101386 = c;
        double r101387 = r101385 * r101386;
        double r101388 = a;
        double r101389 = d;
        double r101390 = r101388 * r101389;
        double r101391 = r101387 - r101390;
        double r101392 = r101386 * r101386;
        double r101393 = r101389 * r101389;
        double r101394 = r101392 + r101393;
        double r101395 = r101391 / r101394;
        return r101395;
}

double f(double a, double b, double c, double d) {
        double r101396 = c;
        double r101397 = -2.7774429101464625e+124;
        bool r101398 = r101396 <= r101397;
        double r101399 = b;
        double r101400 = -r101399;
        double r101401 = d;
        double r101402 = hypot(r101396, r101401);
        double r101403 = r101400 / r101402;
        double r101404 = 5.3202544266061094e+154;
        bool r101405 = r101396 <= r101404;
        double r101406 = a;
        double r101407 = r101406 * r101401;
        double r101408 = -r101407;
        double r101409 = fma(r101399, r101396, r101408);
        double r101410 = r101409 / r101402;
        double r101411 = r101410 / r101402;
        double r101412 = r101399 / r101402;
        double r101413 = r101405 ? r101411 : r101412;
        double r101414 = r101398 ? r101403 : r101413;
        return r101414;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.5
Herbie12.6
\[\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 < -2.7774429101464625e+124

    1. Initial program 41.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified41.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-sqrt41.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-identity41.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-frac41.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. Simplified41.0

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

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

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

      \[\leadsto \color{blue}{{\left(\frac{1}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}} \cdot {\left(\frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\]
    12. Applied pow-prod-down27.1

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

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

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

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

    if -2.7774429101464625e+124 < c < 5.3202544266061094e+154

    1. Initial program 18.9

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified18.9

      \[\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-sqrt18.9

      \[\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-identity18.9

      \[\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-frac18.9

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

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

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

      \[\leadsto \color{blue}{{\left(\frac{1}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}} \cdot {\left(\frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\]
    12. Applied pow-prod-down11.9

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

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

    if 5.3202544266061094e+154 < c

    1. Initial program 46.2

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{\left(\frac{1}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}} \cdot {\left(\frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\]
    12. Applied pow-prod-down28.4

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -2.7774429101464625 \cdot 10^{124}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 5.3202544266061094 \cdot 10^{154}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(b, c, -a \cdot d\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 2020045 +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))))