Average Error: 26.7 → 15.5
Time: 24.2s
Precision: binary64
Cost: 14032
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{b}{d} + \left({\left(\frac{-1}{d}\right)}^{2} \cdot a\right) \cdot c\\ \mathbf{if}\;d \leq -6.2 \cdot 10^{+65}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.02 \cdot 10^{-115}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 9.2 \cdot 10^{-151}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{elif}\;d \leq 6.5 \cdot 10^{-77}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{elif}\;d \leq 5.3 \cdot 10^{+20}:\\ \;\;\;\;\frac{\frac{d \cdot b}{c} + a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (+ (/ b d) (* (* (pow (/ -1.0 d) 2.0) a) c))))
   (if (<= d -6.2e+65)
     t_0
     (if (<= d -1.02e-115)
       (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))
       (if (<= d 9.2e-151)
         (+ (/ a c) (/ (* (/ b c) d) c))
         (if (<= d 6.5e-77)
           (/ (fma a c (* b d)) (fma c c (* d d)))
           (if (<= d 5.3e+20) (/ (+ (/ (* d b) c) a) c) t_0)))))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	double t_0 = (b / d) + ((pow((-1.0 / d), 2.0) * a) * c);
	double tmp;
	if (d <= -6.2e+65) {
		tmp = t_0;
	} else if (d <= -1.02e-115) {
		tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
	} else if (d <= 9.2e-151) {
		tmp = (a / c) + (((b / c) * d) / c);
	} else if (d <= 6.5e-77) {
		tmp = fma(a, c, (b * d)) / fma(c, c, (d * d));
	} else if (d <= 5.3e+20) {
		tmp = (((d * b) / c) + a) / c;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(a, b, c, d)
	return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
end
function code(a, b, c, d)
	t_0 = Float64(Float64(b / d) + Float64(Float64((Float64(-1.0 / d) ^ 2.0) * a) * c))
	tmp = 0.0
	if (d <= -6.2e+65)
		tmp = t_0;
	elseif (d <= -1.02e-115)
		tmp = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)));
	elseif (d <= 9.2e-151)
		tmp = Float64(Float64(a / c) + Float64(Float64(Float64(b / c) * d) / c));
	elseif (d <= 6.5e-77)
		tmp = Float64(fma(a, c, Float64(b * d)) / fma(c, c, Float64(d * d)));
	elseif (d <= 5.3e+20)
		tmp = Float64(Float64(Float64(Float64(d * b) / c) + a) / c);
	else
		tmp = t_0;
	end
	return tmp
