Average Error: 26.0 → 4.5
Time: 14.5s
Precision: binary64
Cost: 26760
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{\mathsf{hypot}\left(c, d\right)}{c}\\ \mathbf{if}\;d \leq -1.65 \cdot 10^{+172}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{\frac{d}{\frac{a}{d}}}\\ \mathbf{elif}\;d \leq 1.72 \cdot 10^{+121}:\\ \;\;\;\;t_0 \cdot \left(\frac{d \cdot b}{\mathsf{hypot}\left(c, d\right)} + \frac{a}{t_1}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(b + \frac{a}{{\left(\sqrt[3]{t_1}\right)}^{3}}\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 (/ 1.0 (hypot c d))) (t_1 (/ (hypot c d) c)))
   (if (<= d -1.65e+172)
     (+ (/ b d) (/ c (/ d (/ a d))))
     (if (<= d 1.72e+121)
       (* t_0 (+ (/ (* d b) (hypot c d)) (/ a t_1)))
       (* t_0 (+ b (/ a (pow (cbrt t_1) 3.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 = 1.0 / hypot(c, d);
	double t_1 = hypot(c, d) / c;
	double tmp;
	if (d <= -1.65e+172) {
		tmp = (b / d) + (c / (d / (a / d)));
	} else if (d <= 1.72e+121) {
		tmp = t_0 * (((d * b) / hypot(c, d)) + (a / t_1));
	} else {
		tmp = t_0 * (b + (a / pow(cbrt(t_1), 3.0)));
	}
	return tmp;
}
public static double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
	double t_0 = 1.0 / Math.hypot(c, d);
	double t_1 = Math.hypot(c, d) / c;
	double tmp;
	if (d <= -1.65e+172) {
		tmp = (b / d) + (c / (d / (a / d)));
	} else if (d <= 1.72e+121) {
		tmp = t_0 * (((d * b) / Math.hypot(c, d)) + (a / t_1));
	} else {
		tmp = t_0 * (b + (a / Math.pow(Math.cbrt(t_1), 3.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(1.0 / hypot(c, d))
	t_1 = Float64(hypot(c, d) / c)
	tmp = 0.0
	if (d <= -1.65e+172)
		tmp = Float64(Float64(b / d) + Float64(c / Float64(d / Float64(a / d))));
	elseif (d <= 1.72e+121)
		tmp = Float64(t_0 * Float64(Float64(Float64(d * b) / hypot(c, d)) + Float64(a / t_1)));
	else
		tmp = Float64(t_0 * Float64(b + Float64(a / (cbrt(t_1) ^ 3.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[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[d, -1.65e+172], N[(N[(b / d), $MachinePrecision] + N[(c / N[(d / N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 1.72e+121], N[(t$95$0 * N[(N[(N[(d * b), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] + N[(a / t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[(b + N[(a / N[Power[N[Power[t$95$1, 1/3], $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\
t_1 := \frac{\mathsf{hypot}\left(c, d\right)}{c}\\
\mathbf{if}\;d \leq -1.65 \cdot 10^{+172}:\\
\;\;\;\;\frac{b}{d} + \frac{c}{\frac{d}{\frac{a}{d}}}\\

\mathbf{elif}\;d \leq 1.72 \cdot 10^{+121}:\\
\;\;\;\;t_0 \cdot \left(\frac{d \cdot b}{\mathsf{hypot}\left(c, d\right)} + \frac{a}{t_1}\right)\\

\mathbf{else}:\\
\;\;\;\;t_0 \cdot \left(b + \frac{a}{{\left(\sqrt[3]{t_1}\right)}^{3}}\right)\\


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.0
Target0.4
Herbie4.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 3 regimes
  2. if d < -1.64999999999999991e172

    1. Initial program 43.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr28.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)}} \]
    3. Applied egg-rr28.9

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(\frac{a \cdot c}{\mathsf{hypot}\left(c, d\right)} + \frac{b \cdot d}{\mathsf{hypot}\left(c, d\right)}\right)} \]
    4. Simplified24.1

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(\frac{d \cdot b}{\mathsf{hypot}\left(c, d\right)} + \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)} \]
      Proof
      (*.f64 (/.f64 1 (hypot.f64 c d)) (+.f64 (/.f64 (*.f64 d b) (hypot.f64 c d)) (/.f64 a (/.f64 (hypot.f64 c d) c)))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 1 (hypot.f64 c d)) (+.f64 (/.f64 (Rewrite<= *-commutative_binary64 (*.f64 b d)) (hypot.f64 c d)) (/.f64 a (/.f64 (hypot.f64 c d) c)))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 1 (hypot.f64 c d)) (+.f64 (/.f64 (*.f64 b d) (hypot.f64 c d)) (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 a c) (hypot.f64 c d))))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 1 (hypot.f64 c d)) (Rewrite<= +-commutative_binary64 (+.f64 (/.f64 (*.f64 a c) (hypot.f64 c d)) (/.f64 (*.f64 b d) (hypot.f64 c d))))): 4 points increase in error, 0 points decrease in error
    5. Taylor expanded in c around 0 14.5

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}} \]
    6. Simplified9.2

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c}{\frac{d}{\frac{a}{d}}}} \]
      Proof
      (+.f64 (/.f64 b d) (/.f64 c (/.f64 d (/.f64 a d)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 b d) (/.f64 c (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 d d) a)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 b d) (/.f64 c (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 d 2)) a))): 4 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 b d) (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 c a) (pow.f64 d 2)))): 3 points increase in error, 1 points decrease in error

    if -1.64999999999999991e172 < d < 1.7200000000000001e121

    1. Initial program 19.8

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

      \[\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-rr12.4

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(\frac{a \cdot c}{\mathsf{hypot}\left(c, d\right)} + \frac{b \cdot d}{\mathsf{hypot}\left(c, d\right)}\right)} \]
    4. Simplified3.7

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(\frac{d \cdot b}{\mathsf{hypot}\left(c, d\right)} + \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)} \]
      Proof
      (*.f64 (/.f64 1 (hypot.f64 c d)) (+.f64 (/.f64 (*.f64 d b) (hypot.f64 c d)) (/.f64 a (/.f64 (hypot.f64 c d) c)))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 1 (hypot.f64 c d)) (+.f64 (/.f64 (Rewrite<= *-commutative_binary64 (*.f64 b d)) (hypot.f64 c d)) (/.f64 a (/.f64 (hypot.f64 c d) c)))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 1 (hypot.f64 c d)) (+.f64 (/.f64 (*.f64 b d) (hypot.f64 c d)) (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 a c) (hypot.f64 c d))))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 1 (hypot.f64 c d)) (Rewrite<= +-commutative_binary64 (+.f64 (/.f64 (*.f64 a c) (hypot.f64 c d)) (/.f64 (*.f64 b d) (hypot.f64 c d))))): 4 points increase in error, 0 points decrease in error

    if 1.7200000000000001e121 < d

    1. Initial program 40.8

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

      \[\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.3

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(\frac{a \cdot c}{\mathsf{hypot}\left(c, d\right)} + \frac{b \cdot d}{\mathsf{hypot}\left(c, d\right)}\right)} \]
    4. Simplified21.2

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(\frac{d \cdot b}{\mathsf{hypot}\left(c, d\right)} + \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)} \]
      Proof
      (*.f64 (/.f64 1 (hypot.f64 c d)) (+.f64 (/.f64 (*.f64 d b) (hypot.f64 c d)) (/.f64 a (/.f64 (hypot.f64 c d) c)))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 1 (hypot.f64 c d)) (+.f64 (/.f64 (Rewrite<= *-commutative_binary64 (*.f64 b d)) (hypot.f64 c d)) (/.f64 a (/.f64 (hypot.f64 c d) c)))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 1 (hypot.f64 c d)) (+.f64 (/.f64 (*.f64 b d) (hypot.f64 c d)) (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 a c) (hypot.f64 c d))))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 1 (hypot.f64 c d)) (Rewrite<= +-commutative_binary64 (+.f64 (/.f64 (*.f64 a c) (hypot.f64 c d)) (/.f64 (*.f64 b d) (hypot.f64 c d))))): 4 points increase in error, 0 points decrease in error
    5. Applied egg-rr21.3

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{d \cdot b}{\mathsf{hypot}\left(c, d\right)} + \frac{a}{\color{blue}{{\left(\sqrt[3]{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)}^{3}}}\right) \]
    6. Taylor expanded in d around inf 4.5

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\color{blue}{b} + \frac{a}{{\left(\sqrt[3]{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)}^{3}}\right) \]
  3. Recombined 3 regimes into one program.
  4. Final simplification4.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.65 \cdot 10^{+172}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{\frac{d}{\frac{a}{d}}}\\ \mathbf{elif}\;d \leq 1.72 \cdot 10^{+121}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{d \cdot b}{\mathsf{hypot}\left(c, d\right)} + \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(b + \frac{a}{{\left(\sqrt[3]{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)}^{3}}\right)\\ \end{array} \]

