Average Error: 26.2 → 14.6
Time: 10.9s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \leq -5.716459959379754 \cdot 10^{+151}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -4.319804439634208 \cdot 10^{+68}:\\ \;\;\;\;\frac{\frac{d \cdot a}{c} - b}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq -1.4292144434333795 \cdot 10^{-145}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 3.49936390175958 \cdot 10^{-78}:\\ \;\;\;\;\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.596706401112998 \cdot 10^{+146}:\\ \;\;\;\;\frac{c \cdot b}{{d}^{2} + {c}^{2}} - \frac{d \cdot a}{{d}^{2} + {c}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \leq -5.716459959379754 \cdot 10^{+151}:\\
\;\;\;\;\frac{b}{c}\\

\mathbf{elif}\;c \leq -4.319804439634208 \cdot 10^{+68}:\\
\;\;\;\;\frac{\frac{d \cdot a}{c} - b}{\sqrt{c \cdot c + d \cdot d}}\\

\mathbf{elif}\;c \leq -1.4292144434333795 \cdot 10^{-145}:\\
\;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\

\mathbf{elif}\;c \leq 3.49936390175958 \cdot 10^{-78}:\\
\;\;\;\;\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}\\

\mathbf{elif}\;c \leq 1.596706401112998 \cdot 10^{+146}:\\
\;\;\;\;\frac{c \cdot b}{{d}^{2} + {c}^{2}} - \frac{d \cdot a}{{d}^{2} + {c}^{2}}\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{c}\\

\end{array}
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (if (<= c -5.716459959379754e+151)
   (/ b c)
   (if (<= c -4.319804439634208e+68)
     (/ (- (/ (* d a) c) b) (sqrt (+ (* c c) (* d d))))
     (if (<= c -1.4292144434333795e-145)
       (/
        (/ (- (* c b) (* d a)) (sqrt (+ (* c c) (* d d))))
        (sqrt (+ (* c c) (* d d))))
       (if (<= c 3.49936390175958e-78)
         (- (/ (* c b) (pow d 2.0)) (/ a d))
         (if (<= c 1.596706401112998e+146)
           (-
            (/ (* c b) (+ (pow d 2.0) (pow c 2.0)))
            (/ (* d a) (+ (pow d 2.0) (pow c 2.0))))
           (/ b c)))))))
double code(double a, double b, double c, double d) {
	return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	double tmp;
	if (c <= -5.716459959379754e+151) {
		tmp = b / c;
	} else if (c <= -4.319804439634208e+68) {
		tmp = (((d * a) / c) - b) / sqrt((c * c) + (d * d));
	} else if (c <= -1.4292144434333795e-145) {
		tmp = (((c * b) - (d * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (c <= 3.49936390175958e-78) {
		tmp = ((c * b) / pow(d, 2.0)) - (a / d);
	} else if (c <= 1.596706401112998e+146) {
		tmp = ((c * b) / (pow(d, 2.0) + pow(c, 2.0))) - ((d * a) / (pow(d, 2.0) + pow(c, 2.0)));
	} else {
		tmp = b / c;
	}
	return tmp;
}

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.2
Target0.5
Herbie14.6
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \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 5 regimes
  2. if c < -5.71645995937975394e151 or 1.596706401112998e146 < c

    1. Initial program 44.5

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Taylor expanded around inf 14.9

      \[\leadsto \color{blue}{\frac{b}{c}}\]

    if -5.71645995937975394e151 < c < -4.3198044396342084e68

    1. Initial program 24.1

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

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Simplified24.0

      \[\leadsto \frac{\color{blue}{\frac{c \cdot b - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}}{\sqrt{c \cdot c + d \cdot d}}\]
    6. Taylor expanded around -inf 17.8

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

    if -4.3198044396342084e68 < c < -1.4292144434333795e-145

    1. Initial program 15.6

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

      \[\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 associate-/r*_binary64_343215.5

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Simplified15.5

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

    if -1.4292144434333795e-145 < c < 3.4993639017595799e-78

    1. Initial program 22.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Taylor expanded around 0 11.4

      \[\leadsto \color{blue}{\frac{b \cdot c}{{d}^{2}} - \frac{a}{d}}\]

    if 3.4993639017595799e-78 < c < 1.596706401112998e146

    1. Initial program 17.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Taylor expanded around 0 17.1

      \[\leadsto \color{blue}{\frac{b \cdot c}{{c}^{2} + {d}^{2}} - \frac{d \cdot a}{{c}^{2} + {d}^{2}}}\]
  3. Recombined 5 regimes into one program.
  4. Final simplification14.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -5.716459959379754 \cdot 10^{+151}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -4.319804439634208 \cdot 10^{+68}:\\ \;\;\;\;\frac{\frac{d \cdot a}{c} - b}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq -1.4292144434333795 \cdot 10^{-145}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 3.49936390175958 \cdot 10^{-78}:\\ \;\;\;\;\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.596706401112998 \cdot 10^{+146}:\\ \;\;\;\;\frac{c \cdot b}{{d}^{2} + {c}^{2}} - \frac{d \cdot a}{{d}^{2} + {c}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array}\]

Reproduce

herbie shell --seed 2021045 
(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))))