Average Error: 25.6 → 6.2
Time: 5.2s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := b \cdot \frac{\frac{c}{d}}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -1.692033155006485 \cdot 10^{+165}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.0364479188939085 \cdot 10^{+154}:\\ \;\;\;\;b \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c}{\mathsf{hypot}\left(c, d\right)}\right) - \frac{\frac{d \cdot a}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (- (* b (/ (/ c d) d)) (/ a d))))
   (if (<= d -1.692033155006485e+165)
     t_0
     (if (<= d 1.0364479188939085e+154)
       (-
        (* b (* (/ 1.0 (hypot c d)) (/ c (hypot c d))))
        (/ (/ (* d a) (hypot c d)) (hypot c d)))
       t_0))))
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 t_0 = (b * ((c / d) / d)) - (a / d);
	double tmp;
	if (d <= -1.692033155006485e+165) {
		tmp = t_0;
	} else if (d <= 1.0364479188939085e+154) {
		tmp = (b * ((1.0 / hypot(c, d)) * (c / hypot(c, d)))) - (((d * a) / hypot(c, d)) / hypot(c, d));
	} else {
		tmp = t_0;
	}
	return tmp;
}
public static double code(double a, double b, double c, double d) {
	return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
	double t_0 = (b * ((c / d) / d)) - (a / d);
	double tmp;
	if (d <= -1.692033155006485e+165) {
		tmp = t_0;
	} else if (d <= 1.0364479188939085e+154) {
		tmp = (b * ((1.0 / Math.hypot(c, d)) * (c / Math.hypot(c, d)))) - (((d * a) / Math.hypot(c, d)) / Math.hypot(c, d));
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(a, b, c, d):
	return ((b * c) - (a * d)) / ((c * c) + (d * d))
def code(a, b, c, d):
	t_0 = (b * ((c / d) / d)) - (a / d)
	tmp = 0
	if d <= -1.692033155006485e+165:
		tmp = t_0
	elif d <= 1.0364479188939085e+154:
		tmp = (b * ((1.0 / math.hypot(c, d)) * (c / math.hypot(c, d)))) - (((d * a) / math.hypot(c, d)) / math.hypot(c, d))
	else:
		tmp = t_0
	return tmp
function code(a, b, c, d)
	return Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d)))
end
function code(a, b, c, d)
	t_0 = Float64(Float64(b * Float64(Float64(c / d) / d)) - Float64(a / d))
	tmp = 0.0
	if (d <= -1.692033155006485e+165)
		tmp = t_0;
	elseif (d <= 1.0364479188939085e+154)
		tmp = Float64(Float64(b * Float64(Float64(1.0 / hypot(c, d)) * Float64(c / hypot(c, d)))) - Float64(Float64(Float64(d * a) / hypot(c, d)) / hypot(c, d)));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp = code(a, b, c, d)
	tmp = ((b * c) - (a * d)) / ((c * c) + (d * d));
end
function tmp_2 = code(a, b, c, d)
	t_0 = (b * ((c / d) / d)) - (a / d);
	tmp = 0.0;
	if (d <= -1.692033155006485e+165)
		tmp = t_0;
	elseif (d <= 1.0364479188939085e+154)
		tmp = (b * ((1.0 / hypot(c, d)) * (c / hypot(c, d)))) - (((d * a) / hypot(c, d)) / hypot(c, d));
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_, d_] := N[(N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(b * N[(N[(c / d), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] - N[(a / d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -1.692033155006485e+165], t$95$0, If[LessEqual[d, 1.0364479188939085e+154], N[(N[(b * N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(N[(N[(d * a), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := b \cdot \frac{\frac{c}{d}}{d} - \frac{a}{d}\\
\mathbf{if}\;d \leq -1.692033155006485 \cdot 10^{+165}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d \leq 1.0364479188939085 \cdot 10^{+154}:\\
\;\;\;\;b \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c}{\mathsf{hypot}\left(c, d\right)}\right) - \frac{\frac{d \cdot a}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

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.6
Target0.5
Herbie6.2
\[\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 2 regimes
  2. if d < -1.69203315500648505e165 or 1.0364479188939085e154 < d

    1. Initial program 44.3

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified44.3

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(d, -a, b \cdot c\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Applied egg-rr44.3

      \[\leadsto \color{blue}{\left(d \cdot a - b \cdot c\right) \cdot \frac{1}{-{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}}} \]
    4. Taylor expanded in d around inf 15.6

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

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

    if -1.69203315500648505e165 < d < 1.0364479188939085e154

    1. Initial program 19.3

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified19.3

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(d, -a, b \cdot c\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Applied egg-rr19.5

      \[\leadsto \color{blue}{\left(d \cdot a - b \cdot c\right) \cdot \frac{1}{-{\left(\mathsf{hypot}\left(d, c\right)\right)}^{2}}} \]
    4. Taylor expanded in a around 0 19.3

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

      \[\leadsto \color{blue}{b \cdot \frac{c}{\mathsf{fma}\left(c, c, d \cdot d\right)} - d \cdot \frac{a}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    6. Applied egg-rr15.9

      \[\leadsto b \cdot \frac{c}{\mathsf{fma}\left(c, c, d \cdot d\right)} - \color{blue}{\frac{\frac{d \cdot a}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}} \]
    7. Applied egg-rr4.4

      \[\leadsto b \cdot \color{blue}{\left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c}{\mathsf{hypot}\left(c, d\right)}\right)} - \frac{\frac{d \cdot a}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification6.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.692033155006485 \cdot 10^{+165}:\\ \;\;\;\;b \cdot \frac{\frac{c}{d}}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 1.0364479188939085 \cdot 10^{+154}:\\ \;\;\;\;b \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c}{\mathsf{hypot}\left(c, d\right)}\right) - \frac{\frac{d \cdot a}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;b \cdot \frac{\frac{c}{d}}{d} - \frac{a}{d}\\ \end{array} \]

Reproduce

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