Alternatives

Alternative 1
Error5.2
Cost20616
\[\begin{array}{l} \mathbf{if}\;d \leq -1.85 \cdot 10^{+171}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{\frac{d}{\frac{a}{d}}}\\ \mathbf{elif}\;d \leq 1.4 \cdot 10^{+123}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{d \cdot b}{\mathsf{hypot}\left(c, d\right)} + \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \end{array} \]
Alternative 2
Error10.7
Cost20296
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \mathsf{fma}\left(d, d, c \cdot c\right)\\ \mathbf{if}\;c \leq -1.75 \cdot 10^{+73}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -1.16 \cdot 10^{-194}:\\ \;\;\;\;t_0 \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq 5.8 \cdot 10^{-94}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 4.5 \cdot 10^{+123}:\\ \;\;\;\;\frac{d}{\frac{t_1}{b}} + \frac{c}{\frac{t_1}{a}}\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ \end{array} \]
Alternative 3
Error12.0
Cost14544
\[\begin{array}{l} t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\ t_1 := \frac{d}{\frac{t_0}{b}} + \frac{c}{\frac{t_0}{a}}\\ \mathbf{if}\;c \leq -9.8 \cdot 10^{+145}:\\ \;\;\;\;\frac{\frac{c}{\frac{\mathsf{hypot}\left(c, d\right)}{a}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq -3.8 \cdot 10^{-93}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 3.4 \cdot 10^{-92}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 4.4 \cdot 10^{+124}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ \end{array} \]
Alternative 4
Error11.9
Cost7696
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -7.6 \cdot 10^{+72}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -5 \cdot 10^{-102}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 9.5 \cdot 10^{-94}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 2.25 \cdot 10^{+50}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ \end{array} \]
Alternative 5
Error12.1
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{b}{c} \cdot \frac{d}{c}\\ \mathbf{if}\;c \leq -7.6 \cdot 10^{+72}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -2.8 \cdot 10^{-102}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.05 \cdot 10^{-93}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 2.4 \cdot 10^{+50}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error15.8
Cost1234
\[\begin{array}{l} \mathbf{if}\;c \leq -1.95 \cdot 10^{-7} \lor \neg \left(c \leq 7 \cdot 10^{-56}\right) \land \left(c \leq 2.6 \cdot 10^{+36} \lor \neg \left(c \leq 2.1 \cdot 10^{+56}\right)\right):\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \end{array} \]
Alternative 7
Error19.5
Cost1100
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{if}\;c \leq -1.9 \cdot 10^{-26}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -8.8 \cdot 10^{-93}:\\ \;\;\;\;\frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;c \leq 3 \cdot 10^{-59}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error24.1
Cost712
\[\begin{array}{l} \mathbf{if}\;c \leq -860000:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -8.8 \cdot 10^{-93}:\\ \;\;\;\;a \cdot \frac{c}{d \cdot d}\\ \mathbf{elif}\;c \leq 1.95 \cdot 10^{-18}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 9
Error24.1
Cost712
\[\begin{array}{l} \mathbf{if}\;c \leq -850000:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -7.8 \cdot 10^{-93}:\\ \;\;\;\;\frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;c \leq 5.2 \cdot 10^{-19}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 10
Error22.8
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -5.8 \cdot 10^{+51}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 1.7 \cdot 10^{-18}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 11
Error37.6
Cost192
\[\frac{a}{c} \]

Error

Reproduce

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