?

Average Error: 26.4 → 9.8
Time: 11.4s
Precision: binary64
Cost: 22088

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{+267}:\\ \;\;\;\;\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)}\\ \mathbf{else}:\\ \;\;\;\;\frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \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 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))))
   (if (<= t_0 (- INFINITY))
     (/ b (+ d (/ c (/ d c))))
     (if (<= t_0 2e+267)
       (* (/ 1.0 (hypot c d)) (/ (fma a c (* b d)) (hypot c d)))
       (* (/ d (hypot d c)) (/ b (hypot d c)))))))
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 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double tmp;
	if (t_0 <= -((double) INFINITY)) {
		tmp = b / (d + (c / (d / c)));
	} else if (t_0 <= 2e+267) {
		tmp = (1.0 / hypot(c, d)) * (fma(a, c, (b * d)) / hypot(c, d));
	} else {
		tmp = (d / hypot(d, c)) * (b / hypot(d, c));
	}
	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(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
	tmp = 0.0
	if (t_0 <= Float64(-Inf))
		tmp = Float64(b / Float64(d + Float64(c / Float64(d / c))));
	elseif (t_0 <= 2e+267)
		tmp = Float64(Float64(1.0 / hypot(c, d)) * Float64(fma(a, c, Float64(b * d)) / hypot(c, d)));
	else
		tmp = Float64(Float64(d / hypot(d, c)) * Float64(b / hypot(d, c)));
	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), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], N[(b / N[(d + N[(c / N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 2e+267], N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(a * c + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(d / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision] * N[(b / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{if}\;t_0 \leq -\infty:\\
\;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\

\mathbf{elif}\;t_0 \leq 2 \cdot 10^{+267}:\\
\;\;\;\;\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)}\\

\mathbf{else}:\\
\;\;\;\;\frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\


\end{array}

Error?

Target

Original26.4
Target0.5
Herbie9.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 (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < -inf.0

    1. Initial program 64.0

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

      \[\leadsto \frac{\color{blue}{d \cdot b}}{c \cdot c + d \cdot d} \]
    3. Applied egg-rr31.2

      \[\leadsto \color{blue}{\frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}} \]
    4. Applied egg-rr30.2

      \[\leadsto \color{blue}{\frac{b}{\frac{\frac{\mathsf{hypot}\left(d, c\right)}{d}}{\frac{1}{\mathsf{hypot}\left(d, c\right)}}}} \]
    5. Taylor expanded in d around 0 34.0

      \[\leadsto \frac{b}{\color{blue}{\frac{{c}^{2}}{d} + d}} \]
    6. Simplified30.1

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

      [Start]34.0

      \[ \frac{b}{\frac{{c}^{2}}{d} + d} \]

      +-commutative [=>]34.0

      \[ \frac{b}{\color{blue}{d + \frac{{c}^{2}}{d}}} \]

      unpow2 [=>]34.0

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

      associate-/l* [=>]30.1

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

    if -inf.0 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 1.9999999999999999e267

    1. Initial program 11.5

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

      \[\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)}} \]

    if 1.9999999999999999e267 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d)))

    1. Initial program 61.9

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

      \[\leadsto \frac{\color{blue}{d \cdot b}}{c \cdot c + d \cdot d} \]
    3. Applied egg-rr31.4

      \[\leadsto \color{blue}{\frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification9.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq -\infty:\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \mathbf{elif}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 2 \cdot 10^{+267}:\\ \;\;\;\;\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)}\\ \mathbf{else}:\\ \;\;\;\;\frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error12.5
Cost13904
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -1.18 \cdot 10^{+58}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq -2.5 \cdot 10^{-154}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.65 \cdot 10^{-153}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq 4.9 \cdot 10^{+83}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \end{array} \]
Alternative 2
Error12.8
Cost1488
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -1.18 \cdot 10^{+58}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq -5.8 \cdot 10^{-154}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.2 \cdot 10^{-151}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq 6.5 \cdot 10^{+83}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \end{array} \]
Alternative 3
Error18.1
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ t_1 := \frac{b + c \cdot \frac{a}{d}}{d}\\ \mathbf{if}\;d \leq -8.5 \cdot 10^{-63}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 1.14 \cdot 10^{-150}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 5.9 \cdot 10^{-95}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 5.2 \cdot 10^{+40}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \end{array} \]
Alternative 4
Error18.2
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ t_1 := \frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{if}\;d \leq -2.7 \cdot 10^{-60}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 1.14 \cdot 10^{-150}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 5.9 \cdot 10^{-95}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 7 \cdot 10^{+40}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \end{array} \]
Alternative 5
Error17.6
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;d \leq -1.95 \cdot 10^{-60}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq 1.1 \cdot 10^{-150}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 5.9 \cdot 10^{-95}:\\ \;\;\;\;\frac{b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 6.4 \cdot 10^{+40}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \end{array} \]
Alternative 6
Error24.1
Cost844
\[\begin{array}{l} \mathbf{if}\;d \leq -8.2 \cdot 10^{-55}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.14 \cdot 10^{-150}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 5.9 \cdot 10^{-95}:\\ \;\;\;\;\frac{b \cdot d}{d \cdot d}\\ \mathbf{elif}\;d \leq 5.4 \cdot 10^{+40}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 7
Error20.1
Cost841
\[\begin{array}{l} \mathbf{if}\;d \leq -5.8 \cdot 10^{-59} \lor \neg \left(d \leq 2.5 \cdot 10^{-151}\right):\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 8
Error20.0
Cost840
\[\begin{array}{l} \mathbf{if}\;d \leq -1.1 \cdot 10^{-73}:\\ \;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;d \leq 1.14 \cdot 10^{-150}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d + \frac{c}{\frac{d}{c}}}\\ \end{array} \]
Alternative 9
Error24.1
Cost721
\[\begin{array}{l} \mathbf{if}\;d \leq -1.4 \cdot 10^{-55}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.14 \cdot 10^{-150} \lor \neg \left(d \leq 5.9 \cdot 10^{-95}\right) \land d \leq 5.2 \cdot 10^{+40}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 10
Error37.1
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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