Average Error: 26.6 → 10.0
Time: 12.0s
Precision: binary64
Cost: 20932
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 2 \cdot 10^{+291}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, 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
 (if (<= (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) 2e+291)
   (/ (/ (fma a c (* b d)) (hypot c 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 tmp;
	if ((((a * c) + (b * d)) / ((c * c) + (d * d))) <= 2e+291) {
		tmp = (fma(a, c, (b * d)) / hypot(c, 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)
	tmp = 0.0
	if (Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) <= 2e+291)
		tmp = Float64(Float64(fma(a, c, Float64(b * d)) / hypot(c, 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_] := If[LessEqual[N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2e+291], N[(N[(N[(a * c + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $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}
\mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 2 \cdot 10^{+291}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, 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.6
Target0.5
Herbie10.0
\[\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 2 regimes
  2. if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 1.9999999999999999e291

    1. Initial program 14.7

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr3.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-rr2.8

      \[\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 1.9999999999999999e291 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d)))

    1. Initial program 63.0

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 2 \cdot 10^{+291}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, 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
Error13.0
Cost7700
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -2.15 \cdot 10^{+145}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq -1.75 \cdot 10^{-63}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.85 \cdot 10^{-71}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{a \cdot c}{d}}{d}\\ \mathbf{elif}\;c \leq 5.1 \cdot 10^{-14}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 23:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a + \frac{d}{\frac{c}{b}}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 2
Error13.0
Cost7700
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -2.35 \cdot 10^{+145}:\\ \;\;\;\;\frac{\left(-a\right) - \frac{b}{\frac{c}{d}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq -1.35 \cdot 10^{-63}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.12 \cdot 10^{-71}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{a \cdot c}{d}}{d}\\ \mathbf{elif}\;c \leq 5.2 \cdot 10^{-18}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 7.5:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a + \frac{d}{\frac{c}{b}}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 3
Error13.2
Cost1488
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ t_1 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -2.35 \cdot 10^{+145}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1.08 \cdot 10^{-63}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.6 \cdot 10^{-71}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{a \cdot c}{d}}{d}\\ \mathbf{elif}\;c \leq 1.7 \cdot 10^{-12}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 11.5:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Error19.1
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -1.9 \cdot 10^{-8}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -4.3 \cdot 10^{-108}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -1.25 \cdot 10^{-128}:\\ \;\;\;\;\frac{a \cdot \frac{c}{d} - b}{d}\\ \mathbf{elif}\;c \leq 10.5:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error18.1
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ t_1 := \frac{b}{d} + c \cdot \frac{a}{d \cdot d}\\ \mathbf{if}\;d \leq -5.6 \cdot 10^{+53}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{-39}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.8 \cdot 10^{-66}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 1.15 \cdot 10^{+54}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error15.4
Cost968
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -1.9 \cdot 10^{-6}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 15:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error14.7
Cost968
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -4.2 \cdot 10^{-6}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 28:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{a \cdot c}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error23.4
Cost720
\[\begin{array}{l} \mathbf{if}\;d \leq -1.65 \cdot 10^{+54}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{-39}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -1.8 \cdot 10^{-66}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 8 \cdot 10^{+16}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 9
Error37.5
Cost192
\[\frac{a}{c} \]

Error

Reproduce

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