?

Average Error: 26.2 → 4.1
Time: 13.6s
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{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\\ \mathbf{if}\;c \leq -8.5 \cdot 10^{+151}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq 3.3 \cdot 10^{+92}:\\ \;\;\;\;t_0 \cdot \left(t_1 + \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 (/ b (/ (hypot c d) d))))
   (if (<= c -8.5e+151)
     (+ (/ a c) (/ (* b (/ d c)) c))
     (if (<= c 3.3e+92)
       (* t_0 (+ t_1 (/ (* 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 = b / (hypot(c, d) / d);
	double tmp;
	if (c <= -8.5e+151) {
		tmp = (a / c) + ((b * (d / c)) / c);
	} else if (c <= 3.3e+92) {
		tmp = t_0 * (t_1 + ((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 = b / (Math.hypot(c, d) / d);
	double tmp;
	if (c <= -8.5e+151) {
		tmp = (a / c) + ((b * (d / c)) / c);
	} else if (c <= 3.3e+92) {
		tmp = t_0 * (t_1 + ((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 = b / (math.hypot(c, d) / d)
	tmp = 0
	if c <= -8.5e+151:
		tmp = (a / c) + ((b * (d / c)) / c)
	elif c <= 3.3e+92:
		tmp = t_0 * (t_1 + ((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(b / Float64(hypot(c, d) / d))
	tmp = 0.0
	if (c <= -8.5e+151)
		tmp = Float64(Float64(a / c) + Float64(Float64(b * Float64(d / c)) / c));
	elseif (c <= 3.3e+92)
		tmp = Float64(t_0 * Float64(t_1 + 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 = b / (hypot(c, d) / d);
	tmp = 0.0;
	if (c <= -8.5e+151)
		tmp = (a / c) + ((b * (d / c)) / c);
	elseif (c <= 3.3e+92)
		tmp = t_0 * (t_1 + ((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[(b / N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -8.5e+151], N[(N[(a / c), $MachinePrecision] + N[(N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 3.3e+92], N[(t$95$0 * N[(t$95$1 + 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{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\\
\mathbf{if}\;c \leq -8.5 \cdot 10^{+151}:\\
\;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\

\mathbf{elif}\;c \leq 3.3 \cdot 10^{+92}:\\
\;\;\;\;t_0 \cdot \left(t_1 + \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.2
Target0.5
Herbie4.1
\[\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 < -8.50000000000000051e151

    1. Initial program 46.1

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

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

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

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

      [Start]15.1

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

      unpow2 [=>]15.1

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

      associate-/r* [=>]12.2

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

      associate-*l/ [<=]7.6

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

      *-commutative [=>]7.6

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

    if -8.50000000000000051e151 < c < 3.29999999999999974e92

    1. Initial program 18.7

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

      \[\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]12.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 [=>]12.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* [=>]3.1

      \[ \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 [<=]3.1

      \[ \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 3.29999999999999974e92 < c

    1. Initial program 39.6

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

      \[\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]27.4

      \[ \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 [=>]27.4

      \[ \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* [=>]22.4

      \[ \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 [<=]22.4

      \[ \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) \]
    5. Taylor expanded in c around inf 5.5

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

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

Alternatives

Alternative 1
Error9.1
Cost20432
\[\begin{array}{l} t_0 := \frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;c \leq -1.45 \cdot 10^{+158}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq -5 \cdot 10^{-145}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.65 \cdot 10^{-196}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \mathbf{elif}\;c \leq 1.2 \cdot 10^{+38}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a + \frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right)\\ \end{array} \]
Alternative 2
Error12.6
Cost14028
\[\begin{array}{l} \mathbf{if}\;c \leq -5.2 \cdot 10^{+77}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \mathbf{elif}\;c \leq -7.8 \cdot 10^{-67}:\\ \;\;\;\;\frac{b \cdot d + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 1.12 \cdot 10^{-222}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a + \frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right)\\ \end{array} \]
Alternative 3
Error12.1
Cost8080
\[\begin{array}{l} t_0 := \frac{b \cdot d + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -4.2 \cdot 10^{+78}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \mathbf{elif}\;c \leq -3.9 \cdot 10^{-67}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.9 \cdot 10^{-122}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \mathbf{elif}\;c \leq 4.2 \cdot 10^{+113}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a + \frac{b}{\frac{c}{d} + \frac{d}{c} \cdot 0.5}\right)\\ \end{array} \]
Alternative 4
Error12.2
Cost1488
\[\begin{array}{l} t_0 := \frac{b \cdot d + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -5.5 \cdot 10^{+78}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \mathbf{elif}\;c \leq -3.9 \cdot 10^{-67}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.06 \cdot 10^{-122}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \mathbf{elif}\;c \leq 5.1 \cdot 10^{+92}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ \end{array} \]
Alternative 5
Error16.4
Cost1234
\[\begin{array}{l} \mathbf{if}\;d \leq -1.2 \cdot 10^{+30} \lor \neg \left(d \leq 3.5 \cdot 10^{-65} \lor \neg \left(d \leq 5.5 \cdot 10^{+39}\right) \land d \leq 7 \cdot 10^{+76}\right):\\ \;\;\;\;\frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \end{array} \]
Alternative 6
Error15.7
Cost1234
\[\begin{array}{l} \mathbf{if}\;d \leq -1.95 \cdot 10^{+30} \lor \neg \left(d \leq 3.5 \cdot 10^{-65} \lor \neg \left(d \leq 7.5 \cdot 10^{+39}\right) \land d \leq 8.4 \cdot 10^{+76}\right):\\ \;\;\;\;\frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ \end{array} \]
Alternative 7
Error15.8
Cost1234
\[\begin{array}{l} \mathbf{if}\;d \leq -1.1 \cdot 10^{+30} \lor \neg \left(d \leq 3.5 \cdot 10^{-65} \lor \neg \left(d \leq 3.8 \cdot 10^{+39}\right) \land d \leq 7 \cdot 10^{+76}\right):\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ \end{array} \]
Alternative 8
Error15.9
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ t_1 := \frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{if}\;d \leq -1.25 \cdot 10^{+30}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 3.5 \cdot 10^{-65}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.5 \cdot 10^{+39}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\ \mathbf{elif}\;d \leq 1.12 \cdot 10^{+77}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 9
Error15.9
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ \mathbf{if}\;d \leq -1.9 \cdot 10^{+30}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq 3.5 \cdot 10^{-65}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 7 \cdot 10^{+39}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\ \mathbf{elif}\;d \leq 1.1 \cdot 10^{+77}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \end{array} \]
Alternative 10
Error19.8
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -1.8 \cdot 10^{+54}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 2.95 \cdot 10^{+125}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 11
Error24.4
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -0.46:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 2.8 \cdot 10^{+125}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 12
Error37.5
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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