Average Error: 25.9 → 15.7
Time: 6.1s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -27.84020768422239:\\ \;\;\;\;\frac{b \cdot c}{{d}^{2}} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 1.0364315669180588 \cdot 10^{-150}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{{c}^{2}}\\ \mathbf{elif}\;d \leq 1.0308942826624653 \cdot 10^{+129}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \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 -27.84020768422239:\\
\;\;\;\;\frac{b \cdot c}{{d}^{2}} - \frac{a}{d}\\

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

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

\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 -27.84020768422239)
   (- (/ (* b c) (pow d 2.0)) (/ a d))
   (if (<= d 1.0364315669180588e-150)
     (- (/ b c) (/ (* d a) (pow c 2.0)))
     (if (<= d 1.0308942826624653e+129)
       (/
        (/ (- (* b c) (* d a)) (sqrt (+ (* c c) (* d d))))
        (sqrt (+ (* c c) (* d d))))
       (- (/ 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 <= -27.84020768422239) {
		tmp = ((b * c) / pow(d, 2.0)) - (a / d);
	} else if (d <= 1.0364315669180588e-150) {
		tmp = (b / c) - ((d * a) / pow(c, 2.0));
	} else if (d <= 1.0308942826624653e+129) {
		tmp = (((b * c) - (d * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} 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.9
Target0.4
Herbie15.7
\[\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 < -27.8402076842223885

    1. Initial program 32.8

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

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

    if -27.8402076842223885 < d < 1.0364315669180588e-150

    1. Initial program 20.0

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

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

    if 1.0364315669180588e-150 < d < 1.03089428266246529e129

    1. Initial program 17.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary6417.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*_binary6417.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. Simplified17.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}}\]

    if 1.03089428266246529e129 < d

    1. Initial program 41.0

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

      \[\leadsto \color{blue}{-1 \cdot \frac{a}{d}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification15.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -27.84020768422239:\\ \;\;\;\;\frac{b \cdot c}{{d}^{2}} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 1.0364315669180588 \cdot 10^{-150}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{{c}^{2}}\\ \mathbf{elif}\;d \leq 1.0308942826624653 \cdot 10^{+129}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;-\frac{a}{d}\\ \end{array}\]

Alternatives

Reproduce

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