Average Error: 25.8 → 14.4
Time: 9.2s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -2.6657023148269232 \cdot 10^{+54}:\\ \;\;\;\;\frac{c \cdot b}{d \cdot d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -1.2031460365109471 \cdot 10^{-102}:\\ \;\;\;\;\frac{c \cdot b - d \cdot a}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 5.980746905707731 \cdot 10^{-154}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\ \mathbf{elif}\;d \leq 1.4163261492702185 \cdot 10^{+153}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;-\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 -2.6657023148269232 \cdot 10^{+54}:\\
\;\;\;\;\frac{c \cdot b}{d \cdot d} - \frac{a}{d}\\

\mathbf{elif}\;d \leq -1.2031460365109471 \cdot 10^{-102}:\\
\;\;\;\;\frac{c \cdot b - d \cdot a}{d \cdot d + c \cdot c}\\

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

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

\mathbf{else}:\\
\;\;\;\;-\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 -2.6657023148269232e+54)
   (- (/ (* c b) (* d d)) (/ a d))
   (if (<= d -1.2031460365109471e-102)
     (/ (- (* c b) (* d a)) (+ (* d d) (* c c)))
     (if (<= d 5.980746905707731e-154)
       (- (/ b c) (/ (* d a) (* c c)))
       (if (<= d 1.4163261492702185e+153)
         (/
          (/ (- (* c b) (* d a)) (sqrt (+ (* d d) (* c c))))
          (sqrt (+ (* d d) (* c 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 <= -2.6657023148269232e+54) {
		tmp = ((c * b) / (d * d)) - (a / d);
	} else if (d <= -1.2031460365109471e-102) {
		tmp = ((c * b) - (d * a)) / ((d * d) + (c * c));
	} else if (d <= 5.980746905707731e-154) {
		tmp = (b / c) - ((d * a) / (c * c));
	} else if (d <= 1.4163261492702185e+153) {
		tmp = (((c * b) - (d * a)) / sqrt((d * d) + (c * c))) / sqrt((d * d) + (c * c));
	} else {
		tmp = -(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

Original25.8
Target0.3
Herbie14.4
\[\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 d < -2.6657023148269232e54

    1. Initial program 36.9

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

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

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

    if -2.6657023148269232e54 < d < -1.2031460365109471e-102

    1. Initial program 14.5

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]

    if -1.2031460365109471e-102 < d < 5.9807469057077313e-154

    1. Initial program 23.2

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

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

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

    if 5.9807469057077313e-154 < d < 1.41632614927021847e153

    1. Initial program 17.2

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

      \[\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 1.41632614927021847e153 < d

    1. Initial program 43.0

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

      \[\leadsto \color{blue}{-1 \cdot \frac{a}{d}}\]
    3. Simplified12.8

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -2.6657023148269232 \cdot 10^{+54}:\\ \;\;\;\;\frac{c \cdot b}{d \cdot d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -1.2031460365109471 \cdot 10^{-102}:\\ \;\;\;\;\frac{c \cdot b - d \cdot a}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 5.980746905707731 \cdot 10^{-154}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\ \mathbf{elif}\;d \leq 1.4163261492702185 \cdot 10^{+153}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;-\frac{a}{d}\\ \end{array}\]

Reproduce

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