Average Error: 26.0 → 13.1
Time: 6.6s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -1.6322724406596933 \cdot 10^{+154}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{elif}\;d \leq -1.548666922304495 \cdot 10^{-104}:\\ \;\;\;\;\frac{c \cdot b}{c \cdot c + d \cdot d} - \frac{a \cdot \frac{d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 2.4346042928963056 \cdot 10^{-77}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 5.927595764345909 \cdot 10^{+123}:\\ \;\;\;\;\frac{c}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}} - \frac{d \cdot a}{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 -1.6322724406596933 \cdot 10^{+154}:\\
\;\;\;\;-\frac{a}{d}\\

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

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

\mathbf{elif}\;d \leq 5.927595764345909 \cdot 10^{+123}:\\
\;\;\;\;\frac{c}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}} - \frac{d \cdot a}{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 -1.6322724406596933e+154)
   (- (/ a d))
   (if (<= d -1.548666922304495e-104)
     (-
      (/ (* c b) (+ (* c c) (* d d)))
      (/ (* a (/ d (sqrt (+ (* c c) (* d d))))) (sqrt (+ (* c c) (* d d)))))
     (if (<= d 2.4346042928963056e-77)
       (- (/ b c) (/ (* d a) (+ (* c c) (* d d))))
       (if (<= d 5.927595764345909e+123)
         (-
          (* (/ c (sqrt (+ (* c c) (* d d)))) (/ b (sqrt (+ (* c c) (* d d)))))
          (/ (* d a) (+ (* 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 <= -1.6322724406596933e+154) {
		tmp = -(a / d);
	} else if (d <= -1.548666922304495e-104) {
		tmp = ((c * b) / ((c * c) + (d * d))) - ((a * (d / sqrt((c * c) + (d * d)))) / sqrt((c * c) + (d * d)));
	} else if (d <= 2.4346042928963056e-77) {
		tmp = (b / c) - ((d * a) / ((c * c) + (d * d)));
	} else if (d <= 5.927595764345909e+123) {
		tmp = ((c / sqrt((c * c) + (d * d))) * (b / sqrt((c * c) + (d * d)))) - ((d * a) / ((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

Original26.0
Target0.4
Herbie13.1
\[\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 < -1.6322724406596933e154 or 5.9275957643459093e123 < d

    1. Initial program 43.5

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

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

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

    if -1.6322724406596933e154 < d < -1.5486669223044951e-104

    1. Initial program 16.9

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied div-sub_binary6416.9

      \[\leadsto \color{blue}{\frac{b \cdot c}{c \cdot c + d \cdot d} - \frac{a \cdot d}{c \cdot c + d \cdot d}}\]
    4. Simplified16.9

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

      \[\leadsto \frac{c \cdot b}{c \cdot c + d \cdot d} - \color{blue}{\frac{d \cdot a}{c \cdot c + d \cdot d}}\]
    6. Using strategy rm
    7. Applied add-sqr-sqrt_binary6416.9

      \[\leadsto \frac{c \cdot b}{c \cdot c + d \cdot d} - \frac{d \cdot a}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    8. Applied associate-/r*_binary6416.8

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

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

    if -1.5486669223044951e-104 < d < 2.43460429289630561e-77

    1. Initial program 21.2

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied div-sub_binary6421.2

      \[\leadsto \color{blue}{\frac{b \cdot c}{c \cdot c + d \cdot d} - \frac{a \cdot d}{c \cdot c + d \cdot d}}\]
    4. Simplified21.2

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

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

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

    if 2.43460429289630561e-77 < d < 5.9275957643459093e123

    1. Initial program 16.5

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied div-sub_binary6416.5

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

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

      \[\leadsto \frac{c \cdot b}{c \cdot c + d \cdot d} - \color{blue}{\frac{d \cdot a}{c \cdot c + d \cdot d}}\]
    6. Using strategy rm
    7. Applied add-sqr-sqrt_binary6416.5

      \[\leadsto \frac{c \cdot b}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} - \frac{d \cdot a}{c \cdot c + d \cdot d}\]
    8. Applied times-frac_binary6415.7

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.6322724406596933 \cdot 10^{+154}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{elif}\;d \leq -1.548666922304495 \cdot 10^{-104}:\\ \;\;\;\;\frac{c \cdot b}{c \cdot c + d \cdot d} - \frac{a \cdot \frac{d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 2.4346042928963056 \cdot 10^{-77}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 5.927595764345909 \cdot 10^{+123}:\\ \;\;\;\;\frac{c}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}} - \frac{d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;-\frac{a}{d}\\ \end{array}\]

Alternatives

Reproduce

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