?

Average Error: 26.4 → 5.0
Time: 13.8s
Precision: binary64
Cost: 20616

?

\[\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{d}{\frac{c}{b}}\\ \mathbf{if}\;c \leq -6.8 \cdot 10^{+104}:\\ \;\;\;\;\frac{a}{c} + \frac{t_1}{c}\\ \mathbf{elif}\;c \leq 2.15 \cdot 10^{+192}:\\ \;\;\;\;t_0 \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + \frac{c \cdot a}{\mathsf{hypot}\left(c, d\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(a + t_1\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 (/ d (/ c b))))
   (if (<= c -6.8e+104)
     (+ (/ a c) (/ t_1 c))
     (if (<= c 2.15e+192)
       (* t_0 (+ (/ b (/ (hypot c d) d)) (/ (* c a) (hypot c d))))
       (* t_0 (+ a t_1))))))
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 = d / (c / b);
	double tmp;
	if (c <= -6.8e+104) {
		tmp = (a / c) + (t_1 / c);
	} else if (c <= 2.15e+192) {
		tmp = t_0 * ((b / (hypot(c, d) / d)) + ((c * a) / hypot(c, d)));
	} else {
		tmp = t_0 * (a + t_1);
	}
	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 = d / (c / b);
	double tmp;
	if (c <= -6.8e+104) {
		tmp = (a / c) + (t_1 / c);
	} else if (c <= 2.15e+192) {
		tmp = t_0 * ((b / (Math.hypot(c, d) / d)) + ((c * a) / Math.hypot(c, d)));
	} else {
		tmp = t_0 * (a + t_1);
	}
	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 = 1.0 / math.hypot(c, d)
	t_1 = d / (c / b)
	tmp = 0
	if c <= -6.8e+104:
		tmp = (a / c) + (t_1 / c)
	elif c <= 2.15e+192:
		tmp = t_0 * ((b / (math.hypot(c, d) / d)) + ((c * a) / math.hypot(c, d)))
	else:
		tmp = t_0 * (a + t_1)
	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(d / Float64(c / b))
	tmp = 0.0
	if (c <= -6.8e+104)
		tmp = Float64(Float64(a / c) + Float64(t_1 / c));
	elseif (c <= 2.15e+192)
		tmp = Float64(t_0 * Float64(Float64(b / Float64(hypot(c, d) / d)) + Float64(Float64(c * a) / hypot(c, d))));
	else
		tmp = Float64(t_0 * Float64(a + t_1));
	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 = 1.0 / hypot(c, d);
	t_1 = d / (c / b);
	tmp = 0.0;
	if (c <= -6.8e+104)
		tmp = (a / c) + (t_1 / c);
	elseif (c <= 2.15e+192)
		tmp = t_0 * ((b / (hypot(c, d) / d)) + ((c * a) / hypot(c, d)));
	else
		tmp = t_0 * (a + t_1);
	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[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(d / N[(c / b), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -6.8e+104], N[(N[(a / c), $MachinePrecision] + N[(t$95$1 / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.15e+192], N[(t$95$0 * N[(N[(b / N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] + N[(N[(c * a), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[(a + t$95$1), $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{d}{\frac{c}{b}}\\
\mathbf{if}\;c \leq -6.8 \cdot 10^{+104}:\\
\;\;\;\;\frac{a}{c} + \frac{t_1}{c}\\

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

\mathbf{else}:\\
\;\;\;\;t_0 \cdot \left(a + t_1\right)\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.4
Target0.4
Herbie5.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 3 regimes
  2. if c < -6.7999999999999994e104

    1. Initial program 39.5

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

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

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

      [Start]15.2

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

      *-commutative [<=]15.2

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

      unpow2 [=>]15.2

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

      associate-/l* [=>]14.4

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

      associate-/r/ [=>]13.4

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

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

    if -6.7999999999999994e104 < c < 2.14999999999999988e192

    1. Initial program 20.9

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

      \[\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. Simplified4.2

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

      [Start]13.0

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

      +-commutative [=>]13.0

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

      associate-/l* [=>]4.2

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

      *-commutative [<=]4.2

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

    if 2.14999999999999988e192 < c

    1. Initial program 43.4

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

      \[\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. Taylor expanded in c around inf 12.2

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

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

      [Start]12.2

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

      associate-/l* [=>]5.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -6.8 \cdot 10^{+104}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{elif}\;c \leq 2.15 \cdot 10^{+192}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + \frac{c \cdot a}{\mathsf{hypot}\left(c, d\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a + \frac{d}{\frac{c}{b}}\right)\\ \end{array} \]

Alternatives

Alternative 1
Error9.5
Cost21060
\[\begin{array}{l} t_0 := \frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_0 \leq 5 \cdot 10^{+307}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;t_0 \leq \infty:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{c}{\frac{d}{c}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \end{array} \]
Alternative 2
Error11.7
Cost7696
\[\begin{array}{l} t_0 := \frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -1.5 \cdot 10^{+147}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq -1.2 \cdot 10^{-148}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.9 \cdot 10^{-108}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + b \cdot \frac{d}{c}\right)\\ \mathbf{elif}\;d \leq 4.6 \cdot 10^{+85}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(b + \frac{c}{\frac{d}{a}}\right)\\ \end{array} \]
Alternative 3
Error11.6
Cost7696
\[\begin{array}{l} t_0 := \frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ t_1 := b + \frac{c}{\frac{d}{a}}\\ \mathbf{if}\;d \leq -4.5 \cdot 10^{+68}:\\ \;\;\;\;t_1 \cdot \frac{-1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq -1.6 \cdot 10^{-153}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.5 \cdot 10^{-108}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + b \cdot \frac{d}{c}\right)\\ \mathbf{elif}\;d \leq 6 \cdot 10^{+95}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot t_1\\ \end{array} \]
Alternative 4
Error11.8
Cost1488
\[\begin{array}{l} t_0 := \frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ t_1 := \frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{if}\;d \leq -7.5 \cdot 10^{+146}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1.5 \cdot 10^{-153}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.9 \cdot 10^{-108}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + b \cdot \frac{d}{c}\right)\\ \mathbf{elif}\;d \leq 5.8 \cdot 10^{+95}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Error18.5
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -2.9 \cdot 10^{-33} \lor \neg \left(c \leq 1.35 \cdot 10^{-27}\right):\\ \;\;\;\;\frac{1}{c} \cdot \left(a + b \cdot \frac{d}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 6
Error15.1
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -2.25 \cdot 10^{+39} \lor \neg \left(c \leq 24000\right):\\ \;\;\;\;\frac{1}{c} \cdot \left(a + d \cdot \frac{b}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(b + \frac{c}{\frac{d}{a}}\right) \cdot \frac{1}{d}\\ \end{array} \]
Alternative 7
Error15.0
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -2.3 \cdot 10^{+39} \lor \neg \left(c \leq 450\right):\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\left(b + \frac{c}{\frac{d}{a}}\right) \cdot \frac{1}{d}\\ \end{array} \]
Alternative 8
Error15.2
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -8 \cdot 10^{+38} \lor \neg \left(c \leq 7200\right):\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \end{array} \]
Alternative 9
Error18.4
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -2.7 \cdot 10^{-31}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + d \cdot \frac{b}{c}\right)\\ \mathbf{elif}\;c \leq 5.2 \cdot 10^{-27}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + b \cdot \frac{d}{c}\right)\\ \end{array} \]
Alternative 10
Error22.7
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -2.3 \cdot 10^{+22}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 3.8:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 11
Error37.5
Cost324
\[\begin{array}{l} \mathbf{if}\;d \leq 9.5 \cdot 10^{+138}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{d}\\ \end{array} \]
Alternative 12
Error37.6
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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