Average Error: 26.2 → 14.8
Time: 11.9s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -3.2563987087522574 \cdot 10^{+81}:\\ \;\;\;\;\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -8.232708701844971 \cdot 10^{-48}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \leq 1.8192329406445362 \cdot 10^{-149}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\ \mathbf{elif}\;d \leq 2.0139063475555305 \cdot 10^{+69}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}\\ \end{array}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -3.2563987087522574 \cdot 10^{+81}:\\
\;\;\;\;\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}\\

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

\mathbf{elif}\;d \leq 1.8192329406445362 \cdot 10^{-149}:\\
\;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}\\

\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 (<= d -3.2563987087522574e+81)
   (- (/ b (/ (* d d) c)) (/ a d))
   (if (<= d -8.232708701844971e-48)
     (/
      (/ (- (* b c) (* d a)) (sqrt (+ (* d d) (* c c))))
      (sqrt (+ (* d d) (* c c))))
     (if (<= d 1.8192329406445362e-149)
       (- (/ b c) (/ (* d a) (* c c)))
       (if (<= d 2.0139063475555305e+69)
         (/
          (/ (- (* b c) (* d a)) (sqrt (+ (* d d) (* c c))))
          (sqrt (+ (* d d) (* c c))))
         (- (/ b (/ (* d d) c)) (/ a d)))))))
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 (d <= -3.2563987087522574e+81) {
		tmp = (b / ((d * d) / c)) - (a / d);
	} else if (d <= -8.232708701844971e-48) {
		tmp = (((b * c) - (d * a)) / sqrt((d * d) + (c * c))) / sqrt((d * d) + (c * c));
	} else if (d <= 1.8192329406445362e-149) {
		tmp = (b / c) - ((d * a) / (c * c));
	} else if (d <= 2.0139063475555305e+69) {
		tmp = (((b * c) - (d * a)) / sqrt((d * d) + (c * c))) / sqrt((d * d) + (c * c));
	} else {
		tmp = (b / ((d * d) / c)) - (a / d);
	}
	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.8
\[\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 3 regimes
  2. if d < -3.256398708752257e81 or 2.01390634755553049e69 < d

    1. Initial program 37.9

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary64_487437.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-identity_binary64_485237.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-frac_binary64_485837.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. Simplified37.9

      \[\leadsto \frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \color{blue}{\frac{c \cdot b - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}\]
    7. Taylor expanded around 0 17.5

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

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

    if -3.256398708752257e81 < d < -8.23270870184497108e-48 or 1.8192329406445362e-149 < d < 2.01390634755553049e69

    1. Initial program 14.7

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

      \[\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}}}\]

    if -8.23270870184497108e-48 < d < 1.8192329406445362e-149

    1. Initial program 21.8

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.2563987087522574 \cdot 10^{+81}:\\ \;\;\;\;\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -8.232708701844971 \cdot 10^{-48}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \leq 1.8192329406445362 \cdot 10^{-149}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\ \mathbf{elif}\;d \leq 2.0139063475555305 \cdot 10^{+69}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}\\ \end{array}\]

Reproduce

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