Average Error: 26.6 → 7.4
Time: 6.4s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := d \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\right)\\ t_1 := \frac{b}{c} - t_0\\ \mathbf{if}\;c \leq -7.017566576847787 \cdot 10^{+131}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -2.9482117005194184 \cdot 10^{-127}:\\ \;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right) \cdot \frac{\mathsf{hypot}\left(c, d\right)}{b}} - t_0\\ \mathbf{elif}\;c \leq 5.71825753163984 \cdot 10^{-152}:\\ \;\;\;\;\mathsf{fma}\left(b, \frac{\frac{c}{d}}{d}, \frac{-a}{d}\right)\\ \mathbf{elif}\;c \leq 2.4464143950090505 \cdot 10^{+137}:\\ \;\;\;\;\frac{c}{\frac{\mathsf{fma}\left(c, c, d \cdot d\right)}{b}} - \frac{a \cdot \frac{d}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \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 (* d (* (/ 1.0 (hypot c d)) (/ a (hypot c d)))))
        (t_1 (- (/ b c) t_0)))
   (if (<= c -7.017566576847787e+131)
     t_1
     (if (<= c -2.9482117005194184e-127)
       (- (/ c (* (hypot c d) (/ (hypot c d) b))) t_0)
       (if (<= c 5.71825753163984e-152)
         (fma b (/ (/ c d) d) (/ (- a) d))
         (if (<= c 2.4464143950090505e+137)
           (-
            (/ c (/ (fma c c (* d d)) b))
            (/ (* a (/ d (hypot d c))) (hypot d c)))
           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 = d * ((1.0 / hypot(c, d)) * (a / hypot(c, d)));
	double t_1 = (b / c) - t_0;
	double tmp;
	if (c <= -7.017566576847787e+131) {
		tmp = t_1;
	} else if (c <= -2.9482117005194184e-127) {
		tmp = (c / (hypot(c, d) * (hypot(c, d) / b))) - t_0;
	} else if (c <= 5.71825753163984e-152) {
		tmp = fma(b, ((c / d) / d), (-a / d));
	} else if (c <= 2.4464143950090505e+137) {
		tmp = (c / (fma(c, c, (d * d)) / b)) - ((a * (d / hypot(d, c))) / hypot(d, c));
	} else {
		tmp = t_1;
	}
	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(d * Float64(Float64(1.0 / hypot(c, d)) * Float64(a / hypot(c, d))))
	t_1 = Float64(Float64(b / c) - t_0)
	tmp = 0.0
	if (c <= -7.017566576847787e+131)
		tmp = t_1;
	elseif (c <= -2.9482117005194184e-127)
		tmp = Float64(Float64(c / Float64(hypot(c, d) * Float64(hypot(c, d) / b))) - t_0);
	elseif (c <= 5.71825753163984e-152)
		tmp = fma(b, Float64(Float64(c / d) / d), Float64(Float64(-a) / d));
	elseif (c <= 2.4464143950090505e+137)
		tmp = Float64(Float64(c / Float64(fma(c, c, Float64(d * d)) / b)) - Float64(Float64(a * Float64(d / hypot(d, c))) / hypot(d, c)));
	else
		tmp = t_1;
	end
	return 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[(d * N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(a / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b / c), $MachinePrecision] - t$95$0), $MachinePrecision]}, If[LessEqual[c, -7.017566576847787e+131], t$95$1, If[LessEqual[c, -2.9482117005194184e-127], N[(N[(c / N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] * N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision], If[LessEqual[c, 5.71825753163984e-152], N[(b * N[(N[(c / d), $MachinePrecision] / d), $MachinePrecision] + N[((-a) / d), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.4464143950090505e+137], N[(N[(c / N[(N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision] - N[(N[(a * N[(d / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$1]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := d \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\right)\\
t_1 := \frac{b}{c} - t_0\\
\mathbf{if}\;c \leq -7.017566576847787 \cdot 10^{+131}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;c \leq -2.9482117005194184 \cdot 10^{-127}:\\
\;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right) \cdot \frac{\mathsf{hypot}\left(c, d\right)}{b}} - t_0\\

\mathbf{elif}\;c \leq 5.71825753163984 \cdot 10^{-152}:\\
\;\;\;\;\mathsf{fma}\left(b, \frac{\frac{c}{d}}{d}, \frac{-a}{d}\right)\\

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

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.6
Target0.4
Herbie7.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 c < -7.0175665768477871e131 or 2.44641439500905046e137 < c

    1. Initial program 43.6

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(d, -a, b \cdot c\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Taylor expanded in a around 0 43.6

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

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

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

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

    if -7.0175665768477871e131 < c < -2.94821170051941835e-127

    1. Initial program 16.3

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(d, -a, b \cdot c\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Taylor expanded in a around 0 16.3

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

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

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

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

    if -2.94821170051941835e-127 < c < 5.71825753163984014e-152

    1. Initial program 24.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified24.0

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(d, -a, b \cdot c\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Taylor expanded in d around inf 10.5

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(b, \frac{\frac{c}{d}}{d}, \frac{-a}{d}\right)} \]

    if 5.71825753163984014e-152 < c < 2.44641439500905046e137

    1. Initial program 17.3

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(d, -a, b \cdot c\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Taylor expanded in a around 0 17.3

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -7.017566576847787 \cdot 10^{+131}:\\ \;\;\;\;\frac{b}{c} - d \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\right)\\ \mathbf{elif}\;c \leq -2.9482117005194184 \cdot 10^{-127}:\\ \;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right) \cdot \frac{\mathsf{hypot}\left(c, d\right)}{b}} - d \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\right)\\ \mathbf{elif}\;c \leq 5.71825753163984 \cdot 10^{-152}:\\ \;\;\;\;\mathsf{fma}\left(b, \frac{\frac{c}{d}}{d}, \frac{-a}{d}\right)\\ \mathbf{elif}\;c \leq 2.4464143950090505 \cdot 10^{+137}:\\ \;\;\;\;\frac{c}{\frac{\mathsf{fma}\left(c, c, d \cdot d\right)}{b}} - \frac{a \cdot \frac{d}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - d \cdot \left(\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\right)\\ \end{array} \]

Reproduce

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