?

Average Accuracy: 59.0% → 83.3%
Time: 13.7s
Precision: binary64
Cost: 20560

?

\[\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)} \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;c \leq -2.1 \cdot 10^{+74}:\\ \;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq -1.5 \cdot 10^{-202}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 4.4 \cdot 10^{-222}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;c \leq 10^{+95}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \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)) (/ (fma a c (* d b)) (hypot c d)))))
   (if (<= c -2.1e+74)
     (* (/ c (hypot c d)) (/ a (hypot c d)))
     (if (<= c -1.5e-202)
       t_0
       (if (<= c 4.4e-222)
         (+ (/ b d) (* (/ c d) (/ a d)))
         (if (<= c 1e+95) t_0 (+ (/ a c) (* (/ d c) (/ b c)))))))))
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)) * (fma(a, c, (d * b)) / hypot(c, d));
	double tmp;
	if (c <= -2.1e+74) {
		tmp = (c / hypot(c, d)) * (a / hypot(c, d));
	} else if (c <= -1.5e-202) {
		tmp = t_0;
	} else if (c <= 4.4e-222) {
		tmp = (b / d) + ((c / d) * (a / d));
	} else if (c <= 1e+95) {
		tmp = t_0;
	} else {
		tmp = (a / c) + ((d / c) * (b / c));
	}
	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(1.0 / hypot(c, d)) * Float64(fma(a, c, Float64(d * b)) / hypot(c, d)))
	tmp = 0.0
	if (c <= -2.1e+74)
		tmp = Float64(Float64(c / hypot(c, d)) * Float64(a / hypot(c, d)));
	elseif (c <= -1.5e-202)
		tmp = t_0;
	elseif (c <= 4.4e-222)
		tmp = Float64(Float64(b / d) + Float64(Float64(c / d) * Float64(a / d)));
	elseif (c <= 1e+95)
		tmp = t_0;
	else
		tmp = Float64(Float64(a / c) + Float64(Float64(d / c) * Float64(b / c)));
	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[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(a * c + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -2.1e+74], N[(N[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(a / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -1.5e-202], t$95$0, If[LessEqual[c, 4.4e-222], N[(N[(b / d), $MachinePrecision] + N[(N[(c / d), $MachinePrecision] * N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 1e+95], t$95$0, N[(N[(a / c), $MachinePrecision] + N[(N[(d / c), $MachinePrecision] * N[(b / c), $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)} \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;c \leq -2.1 \cdot 10^{+74}:\\
\;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;c \leq -1.5 \cdot 10^{-202}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;c \leq 4.4 \cdot 10^{-222}:\\
\;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\

\mathbf{elif}\;c \leq 10^{+95}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\


\end{array}

Error?

Target

Original59.0%
Target99.3%
Herbie83.3%
\[\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 < -2.0999999999999999e74

    1. Initial program 41.9%

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

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

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

      [Start]37.9

      \[ \frac{c \cdot a}{c \cdot c + d \cdot d} \]

      add-sqr-sqrt [=>]37.9

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

      times-frac [=>]43.7

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

      hypot-def [=>]43.7

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

      hypot-def [=>]80.0

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

    if -2.0999999999999999e74 < c < -1.50000000000000005e-202 or 4.4e-222 < c < 1.00000000000000002e95

    1. Initial program 73.0%

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

      \[\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)}} \]
      Proof

      [Start]73.0

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

      *-un-lft-identity [=>]73.0

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

      add-sqr-sqrt [=>]73.0

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

      times-frac [=>]73.0

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

      hypot-def [=>]73.0

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

      fma-def [=>]73.0

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

      hypot-def [=>]82.8

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

    if -1.50000000000000005e-202 < c < 4.4e-222

    1. Initial program 61.2%

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

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}} \]
    3. Simplified89.9%

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

      [Start]86.7

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

      unpow2 [=>]86.7

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

      times-frac [=>]89.9

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

    if 1.00000000000000002e95 < c

    1. Initial program 38.3%

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

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}} \]
    3. Simplified82.8%

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

      [Start]74.1

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

      unpow2 [=>]74.1

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

      times-frac [=>]82.8

      \[ \frac{a}{c} + \color{blue}{\frac{d}{c} \cdot \frac{b}{c}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification83.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -2.1 \cdot 10^{+74}:\\ \;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq -1.5 \cdot 10^{-202}:\\ \;\;\;\;\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}\;c \leq 4.4 \cdot 10^{-222}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;c \leq 10^{+95}:\\ \;\;\;\;\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{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy78.1%
Cost17489
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_0 \leq -5 \cdot 10^{+269}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;t_0 \leq -4 \cdot 10^{-281} \lor \neg \left(t_0 \leq 0\right) \land t_0 \leq 10^{+303}:\\ \;\;\;\;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
Accuracy82.1%
Cost7696
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -6.6 \cdot 10^{+66}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq -2.7 \cdot 10^{-152}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.85 \cdot 10^{-142}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;d \leq 2.4 \cdot 10^{+91}:\\ \;\;\;\;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
Accuracy81.9%
Cost1488
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{if}\;d \leq -3.1 \cdot 10^{+66}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -2.5 \cdot 10^{-152}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.52 \cdot 10^{-142}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;d \leq 4.1 \cdot 10^{+89}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Accuracy76.1%
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{if}\;d \leq -3600000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{-62}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;d \leq 4.15 \cdot 10^{-17}:\\ \;\;\;\;\frac{d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 2.4 \cdot 10^{-5}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Accuracy74.3%
Cost1228
\[\begin{array}{l} \mathbf{if}\;c \leq -1.02 \cdot 10^{+21}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;c \leq 2.25 \cdot 10^{-77}:\\ \;\;\;\;\frac{b}{d} + \frac{1}{\frac{d}{c \cdot \frac{a}{d}}}\\ \mathbf{elif}\;c \leq 2.2 \cdot 10^{+94}:\\ \;\;\;\;\frac{c}{\frac{c}{\frac{a}{c}} + \frac{d}{\frac{a}{d}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \end{array} \]
Alternative 6
Accuracy73.6%
Cost1100
\[\begin{array}{l} \mathbf{if}\;c \leq -2.55 \cdot 10^{+20}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;c \leq 2.3 \cdot 10^{-77}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;c \leq 9 \cdot 10^{+93}:\\ \;\;\;\;\frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \end{array} \]
Alternative 7
Accuracy69.3%
Cost977
\[\begin{array}{l} \mathbf{if}\;d \leq -2.5 \cdot 10^{+56}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 0.000125:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq 3.9 \cdot 10^{+157} \lor \neg \left(d \leq 2 \cdot 10^{+199}\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;c \cdot \frac{\frac{a}{d}}{d}\\ \end{array} \]
Alternative 8
Accuracy69.3%
Cost977
\[\begin{array}{l} \mathbf{if}\;d \leq -1.25 \cdot 10^{+57}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 3.9 \cdot 10^{-5}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{+157} \lor \neg \left(d \leq 9.4 \cdot 10^{+198}\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;c \cdot \frac{\frac{a}{d}}{d}\\ \end{array} \]
Alternative 9
Accuracy63.5%
Cost976
\[\begin{array}{l} \mathbf{if}\;d \leq -2.1 \cdot 10^{+57}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.1 \cdot 10^{-6}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq 4.5 \cdot 10^{+156}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 9.2 \cdot 10^{+198}:\\ \;\;\;\;c \cdot \frac{\frac{a}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 10
Accuracy76.2%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -26000000000 \lor \neg \left(d \leq 6 \cdot 10^{-7}\right):\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \end{array} \]
Alternative 11
Accuracy63.8%
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -6 \cdot 10^{+24}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 2.3 \cdot 10^{-77}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 12
Accuracy42.4%
Cost324
\[\begin{array}{l} \mathbf{if}\;d \leq 3.05 \cdot 10^{+172}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{d}\\ \end{array} \]
Alternative 13
Accuracy41.3%
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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