Average Error: 26.5 → 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 -5.457377104622505852467015767494503842035 \cdot 10^{208}:\\ \;\;\;\;{\left(\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \mathbf{elif}\;c \le 1.098050593933298912479119802869378133859 \cdot 10^{-16}:\\ \;\;\;\;{\left(\frac{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \mathbf{elif}\;c \le 1.368280955057290886281182593174139313509 \cdot 10^{154}:\\ \;\;\;\;\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{else}:\\ \;\;\;\;{\left(\frac{b}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -5.457377104622505852467015767494503842035 \cdot 10^{208}:\\
\;\;\;\;{\left(\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\

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

\mathbf{elif}\;c \le 1.368280955057290886281182593174139313509 \cdot 10^{154}:\\
\;\;\;\;\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{else}:\\
\;\;\;\;{\left(\frac{b}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r98323 = b;
        double r98324 = c;
        double r98325 = r98323 * r98324;
        double r98326 = a;
        double r98327 = d;
        double r98328 = r98326 * r98327;
        double r98329 = r98325 - r98328;
        double r98330 = r98324 * r98324;
        double r98331 = r98327 * r98327;
        double r98332 = r98330 + r98331;
        double r98333 = r98329 / r98332;
        return r98333;
}

double f(double a, double b, double c, double d) {
        double r98334 = c;
        double r98335 = -5.457377104622506e+208;
        bool r98336 = r98334 <= r98335;
        double r98337 = -1.0;
        double r98338 = b;
        double r98339 = r98337 * r98338;
        double r98340 = d;
        double r98341 = hypot(r98334, r98340);
        double r98342 = r98339 / r98341;
        double r98343 = 1.0;
        double r98344 = pow(r98342, r98343);
        double r98345 = 1.0980505939332989e-16;
        bool r98346 = r98334 <= r98345;
        double r98347 = r98338 * r98334;
        double r98348 = a;
        double r98349 = r98348 * r98340;
        double r98350 = r98347 - r98349;
        double r98351 = r98350 / r98341;
        double r98352 = r98351 / r98341;
        double r98353 = pow(r98352, r98343);
        double r98354 = 1.368280955057291e+154;
        bool r98355 = r98334 <= r98354;
        double r98356 = r98340 * r98340;
        double r98357 = fma(r98334, r98334, r98356);
        double r98358 = r98357 / r98334;
        double r98359 = r98338 / r98358;
        double r98360 = r98357 / r98340;
        double r98361 = r98348 / r98360;
        double r98362 = r98359 - r98361;
        double r98363 = r98338 / r98341;
        double r98364 = pow(r98363, r98343);
        double r98365 = r98355 ? r98362 : r98364;
        double r98366 = r98346 ? r98353 : r98365;
        double r98367 = r98336 ? r98344 : r98366;
        return r98367;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.5
Target0.5
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 < -5.457377104622506e+208

    1. Initial program 42.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt42.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-identity42.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-frac42.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. Simplified42.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. Simplified31.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 pow131.4

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

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

      \[\leadsto \color{blue}{{\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)}^{1}}\]
    12. Simplified31.3

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

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

    if -5.457377104622506e+208 < c < 1.0980505939332989e-16

    1. Initial program 22.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt22.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-identity22.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-frac22.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. Simplified22.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. Simplified13.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 pow113.6

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

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

      \[\leadsto \color{blue}{{\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)}^{1}}\]
    12. Simplified13.5

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

    if 1.0980505939332989e-16 < c < 1.368280955057291e+154

    1. Initial program 18.9

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

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

      \[\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.368280955057291e+154 < c

    1. Initial program 45.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt45.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-identity45.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-frac45.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. Simplified45.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. 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 pow127.9

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

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

      \[\leadsto \color{blue}{{\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)}^{1}}\]
    12. Simplified27.9

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -5.457377104622505852467015767494503842035 \cdot 10^{208}:\\ \;\;\;\;{\left(\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \mathbf{elif}\;c \le 1.098050593933298912479119802869378133859 \cdot 10^{-16}:\\ \;\;\;\;{\left(\frac{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \mathbf{elif}\;c \le 1.368280955057290886281182593174139313509 \cdot 10^{154}:\\ \;\;\;\;\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{else}:\\ \;\;\;\;{\left(\frac{b}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \end{array}\]

Reproduce

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