Average Error: 26.3 → 1.1
Time: 6.8s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{-1}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := a \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\\ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, t_1 \cdot t_0\right) + \mathsf{fma}\left(t_0, t_1, \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot t_1\right) \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 (/ -1.0 (hypot c d))) (t_1 (* a (/ d (hypot c d)))))
   (+
    (fma (/ c (hypot c d)) (/ b (hypot c d)) (* t_1 t_0))
    (fma t_0 t_1 (* (/ 1.0 (hypot c d)) t_1)))))
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 = -1.0 / hypot(c, d);
	double t_1 = a * (d / hypot(c, d));
	return fma((c / hypot(c, d)), (b / hypot(c, d)), (t_1 * t_0)) + fma(t_0, t_1, ((1.0 / hypot(c, d)) * t_1));
}
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(-1.0 / hypot(c, d))
	t_1 = Float64(a * Float64(d / hypot(c, d)))
	return Float64(fma(Float64(c / hypot(c, d)), Float64(b / hypot(c, d)), Float64(t_1 * t_0)) + fma(t_0, t_1, Float64(Float64(1.0 / hypot(c, d)) * t_1)))
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[(-1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(a * N[(d / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(b / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision] + N[(t$95$0 * t$95$1 + N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{-1}{\mathsf{hypot}\left(c, d\right)}\\
t_1 := a \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\\
\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, t_1 \cdot t_0\right) + \mathsf{fma}\left(t_0, t_1, \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot t_1\right)
\end{array}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.3
Target0.5
Herbie1.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. Initial program 26.3

    \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
  2. Applied egg-rr15.6

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

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\right)\right) + \mathsf{fma}\left(-\frac{1}{\mathsf{hypot}\left(c, d\right)}, a \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}, \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\right)\right)} \]
  5. Final simplification1.1

    \[\leadsto \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \left(a \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\right) \cdot \frac{-1}{\mathsf{hypot}\left(c, d\right)}\right) + \mathsf{fma}\left(\frac{-1}{\mathsf{hypot}\left(c, d\right)}, a \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}, \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\right)\right) \]

Reproduce

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