?

Average Error: 26.6 → 10.6
Time: 14.4s
Precision: binary64
Cost: 14160

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{\frac{c \cdot a + d \cdot b}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{d}{\frac{c}{b}}\\ \mathbf{if}\;c \leq -4.8 \cdot 10^{+82}:\\ \;\;\;\;\frac{\left(-a\right) - t_1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq -3.5 \cdot 10^{-110}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.9 \cdot 10^{-138}:\\ \;\;\;\;\frac{b}{d} + \frac{1}{d} \cdot \left(c \cdot \frac{a}{d}\right)\\ \mathbf{elif}\;c \leq 2.55 \cdot 10^{+39}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a + t_1}{\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
 (let* ((t_0 (/ (/ (+ (* c a) (* d b)) (hypot c d)) (hypot c d)))
        (t_1 (/ d (/ c b))))
   (if (<= c -4.8e+82)
     (/ (- (- a) t_1) (hypot c d))
     (if (<= c -3.5e-110)
       t_0
       (if (<= c 2.9e-138)
         (+ (/ b d) (* (/ 1.0 d) (* c (/ a d))))
         (if (<= c 2.55e+39) t_0 (/ (+ a t_1) (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 t_0 = (((c * a) + (d * b)) / hypot(c, d)) / hypot(c, d);
	double t_1 = d / (c / b);
	double tmp;
	if (c <= -4.8e+82) {
		tmp = (-a - t_1) / hypot(c, d);
	} else if (c <= -3.5e-110) {
		tmp = t_0;
	} else if (c <= 2.9e-138) {
		tmp = (b / d) + ((1.0 / d) * (c * (a / d)));
	} else if (c <= 2.55e+39) {
		tmp = t_0;
	} else {
		tmp = (a + t_1) / hypot(c, d);
	}
	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 = (((c * a) + (d * b)) / Math.hypot(c, d)) / Math.hypot(c, d);
	double t_1 = d / (c / b);
	double tmp;
	if (c <= -4.8e+82) {
		tmp = (-a - t_1) / Math.hypot(c, d);
	} else if (c <= -3.5e-110) {
		tmp = t_0;
	} else if (c <= 2.9e-138) {
		tmp = (b / d) + ((1.0 / d) * (c * (a / d)));
	} else if (c <= 2.55e+39) {
		tmp = t_0;
	} else {
		tmp = (a + t_1) / Math.hypot(c, d);
	}
	return tmp;
}
def code(a, b, c, d):
	return ((a * c) + (b * d)) / ((c * c) + (d * d))
def code(a, b, c, d):
	t_0 = (((c * a) + (d * b)) / math.hypot(c, d)) / math.hypot(c, d)
	t_1 = d / (c / b)
	tmp = 0
	if c <= -4.8e+82:
		tmp = (-a - t_1) / math.hypot(c, d)
	elif c <= -3.5e-110:
		tmp = t_0
	elif c <= 2.9e-138:
		tmp = (b / d) + ((1.0 / d) * (c * (a / d)))
	elif c <= 2.55e+39:
		tmp = t_0
	else:
		tmp = (a + t_1) / math.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)
	t_0 = Float64(Float64(Float64(Float64(c * a) + Float64(d * b)) / hypot(c, d)) / hypot(c, d))
	t_1 = Float64(d / Float64(c / b))
	tmp = 0.0
	if (c <= -4.8e+82)
		tmp = Float64(Float64(Float64(-a) - t_1) / hypot(c, d));
	elseif (c <= -3.5e-110)
		tmp = t_0;
	elseif (c <= 2.9e-138)
		tmp = Float64(Float64(b / d) + Float64(Float64(1.0 / d) * Float64(c * Float64(a / d))));
	elseif (c <= 2.55e+39)
		tmp = t_0;
	else
		tmp = Float64(Float64(a + t_1) / hypot(c, d));
	end
	return tmp
end
function tmp = code(a, b, c, d)
	tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
end
function tmp_2 = code(a, b, c, d)
	t_0 = (((c * a) + (d * b)) / hypot(c, d)) / hypot(c, d);
	t_1 = d / (c / b);
	tmp = 0.0;
	if (c <= -4.8e+82)
		tmp = (-a - t_1) / hypot(c, d);
	elseif (c <= -3.5e-110)
		tmp = t_0;
	elseif (c <= 2.9e-138)
		tmp = (b / d) + ((1.0 / d) * (c * (a / d)));
	elseif (c <= 2.55e+39)
		tmp = t_0;
	else
		tmp = (a + t_1) / hypot(c, d);
	end
	tmp_2 = 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[(N[(c * a), $MachinePrecision] + 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[(d / N[(c / b), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -4.8e+82], N[(N[((-a) - t$95$1), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -3.5e-110], t$95$0, If[LessEqual[c, 2.9e-138], N[(N[(b / d), $MachinePrecision] + N[(N[(1.0 / d), $MachinePrecision] * N[(c * N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.55e+39], t$95$0, N[(N[(a + t$95$1), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{\frac{c \cdot a + d \cdot b}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
t_1 := \frac{d}{\frac{c}{b}}\\
\mathbf{if}\;c \leq -4.8 \cdot 10^{+82}:\\
\;\;\;\;\frac{\left(-a\right) - t_1}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \leq -3.5 \cdot 10^{-110}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;c \leq 2.9 \cdot 10^{-138}:\\
\;\;\;\;\frac{b}{d} + \frac{1}{d} \cdot \left(c \cdot \frac{a}{d}\right)\\

\mathbf{elif}\;c \leq 2.55 \cdot 10^{+39}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{a + t_1}{\mathsf{hypot}\left(c, d\right)}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.6
Target0.4
Herbie10.6
\[\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 4 regimes
  2. if c < -4.79999999999999996e82

    1. Initial program 39.4

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

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

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

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

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

      [Start]14.3

      \[ \frac{-1 \cdot \frac{d \cdot b}{c} + -1 \cdot a}{\mathsf{hypot}\left(c, d\right)} \]

      distribute-lft-out [=>]14.3

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

      +-commutative [<=]14.3

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

      associate-/l* [=>]10.6

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

    if -4.79999999999999996e82 < c < -3.49999999999999974e-110 or 2.89999999999999973e-138 < c < 2.5499999999999999e39

    1. Initial program 15.6

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

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

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

    if -3.49999999999999974e-110 < c < 2.89999999999999973e-138

    1. Initial program 22.0

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

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

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

      [Start]10.2

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

      associate-/l* [=>]13.6

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

      unpow2 [=>]13.6

      \[ \frac{b}{d} + \frac{c}{\frac{\color{blue}{d \cdot d}}{a}} \]
    4. Applied egg-rr8.6

      \[\leadsto \frac{b}{d} + \color{blue}{\frac{1}{d} \cdot \left(c \cdot \frac{a}{d}\right)} \]

    if 2.5499999999999999e39 < c

    1. Initial program 36.8

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

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

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

      \[\leadsto \frac{\color{blue}{a + \frac{d \cdot b}{c}}}{\mathsf{hypot}\left(c, d\right)} \]
    5. Simplified13.6

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

      [Start]16.5

      \[ \frac{a + \frac{d \cdot b}{c}}{\mathsf{hypot}\left(c, d\right)} \]

      associate-/l* [=>]13.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.8 \cdot 10^{+82}:\\ \;\;\;\;\frac{\left(-a\right) - \frac{d}{\frac{c}{b}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq -3.5 \cdot 10^{-110}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq 2.9 \cdot 10^{-138}:\\ \;\;\;\;\frac{b}{d} + \frac{1}{d} \cdot \left(c \cdot \frac{a}{d}\right)\\ \mathbf{elif}\;c \leq 2.55 \cdot 10^{+39}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a + \frac{d}{\frac{c}{b}}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error12.5
Cost7568
\[\begin{array}{l} t_0 := \frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -6 \cdot 10^{+76}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq -1.65 \cdot 10^{-116}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.25 \cdot 10^{-70}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d \cdot \frac{d}{c}}\\ \mathbf{elif}\;c \leq 2.55 \cdot 10^{+39}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a + \frac{d}{\frac{c}{b}}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 2
Error12.3
Cost7568
\[\begin{array}{l} t_0 := \frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ t_1 := \frac{d}{\frac{c}{b}}\\ \mathbf{if}\;c \leq -5 \cdot 10^{+78}:\\ \;\;\;\;\frac{\left(-a\right) - t_1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq -2.6 \cdot 10^{-116}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 4 \cdot 10^{-74}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d \cdot \frac{d}{c}}\\ \mathbf{elif}\;c \leq 1.12 \cdot 10^{+39}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a + t_1}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 3
Error12.7
Cost1488
\[\begin{array}{l} t_0 := \frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ t_1 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -5.4 \cdot 10^{+78}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -8.8 \cdot 10^{-116}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.55 \cdot 10^{-74}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d \cdot \frac{d}{c}}\\ \mathbf{elif}\;c \leq 2.55 \cdot 10^{+39}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Error19.4
Cost1232
\[\begin{array}{l} t_0 := \frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ \mathbf{if}\;c \leq -8 \cdot 10^{+29}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 1.15 \cdot 10^{-67}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.9 \cdot 10^{-27}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 10^{+106}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 5
Error16.1
Cost1232
\[\begin{array}{l} t_0 := \frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ t_1 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -4.6 \cdot 10^{-17}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 3.6 \cdot 10^{-67}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.1 \cdot 10^{-29}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 2.8 \cdot 10^{+44}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error16.2
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -2.15 \cdot 10^{-17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 4.6 \cdot 10^{-67}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ \mathbf{elif}\;c \leq 1.45 \cdot 10^{-26}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 8 \cdot 10^{+43}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error16.1
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -9 \cdot 10^{-17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.8 \cdot 10^{-67}:\\ \;\;\;\;\frac{b}{d} + \frac{1}{d} \cdot \left(c \cdot \frac{a}{d}\right)\\ \mathbf{elif}\;c \leq 1.6 \cdot 10^{-29}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 1.35 \cdot 10^{+44}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error19.1
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -1.65 \cdot 10^{+30} \lor \neg \left(c \leq 3.2 \cdot 10^{+106}\right):\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ \end{array} \]
Alternative 9
Error23.5
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -2.7 \cdot 10^{+29}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 2.1 \cdot 10^{+44}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 10
Error38.0
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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