Average Error: 25.7 → 12.5
Time: 10.0s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -5.9876509466672635 \cdot 10^{162}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)} \cdot \sqrt{1}\\ \mathbf{elif}\;c \le 2.5019882417216274 \cdot 10^{136}:\\ \;\;\;\;\sqrt{1} \cdot \frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, c, -d \cdot a\right)}}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{1} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -5.9876509466672635 \cdot 10^{162}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)} \cdot \sqrt{1}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r97236 = b;
        double r97237 = c;
        double r97238 = r97236 * r97237;
        double r97239 = a;
        double r97240 = d;
        double r97241 = r97239 * r97240;
        double r97242 = r97238 - r97241;
        double r97243 = r97237 * r97237;
        double r97244 = r97240 * r97240;
        double r97245 = r97243 + r97244;
        double r97246 = r97242 / r97245;
        return r97246;
}

double f(double a, double b, double c, double d) {
        double r97247 = c;
        double r97248 = -5.9876509466672635e+162;
        bool r97249 = r97247 <= r97248;
        double r97250 = b;
        double r97251 = -r97250;
        double r97252 = d;
        double r97253 = hypot(r97252, r97247);
        double r97254 = r97251 / r97253;
        double r97255 = 1.0;
        double r97256 = sqrt(r97255);
        double r97257 = r97254 * r97256;
        double r97258 = 2.5019882417216274e+136;
        bool r97259 = r97247 <= r97258;
        double r97260 = a;
        double r97261 = r97252 * r97260;
        double r97262 = -r97261;
        double r97263 = fma(r97250, r97247, r97262);
        double r97264 = r97253 / r97263;
        double r97265 = r97255 / r97264;
        double r97266 = r97265 / r97253;
        double r97267 = r97256 * r97266;
        double r97268 = r97250 / r97253;
        double r97269 = r97256 * r97268;
        double r97270 = r97259 ? r97267 : r97269;
        double r97271 = r97249 ? r97257 : r97270;
        return r97271;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.7
Target0.5
Herbie12.5
\[\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 < -5.9876509466672635e+162

    1. Initial program 43.8

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

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

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

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

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

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

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

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(d, c\right)}} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}\]
    11. Applied add-sqr-sqrt29.5

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

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

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

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

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

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

    if -5.9876509466672635e+162 < c < 2.5019882417216274e+136

    1. Initial program 19.2

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified19.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-sqrt19.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-identity19.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-frac19.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. Simplified19.2

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

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

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

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{1 \cdot \mathsf{hypot}\left(d, c\right)} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}\]
    12. Applied times-frac12.1

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

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

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

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

    if 2.5019882417216274e+136 < c

    1. Initial program 42.2

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified42.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-sqrt42.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-identity42.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-frac42.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. Simplified42.2

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

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

      \[\leadsto \frac{1}{\color{blue}{1 \cdot \mathsf{hypot}\left(d, c\right)}} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}\]
    11. Applied add-sqr-sqrt27.3

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{1 \cdot \mathsf{hypot}\left(d, c\right)} \cdot \frac{\mathsf{fma}\left(b, c, -d \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}\]
    12. Applied times-frac27.3

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -5.9876509466672635 \cdot 10^{162}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)} \cdot \sqrt{1}\\ \mathbf{elif}\;c \le 2.5019882417216274 \cdot 10^{136}:\\ \;\;\;\;\sqrt{1} \cdot \frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\mathsf{fma}\left(b, c, -d \cdot a\right)}}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{1} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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