Average Error: 26.8 → 6.9
Time: 13.0s
Precision: binary64
Cost: 33288
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{c}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{b}{\mathsf{hypot}\left(c, d\right)}\\ t_2 := \mathsf{fma}\left(t_0, t_1, \frac{-a}{d}\right)\\ \mathbf{if}\;d \leq -2.765891584532292 \cdot 10^{+76}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d \leq 1.3579662715200553 \cdot 10^{+98}:\\ \;\;\;\;\mathsf{fma}\left(t_0, t_1, \frac{d \cdot \left(-a\right)}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}\right)\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \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 (/ c (hypot c d)))
        (t_1 (/ b (hypot c d)))
        (t_2 (fma t_0 t_1 (/ (- a) d))))
   (if (<= d -2.765891584532292e+76)
     t_2
     (if (<= d 1.3579662715200553e+98)
       (fma t_0 t_1 (/ (* d (- a)) (pow (hypot c d) 2.0)))
       t_2))))
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 = c / hypot(c, d);
	double t_1 = b / hypot(c, d);
	double t_2 = fma(t_0, t_1, (-a / d));
	double tmp;
	if (d <= -2.765891584532292e+76) {
		tmp = t_2;
	} else if (d <= 1.3579662715200553e+98) {
		tmp = fma(t_0, t_1, ((d * -a) / pow(hypot(c, d), 2.0)));
	} else {
		tmp = t_2;
	}
	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(c / hypot(c, d))
	t_1 = Float64(b / hypot(c, d))
	t_2 = fma(t_0, t_1, Float64(Float64(-a) / d))
	tmp = 0.0
	if (d <= -2.765891584532292e+76)
		tmp = t_2;
	elseif (d <= 1.3579662715200553e+98)
		tmp = fma(t_0, t_1, Float64(Float64(d * Float64(-a)) / (hypot(c, d) ^ 2.0)));
	else
		tmp = t_2;
	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[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(b / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(t$95$0 * t$95$1 + N[((-a) / d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -2.765891584532292e+76], t$95$2, If[LessEqual[d, 1.3579662715200553e+98], N[(t$95$0 * t$95$1 + N[(N[(d * (-a)), $MachinePrecision] / N[Power[N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$2]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{c}{\mathsf{hypot}\left(c, d\right)}\\
t_1 := \frac{b}{\mathsf{hypot}\left(c, d\right)}\\
t_2 := \mathsf{fma}\left(t_0, t_1, \frac{-a}{d}\right)\\
\mathbf{if}\;d \leq -2.765891584532292 \cdot 10^{+76}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;d \leq 1.3579662715200553 \cdot 10^{+98}:\\
\;\;\;\;\mathsf{fma}\left(t_0, t_1, \frac{d \cdot \left(-a\right)}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}\right)\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}

Error

Target

Original26.8
Target0.6
Herbie6.9
\[\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 < -2.7658915845322919e76 or 1.3579662715200553e98 < d

    1. Initial program 39.2

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

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

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

    if -2.7658915845322919e76 < d < 1.3579662715200553e98

    1. Initial program 19.2

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -2.765891584532292 \cdot 10^{+76}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{-a}{d}\right)\\ \mathbf{elif}\;d \leq 1.3579662715200553 \cdot 10^{+98}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{d \cdot \left(-a\right)}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{-a}{d}\right)\\ \end{array} \]

Alternatives

Alternative 1
Error6.6
Cost22024
\[\begin{array}{l} t_0 := \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{-a}{d}\right)\\ t_1 := c \cdot b - d \cdot a\\ t_2 := \frac{t_1}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_2 \leq -\infty:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_2 \leq 5 \cdot 10^{+268}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{t_1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error10.3
Cost14288
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;d \leq -8.515586876420877 \cdot 10^{+72}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-128}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 10^{-210}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 1.3579662715200553 \cdot 10^{+98}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{b}{d}}{\frac{d}{c}} - \frac{a}{d}\\ \end{array} \]
Alternative 3
Error15.9
Cost1628
\[\begin{array}{l} t_0 := \frac{b \cdot \frac{c}{d} - a}{d}\\ t_1 := \frac{b - a \cdot \frac{d}{c}}{c}\\ t_2 := \frac{c \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -5.141091477822407 \cdot 10^{-17}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 2.0751177407278352 \cdot 10^{-64}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.5753156676145865 \cdot 10^{-40}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;c \leq 224611782627.78482:\\ \;\;\;\;\frac{\frac{b}{d}}{\frac{d}{c}} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.461643935036261 \cdot 10^{+86}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 5.231201427481236 \cdot 10^{+110}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.845256899382351 \cdot 10^{+130}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \end{array} \]
Alternative 4
Error16.0
Cost1628
\[\begin{array}{l} t_0 := \frac{b \cdot \frac{c}{d} - a}{d}\\ t_1 := \frac{b}{c} - d \cdot \frac{\frac{a}{c}}{c}\\ t_2 := \frac{c \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -5.141091477822407 \cdot 10^{-17}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq 2.0751177407278352 \cdot 10^{-64}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.5753156676145865 \cdot 10^{-40}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;c \leq 224611782627.78482:\\ \;\;\;\;\frac{\frac{b}{d}}{\frac{d}{c}} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.461643935036261 \cdot 10^{+86}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 5.231201427481236 \cdot 10^{+110}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 9.799499218022913 \cdot 10^{+128}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Error19.8
Cost1104
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ t_1 := \frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{if}\;c \leq -5.141091477822407 \cdot 10^{-17}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -5 \cdot 10^{-270}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -1.16 \cdot 10^{-293}:\\ \;\;\;\;\frac{b}{d \cdot \frac{d}{c}}\\ \mathbf{elif}\;c \leq 2.0751177407278352 \cdot 10^{-64}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error15.6
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -5.141091477822407 \cdot 10^{-17}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq 2.0751177407278352 \cdot 10^{-64}:\\ \;\;\;\;\frac{b \cdot \frac{c}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \end{array} \]
Alternative 7
Error24.2
Cost844
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;c \leq -5.141091477822407 \cdot 10^{-17}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -5 \cdot 10^{-270}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -1.16 \cdot 10^{-293}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d}}{d}\\ \mathbf{elif}\;c \leq 2.0751177407278352 \cdot 10^{-64}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 8
Error24.2
Cost844
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;c \leq -5.141091477822407 \cdot 10^{-17}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -5 \cdot 10^{-270}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -1.16 \cdot 10^{-293}:\\ \;\;\;\;\frac{b}{d \cdot \frac{d}{c}}\\ \mathbf{elif}\;c \leq 2.0751177407278352 \cdot 10^{-64}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 9
Error15.5
Cost840
\[\begin{array}{l} t_0 := \frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{if}\;c \leq -5.141091477822407 \cdot 10^{-17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.0751177407278352 \cdot 10^{-64}:\\ \;\;\;\;\frac{b \cdot \frac{c}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Error15.4
Cost840
\[\begin{array}{l} \mathbf{if}\;c \leq -5.141091477822407 \cdot 10^{-17}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq 2.0751177407278352 \cdot 10^{-64}:\\ \;\;\;\;\frac{b \cdot \frac{c}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \end{array} \]
Alternative 11
Error23.7
Cost520
\[\begin{array}{l} \mathbf{if}\;c \leq -5.141091477822407 \cdot 10^{-17}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 2.0751177407278352 \cdot 10^{-64}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 12
Error37.3
Cost192
\[\frac{b}{c} \]

Error

Reproduce

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