end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * 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 / d), $MachinePrecision] + N[(N[(N[Power[N[(-1.0 / d), $MachinePrecision], 2.0], $MachinePrecision] * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -6.2e+65], t$95$0, If[LessEqual[d, -1.02e-115], N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 9.2e-151], N[(N[(a / c), $MachinePrecision] + N[(N[(N[(b / c), $MachinePrecision] * d), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 6.5e-77], N[(N[(a * c + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 5.3e+20], N[(N[(N[(N[(d * b), $MachinePrecision] / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], t$95$0]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{b}{d} + \left({\left(\frac{-1}{d}\right)}^{2} \cdot a\right) \cdot c\\
\mathbf{if}\;d \leq -6.2 \cdot 10^{+65}:\\
\;\;\;\;t_0\\

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

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

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

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

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


\end{array}

Error

Target

Original26.7
Target0.4
Herbie15.5
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \left|c\right|:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array} \]

Derivation

  1. Split input into 5 regimes
  2. if d < -6.19999999999999981e65 or 5.3e20 < d

    1. Initial program 34.7

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

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

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

      \[\leadsto \color{blue}{\frac{b}{d} + \left({\left(\frac{-1}{d}\right)}^{2} \cdot a\right) \cdot c} \]
      Proof

    if -6.19999999999999981e65 < d < -1.02e-115

    1. Initial program 17.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]

    if -1.02e-115 < d < 9.19999999999999984e-151

    1. Initial program 24.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around -inf 15.1

      \[\leadsto \color{blue}{\frac{a}{c} + d \cdot \left(b \cdot {\left(\frac{-1}{c}\right)}^{2}\right)} \]
    3. Simplified12.2

      \[\leadsto \color{blue}{\frac{a}{c} + \left(d \cdot b\right) \cdot {\left(\frac{-1}{c}\right)}^{2}} \]
      Proof
    4. Applied egg-rr7.5

      \[\leadsto \frac{a}{c} + \color{blue}{\frac{\frac{1}{c} \cdot \left(d \cdot b\right)}{c}} \]
    5. Applied egg-rr8.9

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

    if 9.19999999999999984e-151 < d < 6.4999999999999999e-77

    1. Initial program 15.1

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

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

    if 6.4999999999999999e-77 < d < 5.3e20

    1. Initial program 16.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around -inf 28.6

      \[\leadsto \color{blue}{\frac{a}{c} + d \cdot \left(b \cdot {\left(\frac{-1}{c}\right)}^{2}\right)} \]
    3. Simplified28.8

      \[\leadsto \color{blue}{\frac{a}{c} + \left(d \cdot b\right) \cdot {\left(\frac{-1}{c}\right)}^{2}} \]
      Proof
    4. Taylor expanded in a around 0 28.8

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}} \]
    5. Simplified27.0

      \[\leadsto \color{blue}{\frac{\frac{d \cdot b}{c} + a}{c}} \]
      Proof
  3. Recombined 5 regimes into one program.

Alternatives

Alternative 1
Error15.7
Cost7828
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ t_1 := \frac{b}{d} + \left({\left(\frac{-1}{d}\right)}^{2} \cdot a\right) \cdot c\\ \mathbf{if}\;d \leq -6.4 \cdot 10^{+65}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -3.8 \cdot 10^{-115}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.85 \cdot 10^{-161}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{-77}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 6.5 \cdot 10^{+24}:\\ \;\;\;\;\frac{\frac{d \cdot b}{c} + a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error15.1
Cost1488
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -5.2 \cdot 10^{+94}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -2.3 \cdot 10^{-115}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{-152}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{elif}\;d \leq 9.2 \cdot 10^{+87}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 3
Error20.7
Cost1232
\[\begin{array}{l} \mathbf{if}\;d \leq -1.55 \cdot 10^{+22}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.05 \cdot 10^{-64}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{-89}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 2.5 \cdot 10^{+24}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 4
Error20.5
Cost1232
\[\begin{array}{l} \mathbf{if}\;d \leq -1.6 \cdot 10^{+26}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.05 \cdot 10^{-64}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{-89}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.15 \cdot 10^{+24}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 5
Error20.8
Cost1232
\[\begin{array}{l} \mathbf{if}\;d \leq -1.35 \cdot 10^{+26}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -2 \cdot 10^{-64}:\\ \;\;\;\;\frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{-89}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 4 \cdot 10^{+24}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 6
Error20.2
Cost1104
\[\begin{array}{l} \mathbf{if}\;d \leq -7.2 \cdot 10^{+24}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.1 \cdot 10^{-64}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{-89}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.95 \cdot 10^{+25}:\\ \;\;\;\;\frac{\frac{d \cdot b}{c} + a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 7
Error19.7
Cost1100
\[\begin{array}{l} \mathbf{if}\;d \leq -1.46 \cdot 10^{+41}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{-89}:\\ \;\;\;\;\frac{d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 4.2 \cdot 10^{+24}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 8
Error24.0
Cost720
\[\begin{array}{l} \mathbf{if}\;d \leq -1.06 \cdot 10^{+27}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4.4 \cdot 10^{-64}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -6 \cdot 10^{-96}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.5 \cdot 10^{+44}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 9
Error37.2
Cost192
\[\frac{a}{c} \]

Error

Reproduce

herbie shell --seed 2023010 
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d)))))

  (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))