Average Error: 26.1 → 13.4
Time: 4.9s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \mathsf{fma}\left(c, c, d \cdot d\right)\\ t_1 := \frac{b}{c} - \frac{d}{c \cdot \frac{c}{a}}\\ \mathbf{if}\;c \leq -1.5821466196364514 \cdot 10^{+72}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-158}:\\ \;\;\;\;\frac{c \cdot b}{t_0} - \frac{a}{\frac{t_0}{d}}\\ \mathbf{elif}\;c \leq 10^{-120}:\\ \;\;\;\;\frac{b \cdot \frac{c}{d} - a}{d}\\ \mathbf{elif}\;c \leq 7.894016947758949 \cdot 10^{-59}:\\ \;\;\;\;{\left(\mathsf{hypot}\left(d, c\right)\right)}^{-2} \cdot \left(c \cdot b - d \cdot a\right)\\ \mathbf{elif}\;c \leq 3.788409517828919 \cdot 10^{+20}:\\ \;\;\;\;\frac{-a}{d}\\ \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 (fma c c (* d d))) (t_1 (- (/ b c) (/ d (* c (/ c a))))))
   (if (<= c -1.5821466196364514e+72)
     t_1
     (if (<= c -1e-158)
       (- (/ (* c b) t_0) (/ a (/ t_0 d)))
       (if (<= c 1e-120)
         (/ (- (* b (/ c d)) a) d)
         (if (<= c 7.894016947758949e-59)
           (* (pow (hypot d c) -2.0) (- (* c b) (* d a)))
           (if (<= c 3.788409517828919e+20) (/ (- a) 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 = fma(c, c, (d * d));
	double t_1 = (b / c) - (d / (c * (c / a)));
	double tmp;
	if (c <= -1.5821466196364514e+72) {
		tmp = t_1;
	} else if (c <= -1e-158) {
		tmp = ((c * b) / t_0) - (a / (t_0 / d));
	} else if (c <= 1e-120) {
		tmp = ((b * (c / d)) - a) / d;
	} else if (c <= 7.894016947758949e-59) {
		tmp = pow(hypot(d, c), -2.0) * ((c * b) - (d * a));
	} else if (c <= 3.788409517828919e+20) {
		tmp = -a / d;
	} 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 = fma(c, c, Float64(d * d))
	t_1 = Float64(Float64(b / c) - Float64(d / Float64(c * Float64(c / a))))
	tmp = 0.0
	if (c <= -1.5821466196364514e+72)
		tmp = t_1;
	elseif (c <= -1e-158)
		tmp = Float64(Float64(Float64(c * b) / t_0) - Float64(a / Float64(t_0 / d)));
	elseif (c <= 1e-120)
		tmp = Float64(Float64(Float64(b * Float64(c / d)) - a) / d);
	elseif (c <= 7.894016947758949e-59)
		tmp = Float64((hypot(d, c) ^ -2.0) * Float64(Float64(c * b) - Float64(d * a)));
	elseif (c <= 3.788409517828919e+20)
		tmp = Float64(Float64(-a) / d);
	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[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b / c), $MachinePrecision] - N[(d / N[(c * N[(c / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -1.5821466196364514e+72], t$95$1, If[LessEqual[c, -1e-158], N[(N[(N[(c * b), $MachinePrecision] / t$95$0), $MachinePrecision] - N[(a / N[(t$95$0 / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 1e-120], N[(N[(N[(b * N[(c / d), $MachinePrecision]), $MachinePrecision] - a), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 7.894016947758949e-59], N[(N[Power[N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision], -2.0], $MachinePrecision] * N[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 3.788409517828919e+20], N[((-a) / d), $MachinePrecision], t$95$1]]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \mathsf{fma}\left(c, c, d \cdot d\right)\\
t_1 := \frac{b}{c} - \frac{d}{c \cdot \frac{c}{a}}\\
\mathbf{if}\;c \leq -1.5821466196364514 \cdot 10^{+72}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;c \leq -1 \cdot 10^{-158}:\\
\;\;\;\;\frac{c \cdot b}{t_0} - \frac{a}{\frac{t_0}{d}}\\

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

\mathbf{elif}\;c \leq 7.894016947758949 \cdot 10^{-59}:\\
\;\;\;\;{\left(\mathsf{hypot}\left(d, c\right)\right)}^{-2} \cdot \left(c \cdot b - d \cdot a\right)\\

\mathbf{elif}\;c \leq 3.788409517828919 \cdot 10^{+20}:\\
\;\;\;\;\frac{-a}{d}\\

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


\end{array}

Error

Target

Original26.1
Target0.4
Herbie13.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 5 regimes
  2. if c < -1.58214661963645142e72 or 378840951782891913000 < c

    1. Initial program 35.7

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified35.7

      \[\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 0 17.9

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

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

    if -1.58214661963645142e72 < c < -1.00000000000000006e-158

    1. Initial program 15.2

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

      \[\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 15.2

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

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

    if -1.00000000000000006e-158 < c < 9.99999999999999979e-121

    1. Initial program 22.7

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified22.7

      \[\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-rr23.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 9.9

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

      \[\leadsto \color{blue}{\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}} \]
    6. Applied egg-rr6.5

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

    if 9.99999999999999979e-121 < c < 7.8940169477589486e-59

    1. Initial program 13.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified13.4

      \[\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-rr13.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. Applied egg-rr13.4

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

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

    if 7.8940169477589486e-59 < c < 378840951782891913000

    1. Initial program 15.3

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified15.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 d around inf 37.4

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

      \[\leadsto \color{blue}{\frac{-a}{d}} \]
  3. Recombined 5 regimes into one program.
  4. Final simplification13.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -1.5821466196364514 \cdot 10^{+72}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c \cdot \frac{c}{a}}\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-158}:\\ \;\;\;\;\frac{c \cdot b}{\mathsf{fma}\left(c, c, d \cdot d\right)} - \frac{a}{\frac{\mathsf{fma}\left(c, c, d \cdot d\right)}{d}}\\ \mathbf{elif}\;c \leq 10^{-120}:\\ \;\;\;\;\frac{b \cdot \frac{c}{d} - a}{d}\\ \mathbf{elif}\;c \leq 7.894016947758949 \cdot 10^{-59}:\\ \;\;\;\;{\left(\mathsf{hypot}\left(d, c\right)\right)}^{-2} \cdot \left(c \cdot b - d \cdot a\right)\\ \mathbf{elif}\;c \leq 3.788409517828919 \cdot 10^{+20}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c \cdot \frac{c}{a}}\\ \end{array} \]

Reproduce

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