Average Error: 26.5 → 14.8
Time: 4.4s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{b}{c} - d \cdot \frac{\frac{a}{c}}{c}\\ \mathbf{if}\;c \leq -4.689258242427546 \cdot 10^{+36}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.3285382481799226 \cdot 10^{-70}:\\ \;\;\;\;\mathsf{fma}\left(\frac{b}{d}, \frac{c}{d}, -\frac{a}{d}\right)\\ \mathbf{elif}\;c \leq 4.266676636741629 \cdot 10^{+128}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, -a, c \cdot b\right)}{\mathsf{fma}\left(c, c, d \cdot 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 (/ (/ a c) c)))))
   (if (<= c -4.689258242427546e+36)
     t_0
     (if (<= c 2.3285382481799226e-70)
       (fma (/ b d) (/ c d) (- (/ a d)))
       (if (<= c 4.266676636741629e+128)
         (/ (fma d (- a) (* c b)) (fma c c (* d 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 * ((a / c) / c));
	double tmp;
	if (c <= -4.689258242427546e+36) {
		tmp = t_0;
	} else if (c <= 2.3285382481799226e-70) {
		tmp = fma((b / d), (c / d), -(a / d));
	} else if (c <= 4.266676636741629e+128) {
		tmp = fma(d, -a, (c * b)) / fma(c, c, (d * 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 / c) - Float64(d * Float64(Float64(a / c) / c)))
	tmp = 0.0
	if (c <= -4.689258242427546e+36)
		tmp = t_0;
	elseif (c <= 2.3285382481799226e-70)
		tmp = fma(Float64(b / d), Float64(c / d), Float64(-Float64(a / d)));
	elseif (c <= 4.266676636741629e+128)
		tmp = Float64(fma(d, Float64(-a), Float64(c * b)) / fma(c, c, Float64(d * d)));
	else
		tmp = t_0;
	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[(N[(b / c), $MachinePrecision] - N[(d * N[(N[(a / c), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -4.689258242427546e+36], t$95$0, If[LessEqual[c, 2.3285382481799226e-70], N[(N[(b / d), $MachinePrecision] * N[(c / d), $MachinePrecision] + (-N[(a / d), $MachinePrecision])), $MachinePrecision], If[LessEqual[c, 4.266676636741629e+128], N[(N[(d * (-a) + N[(c * b), $MachinePrecision]), $MachinePrecision] / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{b}{c} - d \cdot \frac{\frac{a}{c}}{c}\\
\mathbf{if}\;c \leq -4.689258242427546 \cdot 10^{+36}:\\
\;\;\;\;t_0\\

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

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

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


\end{array}

Error

Target

Original26.5
Target0.5
Herbie14.8
\[\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 3 regimes
  2. if c < -4.68925824242754578e36 or 4.266676636741629e128 < c

    1. Initial program 38.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified38.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 0 17.5

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

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

    if -4.68925824242754578e36 < c < 2.3285382481799226e-70

    1. Initial program 19.6

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

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

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

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

    if 2.3285382481799226e-70 < c < 4.266676636741629e128

    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. Recombined 3 regimes into one program.
  4. Final simplification14.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.689258242427546 \cdot 10^{+36}:\\ \;\;\;\;\frac{b}{c} - d \cdot \frac{\frac{a}{c}}{c}\\ \mathbf{elif}\;c \leq 2.3285382481799226 \cdot 10^{-70}:\\ \;\;\;\;\mathsf{fma}\left(\frac{b}{d}, \frac{c}{d}, -\frac{a}{d}\right)\\ \mathbf{elif}\;c \leq 4.266676636741629 \cdot 10^{+128}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, -a, c \cdot b\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - d \cdot \frac{\frac{a}{c}}{c}\\ \end{array} \]

Reproduce

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