Average Error: 26.2 → 14.8
Time: 10.1s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -3.2283116114628037 \cdot 10^{+100}:\\ \;\;\;\;\frac{c \cdot b}{d \cdot d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -2.2844571867045313 \cdot 10^{-93}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \leq 9.170406126705686 \cdot 10^{-109}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\ \mathbf{elif}\;d \leq 3.6607070884127416 \cdot 10^{+145}:\\ \;\;\;\;\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 -3.2283116114628037 \cdot 10^{+100}:\\
\;\;\;\;\frac{c \cdot b}{d \cdot d} - \frac{a}{d}\\

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

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

\mathbf{elif}\;d \leq 3.6607070884127416 \cdot 10^{+145}:\\
\;\;\;\;\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 -3.2283116114628037e+100)
   (- (/ (* c b) (* d d)) (/ a d))
   (if (<= d -2.2844571867045313e-93)
     (/
      (/ (- (* c b) (* d a)) (sqrt (+ (* d d) (* c c))))
      (sqrt (+ (* d d) (* c c))))
     (if (<= d 9.170406126705686e-109)
       (- (/ b c) (/ (* d a) (* c c)))
       (if (<= d 3.6607070884127416e+145)
         (/
          (/ (- (* 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 <= -3.2283116114628037e+100) {
		tmp = ((c * b) / (d * d)) - (a / d);
	} else if (d <= -2.2844571867045313e-93) {
		tmp = (((c * b) - (d * a)) / sqrt((d * d) + (c * c))) / sqrt((d * d) + (c * c));
	} else if (d <= 9.170406126705686e-109) {
		tmp = (b / c) - ((d * a) / (c * c));
	} else if (d <= 3.6607070884127416e+145) {
		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

Original26.2
Target0.4
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 4 regimes
  2. if d < -3.22831161146280375e100

    1. Initial program 39.8

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

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

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

    if -3.22831161146280375e100 < d < -2.28445718670453135e-93 or 9.17040612670568637e-109 < d < 3.6607070884127416e145

    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_78217.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_70417.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}}}\]
    5. Simplified17.1

      \[\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 -2.28445718670453135e-93 < d < 9.17040612670568637e-109

    1. Initial program 20.9

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

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

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

    if 3.6607070884127416e145 < d

    1. Initial program 44.0

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.2283116114628037 \cdot 10^{+100}:\\ \;\;\;\;\frac{c \cdot b}{d \cdot d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -2.2844571867045313 \cdot 10^{-93}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \leq 9.170406126705686 \cdot 10^{-109}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\ \mathbf{elif}\;d \leq 3.6607070884127416 \cdot 10^{+145}:\\ \;\;\;\;\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 2021043 
(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))))