Average Error: 26.4 → 11.8
Time: 11.6s
Precision: binary64
Cost: 20432
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -2.7673013263796686 \cdot 10^{+116}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -8.271532338194581 \cdot 10^{-74}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-240}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{\frac{d}{\frac{a}{d}}}\\ \mathbf{elif}\;c \leq 6.525239025109652 \cdot 10^{+54}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \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 (/ (/ (fma a c (* d b)) (hypot c d)) (hypot c d)))
        (t_1 (+ (/ a c) (* (/ d c) (/ b c)))))
   (if (<= c -2.7673013263796686e+116)
     t_1
     (if (<= c -8.271532338194581e-74)
       t_0
       (if (<= c 1e-240)
         (+ (/ b d) (/ c (/ d (/ a d))))
         (if (<= c 6.525239025109652e+54) t_0 t_1))))))
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 = (fma(a, c, (d * b)) / hypot(c, d)) / hypot(c, d);
	double t_1 = (a / c) + ((d / c) * (b / c));
	double tmp;
	if (c <= -2.7673013263796686e+116) {
		tmp = t_1;
	} else if (c <= -8.271532338194581e-74) {
		tmp = t_0;
	} else if (c <= 1e-240) {
		tmp = (b / d) + (c / (d / (a / d)));
	} else if (c <= 6.525239025109652e+54) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	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(fma(a, c, Float64(d * b)) / hypot(c, d)) / hypot(c, d))
	t_1 = Float64(Float64(a / c) + Float64(Float64(d / c) * Float64(b / c)))
	tmp = 0.0
	if (c <= -2.7673013263796686e+116)
		tmp = t_1;
	elseif (c <= -8.271532338194581e-74)
		tmp = t_0;
	elseif (c <= 1e-240)
		tmp = Float64(Float64(b / d) + Float64(c / Float64(d / Float64(a / d))));
	elseif (c <= 6.525239025109652e+54)
		tmp = t_0;
	else
		tmp = t_1;
	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[(N[(a * c + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(a / c), $MachinePrecision] + N[(N[(d / c), $MachinePrecision] * N[(b / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -2.7673013263796686e+116], t$95$1, If[LessEqual[c, -8.271532338194581e-74], t$95$0, If[LessEqual[c, 1e-240], N[(N[(b / d), $MachinePrecision] + N[(c / N[(d / N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 6.525239025109652e+54], t$95$0, t$95$1]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
t_1 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\
\mathbf{if}\;c \leq -2.7673013263796686 \cdot 10^{+116}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;c \leq -8.271532338194581 \cdot 10^{-74}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;c \leq 6.525239025109652 \cdot 10^{+54}:\\
\;\;\;\;t_0\\

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


\end{array}

Error

Target

Original26.4
Target0.4
Herbie11.8
\[\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 3 regimes
  2. if c < -2.7673013263796686e116 or 6.5252390251096518e54 < c

    1. Initial program 38.0

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

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

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

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

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}} \]
      Proof
      (+.f64 (/.f64 a c) (*.f64 (/.f64 d c) (/.f64 b c))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 a c) (Rewrite<= times-frac_binary64 (/.f64 (*.f64 d b) (*.f64 c c)))): 40 points increase in error, 10 points decrease in error
      (+.f64 (/.f64 a c) (/.f64 (*.f64 d b) (Rewrite<= unpow2_binary64 (pow.f64 c 2)))): 0 points increase in error, 0 points decrease in error

    if -2.7673013263796686e116 < c < -8.2715323381945814e-74 or 9.9999999999999997e-241 < c < 6.5252390251096518e54

    1. Initial program 17.5

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

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

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

    if -8.2715323381945814e-74 < c < 9.9999999999999997e-241

    1. Initial program 22.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around 0 12.4

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}} \]
    3. Simplified10.9

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)} \]
      Proof
      (fma.f64 (/.f64 c d) (/.f64 a d) (/.f64 b d)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 (/.f64 c d) (/.f64 a d)) (/.f64 b d))): 0 points increase in error, 1 points decrease in error
      (+.f64 (Rewrite<= times-frac_binary64 (/.f64 (*.f64 c a) (*.f64 d d))) (/.f64 b d)): 36 points increase in error, 16 points decrease in error
      (+.f64 (/.f64 (*.f64 c a) (Rewrite<= unpow2_binary64 (pow.f64 d 2))) (/.f64 b d)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 (/.f64 b d) (/.f64 (*.f64 c a) (pow.f64 d 2)))): 0 points increase in error, 0 points decrease in error
    4. Applied egg-rr10.9

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

      \[\leadsto \frac{b}{d} + \color{blue}{\frac{c}{\frac{d}{\frac{a}{d}}}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification11.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -2.7673013263796686 \cdot 10^{+116}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq -8.271532338194581 \cdot 10^{-74}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq 10^{-240}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{\frac{d}{\frac{a}{d}}}\\ \mathbf{elif}\;c \leq 6.525239025109652 \cdot 10^{+54}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \end{array} \]

Alternatives

Alternative 1
Error21.1
Cost1496
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \mathbf{if}\;c \leq -4.7413158013113923 \cdot 10^{+42}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 2.9 \cdot 10^{-124}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 8.106500627277879 \cdot 10^{-76}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 168042824510737.88:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 6.060970290553637 \cdot 10^{+134}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 6.634909662165788 \cdot 10^{+188}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 2
Error22.1
Cost1496
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \mathbf{if}\;c \leq -4.7413158013113923 \cdot 10^{+42}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 2.9 \cdot 10^{-124}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{\frac{d}{\frac{a}{d}}}\\ \mathbf{elif}\;c \leq 8.106500627277879 \cdot 10^{-76}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 168042824510737.88:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 6.060970290553637 \cdot 10^{+134}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 6.634909662165788 \cdot 10^{+188}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 3
Error13.3
Cost1488
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -2.7673013263796686 \cdot 10^{+116}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -8.271532338194581 \cdot 10^{-74}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-140}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{\frac{d}{\frac{a}{d}}}\\ \mathbf{elif}\;c \leq 6.525239025109652 \cdot 10^{+54}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Error15.9
Cost968
\[\begin{array}{l} \mathbf{if}\;d \leq -1307311938.091909:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \mathbf{elif}\;d \leq 1.224898047879664 \cdot 10^{+33}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{\frac{d}{\frac{a}{d}}}\\ \end{array} \]
Alternative 5
Error23.9
Cost720
\[\begin{array}{l} \mathbf{if}\;d \leq -1307311938.091909:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.224898047879664 \cdot 10^{+33}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 2.683623825811258 \cdot 10^{+89}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.5129465991589888 \cdot 10^{+142}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 6
Error59.0
Cost192
\[\frac{a}{d} \]
Alternative 7
Error37.3
Cost192
\[\frac{b}{d} \]

Error

Reproduce

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