?

Average Accuracy: 58.0% → 83.9%
Time: 13.1s
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, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{if}\;c \leq -5.7 \cdot 10^{+181}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -3.2 \cdot 10^{-147}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-126}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{\frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 5 \cdot 10^{+44}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \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 (* b d)) (hypot c d))))
        (t_1 (+ (/ a c) (* (/ b c) (/ d c)))))
   (if (<= c -5.7e+181)
     t_1
     (if (<= c -3.2e-147)
       t_0
       (if (<= c 1e-126)
         (+ (/ b d) (* a (/ (/ c d) d)))
         (if (<= c 5e+44) t_0 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)) * (fma(a, c, (b * d)) / hypot(c, d));
	double t_1 = (a / c) + ((b / c) * (d / c));
	double tmp;
	if (c <= -5.7e+181) {
		tmp = t_1;
	} else if (c <= -3.2e-147) {
		tmp = t_0;
	} else if (c <= 1e-126) {
		tmp = (b / d) + (a * ((c / d) / d));
	} else if (c <= 5e+44) {
		tmp = t_0;
	} else {
		tmp = 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(Float64(1.0 / hypot(c, d)) * Float64(fma(a, c, Float64(b * d)) / hypot(c, d)))
	t_1 = Float64(Float64(a / c) + Float64(Float64(b / c) * Float64(d / c)))
	tmp = 0.0
	if (c <= -5.7e+181)
		tmp = t_1;
	elseif (c <= -3.2e-147)
		tmp = t_0;
	elseif (c <= 1e-126)
		tmp = Float64(Float64(b / d) + Float64(a * Float64(Float64(c / d) / d)));
	elseif (c <= 5e+44)
		tmp = t_0;
	else
		tmp = t_1;
	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[(b * d), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(a / c), $MachinePrecision] + N[(N[(b / c), $MachinePrecision] * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -5.7e+181], t$95$1, If[LessEqual[c, -3.2e-147], t$95$0, If[LessEqual[c, 1e-126], N[(N[(b / d), $MachinePrecision] + N[(a * N[(N[(c / d), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 5e+44], t$95$0, t$95$1]]]]]]
\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, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}\\
t_1 := \frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\
\mathbf{if}\;c \leq -5.7 \cdot 10^{+181}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;c \leq -3.2 \cdot 10^{-147}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;c \leq 5 \cdot 10^{+44}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}

Error?

Target

Original58.0%
Target99.4%
Herbie83.9%
\[\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 < -5.7000000000000002e181 or 4.9999999999999996e44 < c

    1. Initial program 40.0%

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

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

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

      [Start]74.9

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

      *-commutative [<=]74.9

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

      unpow2 [=>]74.9

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

      times-frac [=>]83.9

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

    if -5.7000000000000002e181 < c < -3.19999999999999979e-147 or 9.9999999999999995e-127 < c < 4.9999999999999996e44

    1. Initial program 70.4%

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

      [Start]70.4

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

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

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

      add-sqr-sqrt [=>]70.4

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

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

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

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

      \[ \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 -3.19999999999999979e-147 < c < 9.9999999999999995e-127

    1. Initial program 62.2%

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

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

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

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

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

      add-sqr-sqrt [=>]62.2

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

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

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

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

      \[ \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)}} \]
    3. Applied egg-rr79.0%

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

      [Start]79.1

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

      clear-num [=>]79.0

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

      un-div-inv [=>]79.0

      \[ \color{blue}{\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}} \]
    4. Taylor expanded in c around 0 84.7%

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

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

      [Start]84.7

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

      associate-/l* [=>]79.5

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

      associate-/r/ [=>]83.4

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

      unpow2 [=>]83.4

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

      associate-/r* [=>]89.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -5.7 \cdot 10^{+181}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -3.2 \cdot 10^{-147}:\\ \;\;\;\;\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)}\\ \mathbf{elif}\;c \leq 10^{-126}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{\frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 5 \cdot 10^{+44}:\\ \;\;\;\;\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)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy80.4%
Cost1488
\[\begin{array}{l} t_0 := \frac{b \cdot d + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -1.25 \cdot 10^{+145}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-137}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.3 \cdot 10^{-49}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;c \leq 4.4 \cdot 10^{+44}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 2
Accuracy75.9%
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{if}\;d \leq -41000000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{-37}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{elif}\;d \leq 7.2 \cdot 10^{+56}:\\ \;\;\;\;\frac{b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 3.3 \cdot 10^{+80}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Accuracy66.2%
Cost1100
\[\begin{array}{l} \mathbf{if}\;c \leq -37000:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 7.5 \cdot 10^{-114}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{+120}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 4
Accuracy71.5%
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -4.8 \lor \neg \left(c \leq 6.3 \cdot 10^{-49}\right):\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 5
Accuracy71.9%
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -0.122 \lor \neg \left(c \leq 7.2 \cdot 10^{-49}\right):\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 6
Accuracy76.3%
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -7.2 \cdot 10^{+18} \lor \neg \left(c \leq 6.6 \cdot 10^{-49}\right):\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \end{array} \]
Alternative 7
Accuracy65.3%
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -200:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 6.2 \cdot 10^{-49}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 8
Accuracy41.9%
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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