Average Error: 26.1 → 12.9
Time: 10.7s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -5.24214216730030625 \cdot 10^{138}:\\ \;\;\;\;\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{elif}\;c \le 5.31450108717881093 \cdot 10^{140}:\\ \;\;\;\;\frac{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -5.24214216730030625 \cdot 10^{138}:\\
\;\;\;\;\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r117042 = b;
        double r117043 = c;
        double r117044 = r117042 * r117043;
        double r117045 = a;
        double r117046 = d;
        double r117047 = r117045 * r117046;
        double r117048 = r117044 - r117047;
        double r117049 = r117043 * r117043;
        double r117050 = r117046 * r117046;
        double r117051 = r117049 + r117050;
        double r117052 = r117048 / r117051;
        return r117052;
}

double f(double a, double b, double c, double d) {
        double r117053 = c;
        double r117054 = -5.242142167300306e+138;
        bool r117055 = r117053 <= r117054;
        double r117056 = -1.0;
        double r117057 = b;
        double r117058 = r117056 * r117057;
        double r117059 = d;
        double r117060 = hypot(r117053, r117059);
        double r117061 = 1.0;
        double r117062 = r117060 * r117061;
        double r117063 = r117058 / r117062;
        double r117064 = 5.314501087178811e+140;
        bool r117065 = r117053 <= r117064;
        double r117066 = r117057 * r117053;
        double r117067 = a;
        double r117068 = r117067 * r117059;
        double r117069 = r117066 - r117068;
        double r117070 = r117069 / r117060;
        double r117071 = r117070 / r117062;
        double r117072 = r117057 / r117062;
        double r117073 = r117065 ? r117071 : r117072;
        double r117074 = r117055 ? r117063 : r117073;
        return r117074;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.1
Target0.4
Herbie12.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 3 regimes
  2. if c < -5.242142167300306e+138

    1. Initial program 43.7

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt43.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-identity43.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-frac43.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. Simplified43.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. Simplified28.3

      \[\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-*l/28.3

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

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

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

    if -5.242142167300306e+138 < c < 5.314501087178811e+140

    1. Initial program 19.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt19.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-identity19.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-frac19.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. Simplified19.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. Simplified12.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-*l/12.1

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

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

    if 5.314501087178811e+140 < c

    1. Initial program 42.9

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

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

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

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

      \[\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.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-*l/28.5

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -5.24214216730030625 \cdot 10^{138}:\\ \;\;\;\;\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{elif}\;c \le 5.31450108717881093 \cdot 10^{140}:\\ \;\;\;\;\frac{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \end{array}\]

Reproduce

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