Average Error: 26.4 → 12.9
Time: 4.7s
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -5.1227572413769072 \cdot 10^{95}:\\ \;\;\;\;\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 6.69895789485502902 \cdot 10^{-106}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{b \cdot c - a \cdot d}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 2.54946443013026016 \cdot 10^{79}:\\ \;\;\;\;\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{elif}\;c \le 4.6230231189603044 \cdot 10^{141}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{b \cdot c - a \cdot d}}}{\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 -5.1227572413769072 \cdot 10^{95}:\\
\;\;\;\;\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\

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

\mathbf{elif}\;c \le 2.54946443013026016 \cdot 10^{79}:\\
\;\;\;\;\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{elif}\;c \le 4.6230231189603044 \cdot 10^{141}:\\
\;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{b \cdot c - a \cdot d}}}{\mathsf{hypot}\left(c, d\right)}\\

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

\end{array}
double code(double a, double b, double c, double d) {
	return ((double) (((double) (((double) (b * c)) - ((double) (a * d)))) / ((double) (((double) (c * c)) + ((double) (d * d))))));
}
double code(double a, double b, double c, double d) {
	double VAR;
	if ((c <= -5.122757241376907e+95)) {
		VAR = ((double) (((double) (-1.0 * b)) / ((double) hypot(c, d))));
	} else {
		double VAR_1;
		if ((c <= 6.698957894855029e-106)) {
			VAR_1 = ((double) (((double) (1.0 / ((double) (((double) hypot(c, d)) / ((double) (((double) (b * c)) - ((double) (a * d)))))))) / ((double) hypot(c, d))));
		} else {
			double VAR_2;
			if ((c <= 2.54946443013026e+79)) {
				VAR_2 = ((double) (((double) (b / ((double) (((double) fma(c, c, ((double) (d * d)))) / c)))) - ((double) (a / ((double) (((double) fma(c, c, ((double) (d * d)))) / d))))));
			} else {
				double VAR_3;
				if ((c <= 4.6230231189603044e+141)) {
					VAR_3 = ((double) (((double) (1.0 / ((double) (((double) hypot(c, d)) / ((double) (((double) (b * c)) - ((double) (a * d)))))))) / ((double) hypot(c, d))));
				} else {
					VAR_3 = ((double) (b / ((double) hypot(c, d))));
				}
				VAR_2 = VAR_3;
			}
			VAR_1 = VAR_2;
		}
		VAR = VAR_1;
	}
	return VAR;
}

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.4
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 4 regimes
  2. if c < -5.122757241376907e+95

    1. Initial program 39.9

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt39.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-identity39.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-frac39.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. Simplified39.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. Simplified27.7

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

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

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

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

    if -5.122757241376907e+95 < c < 6.698957894855029e-106 or 2.54946443013026e+79 < c < 4.6230231189603044e+141

    1. Initial program 20.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt20.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-identity20.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-frac20.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. Simplified20.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.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 associate-*r/12.3

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

      \[\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 clear-num12.3

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

    if 6.698957894855029e-106 < c < 2.54946443013026e+79

    1. Initial program 15.7

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

      \[\leadsto \color{blue}{\frac{b \cdot c}{c \cdot c + d \cdot d} - \frac{a \cdot d}{c \cdot c + d \cdot d}}\]
    4. Simplified14.4

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

      \[\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 4.6230231189603044e+141 < c

    1. Initial program 44.2

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

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

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

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

      \[\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-*r/28.2

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -5.1227572413769072 \cdot 10^{95}:\\ \;\;\;\;\frac{-1 \cdot b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 6.69895789485502902 \cdot 10^{-106}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{b \cdot c - a \cdot d}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \le 2.54946443013026016 \cdot 10^{79}:\\ \;\;\;\;\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{elif}\;c \le 4.6230231189603044 \cdot 10^{141}:\\ \;\;\;\;\frac{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{b \cdot c - a \cdot d}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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