Average Error: 26.4 → 15.4
Time: 9.7s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -3.0462786319571863 \cdot 10^{+102}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{elif}\;d \leq -5.2727617851885534 \cdot 10^{-170}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 7.6273034464728125 \cdot 10^{-298}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 9.667985544451415 \cdot 10^{-95}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{{c}^{2}}\\ \mathbf{elif}\;d \leq 7.690932894099785 \cdot 10^{+111}:\\ \;\;\;\;\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 -3.0462786319571863 \cdot 10^{+102}:\\
\;\;\;\;-\frac{a}{d}\\

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

\mathbf{elif}\;d \leq 7.6273034464728125 \cdot 10^{-298}:\\
\;\;\;\;\frac{b}{c}\\

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

\mathbf{elif}\;d \leq 7.690932894099785 \cdot 10^{+111}:\\
\;\;\;\;\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 -3.0462786319571863e+102)
   (- (/ a d))
   (if (<= d -5.2727617851885534e-170)
     (/
      (/ (- (* b c) (* d a)) (sqrt (+ (* c c) (* d d))))
      (sqrt (+ (* c c) (* d d))))
     (if (<= d 7.6273034464728125e-298)
       (/ b c)
       (if (<= d 9.667985544451415e-95)
         (- (/ b c) (/ (* d a) (pow c 2.0)))
         (if (<= d 7.690932894099785e+111)
           (/
            (/ (- (* 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 <= -3.0462786319571863e+102) {
		tmp = -(a / d);
	} else if (d <= -5.2727617851885534e-170) {
		tmp = (((b * c) - (d * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (d <= 7.6273034464728125e-298) {
		tmp = b / c;
	} else if (d <= 9.667985544451415e-95) {
		tmp = (b / c) - ((d * a) / pow(c, 2.0));
	} else if (d <= 7.690932894099785e+111) {
		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

Original26.4
Target0.5
Herbie15.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 4 regimes
  2. if d < -3.04627863195718632e102 or 7.6909328940997849e111 < d

    1. Initial program 40.6

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

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

    if -3.04627863195718632e102 < d < -5.27276178518855343e-170 or 9.66798554445141456e-95 < d < 7.6909328940997849e111

    1. Initial program 16.4

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

      \[\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_377316.3

      \[\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 -5.27276178518855343e-170 < d < 7.62730344647281249e-298

    1. Initial program 24.3

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

      \[\leadsto \color{blue}{\frac{b}{c}}\]

    if 7.62730344647281249e-298 < d < 9.66798554445141456e-95

    1. Initial program 21.8

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.0462786319571863 \cdot 10^{+102}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{elif}\;d \leq -5.2727617851885534 \cdot 10^{-170}:\\ \;\;\;\;\frac{\frac{b \cdot c - d \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 7.6273034464728125 \cdot 10^{-298}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 9.667985544451415 \cdot 10^{-95}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{{c}^{2}}\\ \mathbf{elif}\;d \leq 7.690932894099785 \cdot 10^{+111}:\\ \;\;\;\;\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}\]

Reproduce

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