?

Average Error: 26.6 → 10.5
Time: 14.7s
Precision: binary64
Cost: 21060

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;\frac{b \cdot d + a \cdot c}{c \cdot c + d \cdot d} \leq 5 \cdot 10^{+264}:\\ \;\;\;\;\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{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\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 (<= (/ (+ (* b d) (* a c)) (+ (* c c) (* d d))) 5e+264)
   (* (/ 1.0 (hypot c d)) (/ (fma a c (* b d)) (hypot c d)))
   (* (/ c (hypot c d)) (/ a (hypot c d)))))
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 ((((b * d) + (a * c)) / ((c * c) + (d * d))) <= 5e+264) {
		tmp = (1.0 / hypot(c, d)) * (fma(a, c, (b * d)) / hypot(c, d));
	} else {
		tmp = (c / hypot(c, d)) * (a / hypot(c, d));
	}
	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(b * d) + Float64(a * c)) / Float64(Float64(c * c) + Float64(d * d))) <= 5e+264)
		tmp = Float64(Float64(1.0 / hypot(c, d)) * Float64(fma(a, c, Float64(b * d)) / hypot(c, d)));
	else
		tmp = Float64(Float64(c / hypot(c, d)) * Float64(a / hypot(c, d)));
	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[(b * d), $MachinePrecision] + N[(a * c), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 5e+264], 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[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(a / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;\frac{b \cdot d + a \cdot c}{c \cdot c + d \cdot d} \leq 5 \cdot 10^{+264}:\\
\;\;\;\;\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{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\\


\end{array}

Error?

Target

Original26.6
Target0.5
Herbie10.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 2 regimes
  2. if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 5.00000000000000033e264

    1. Initial program 14.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr2.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 5.00000000000000033e264 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d)))

    1. Initial program 61.4

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

      \[\leadsto \frac{\color{blue}{c \cdot a}}{c \cdot c + d \cdot d} \]
    3. Applied egg-rr32.3

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

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

Alternatives

Alternative 1
Error12.8
Cost13904
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ \mathbf{if}\;c \leq -7.6 \cdot 10^{+69}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -2.45 \cdot 10^{-139}:\\ \;\;\;\;\frac{b \cdot d + a \cdot c}{t_0}\\ \mathbf{elif}\;c \leq 7.6 \cdot 10^{-143}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 3.4 \cdot 10^{+90}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, b, a \cdot c\right)}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 2
Error12.9
Cost13904
\[\begin{array}{l} \mathbf{if}\;c \leq -4.2 \cdot 10^{+69}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -1.22 \cdot 10^{-139}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{elif}\;c \leq 5 \cdot 10^{-144}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 9.2 \cdot 10^{+93}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, b, a \cdot c\right)}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 3
Error12.5
Cost7760
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ \mathbf{if}\;c \leq -6.7 \cdot 10^{+69}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -1.4 \cdot 10^{-138}:\\ \;\;\;\;\frac{b \cdot d + a \cdot c}{t_0}\\ \mathbf{elif}\;c \leq 2.4 \cdot 10^{-141}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 1.26 \cdot 10^{+144}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, b, a \cdot c\right)}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \end{array} \]
Alternative 4
Error12.6
Cost1488
\[\begin{array}{l} t_0 := \frac{b \cdot d + a \cdot c}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -8.5 \cdot 10^{+69}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -5 \cdot 10^{-139}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 6.4 \cdot 10^{-142}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 1.16 \cdot 10^{+144}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \end{array} \]
Alternative 5
Error16.8
Cost1364
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ t_1 := d \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -57:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq 6.5 \cdot 10^{-50}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.3 \cdot 10^{+27}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + t_1\right)\\ \mathbf{elif}\;c \leq 3.1 \cdot 10^{+78}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.5 \cdot 10^{+151}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{t_1}{c}\\ \end{array} \]
Alternative 6
Error16.6
Cost1364
\[\begin{array}{l} t_0 := d \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -7300000000:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq 6.5 \cdot 10^{-50}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 4.5 \cdot 10^{+27}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + t_0\right)\\ \mathbf{elif}\;c \leq 4.5 \cdot 10^{+78}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 2.8 \cdot 10^{+151}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{t_0}{c}\\ \end{array} \]
Alternative 7
Error16.6
Cost1364
\[\begin{array}{l} t_0 := d \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -420000:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq 3.5 \cdot 10^{-52}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 2.7 \cdot 10^{+26}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + t_0\right)\\ \mathbf{elif}\;c \leq 6 \cdot 10^{+78}:\\ \;\;\;\;\frac{b}{d} + \frac{1}{\frac{d}{c} \cdot \frac{d}{a}}\\ \mathbf{elif}\;c \leq 2.5 \cdot 10^{+151}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{t_0}{c}\\ \end{array} \]
Alternative 8
Error21.7
Cost1232
\[\begin{array}{l} t_0 := \frac{1}{c} \cdot \left(a + d \cdot \frac{b}{c}\right)\\ \mathbf{if}\;c \leq -1.95 \cdot 10^{-70}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.45 \cdot 10^{-234}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 9.5 \cdot 10^{-151}:\\ \;\;\;\;\frac{a \cdot c}{d \cdot d}\\ \mathbf{elif}\;c \leq 6.5 \cdot 10^{-50}:\\ \;\;\;\;b \cdot \frac{d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 9
Error21.7
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -1.85 \cdot 10^{-70}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq 1.45 \cdot 10^{-234}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 9.5 \cdot 10^{-151}:\\ \;\;\;\;\frac{a \cdot c}{d \cdot d}\\ \mathbf{elif}\;c \leq 6 \cdot 10^{-50}:\\ \;\;\;\;b \cdot \frac{d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + d \cdot \frac{b}{c}\right)\\ \end{array} \]
Alternative 10
Error21.7
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -7.4 \cdot 10^{-71}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq 1.45 \cdot 10^{-234}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 9.5 \cdot 10^{-151}:\\ \;\;\;\;\frac{a \cdot c}{d \cdot d}\\ \mathbf{elif}\;c \leq 1.8 \cdot 10^{-50}:\\ \;\;\;\;b \cdot \frac{d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \end{array} \]
Alternative 11
Error19.1
Cost968
\[\begin{array}{l} \mathbf{if}\;d \leq -1.55 \cdot 10^{+33}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 8.5 \cdot 10^{+64}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + d \cdot \frac{b}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 12
Error23.9
Cost844
\[\begin{array}{l} \mathbf{if}\;d \leq -1.8 \cdot 10^{+26}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.8 \cdot 10^{-147}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -7.6 \cdot 10^{-193}:\\ \;\;\;\;b \cdot \frac{d}{c \cdot c}\\ \mathbf{elif}\;d \leq 2.4 \cdot 10^{+25}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 13
Error23.9
Cost844
\[\begin{array}{l} \mathbf{if}\;d \leq -3.9 \cdot 10^{+27}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.8 \cdot 10^{-147}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -7.6 \cdot 10^{-193}:\\ \;\;\;\;\frac{b}{\frac{c}{\frac{d}{c}}}\\ \mathbf{elif}\;d \leq 2.35 \cdot 10^{+25}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 14
Error23.8
Cost844
\[\begin{array}{l} \mathbf{if}\;d \leq -7.5 \cdot 10^{+27}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.8 \cdot 10^{-147}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -7.6 \cdot 10^{-193}:\\ \;\;\;\;\frac{b \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 2.4 \cdot 10^{+25}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 15
Error22.9
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -1.65 \cdot 10^{+28}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 2.4 \cdot 10^{+25}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 16
Error37.9
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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