?

Average Error: 40.76% → 14.75%
Time: 14.7s
Precision: binary64
Cost: 20432

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;d \leq -3.4 \cdot 10^{+103}:\\ \;\;\;\;\left(b + \frac{c}{\frac{d}{a}}\right) \cdot \frac{-1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq -1.02 \cdot 10^{-196}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.15 \cdot 10^{-181}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{c}{\frac{d}{c}}}\\ \mathbf{elif}\;d \leq 7 \cdot 10^{+89}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \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 (/ (/ (fma a c (* d b)) (hypot c d)) (hypot c d))))
   (if (<= d -3.4e+103)
     (* (+ b (/ c (/ d a))) (/ -1.0 (hypot c d)))
     (if (<= d -1.02e-196)
       t_0
       (if (<= d 2.15e-181)
         (+ (/ a c) (/ b (/ c (/ d c))))
         (if (<= d 7e+89) t_0 (+ (/ b d) (/ (* c (/ a d)) 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 = (fma(a, c, (d * b)) / hypot(c, d)) / hypot(c, d);
	double tmp;
	if (d <= -3.4e+103) {
		tmp = (b + (c / (d / a))) * (-1.0 / hypot(c, d));
	} else if (d <= -1.02e-196) {
		tmp = t_0;
	} else if (d <= 2.15e-181) {
		tmp = (a / c) + (b / (c / (d / c)));
	} else if (d <= 7e+89) {
		tmp = t_0;
	} else {
		tmp = (b / d) + ((c * (a / d)) / 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(fma(a, c, Float64(d * b)) / hypot(c, d)) / hypot(c, d))
	tmp = 0.0
	if (d <= -3.4e+103)
		tmp = Float64(Float64(b + Float64(c / Float64(d / a))) * Float64(-1.0 / hypot(c, d)));
	elseif (d <= -1.02e-196)
		tmp = t_0;
	elseif (d <= 2.15e-181)
		tmp = Float64(Float64(a / c) + Float64(b / Float64(c / Float64(d / c))));
	elseif (d <= 7e+89)
		tmp = t_0;
	else
		tmp = Float64(Float64(b / d) + Float64(Float64(c * Float64(a / d)) / d));
	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[(N[(a * c + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -3.4e+103], N[(N[(b + N[(c / N[(d / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(-1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -1.02e-196], t$95$0, If[LessEqual[d, 2.15e-181], N[(N[(a / c), $MachinePrecision] + N[(b / N[(c / N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 7e+89], t$95$0, N[(N[(b / d), $MachinePrecision] + N[(N[(c * N[(a / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;d \leq -3.4 \cdot 10^{+103}:\\
\;\;\;\;\left(b + \frac{c}{\frac{d}{a}}\right) \cdot \frac{-1}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;d \leq -1.02 \cdot 10^{-196}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;d \leq 7 \cdot 10^{+89}:\\
\;\;\;\;t_0\\

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


\end{array}

Error?

Target

Original40.76%
Target0.62%
Herbie14.75%
\[\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 d < -3.3999999999999998e103

    1. Initial program 63.05

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

      \[\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 d around -inf 20.47

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

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

      [Start]20.47

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

      distribute-lft-out [=>]20.47

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

      associate-/l* [=>]14.48

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

    if -3.3999999999999998e103 < d < -1.0200000000000001e-196 or 2.15e-181 < d < 7.0000000000000001e89

    1. Initial program 25.2

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

      \[\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-rr16.5

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

    if -1.0200000000000001e-196 < d < 2.15e-181

    1. Initial program 37.69

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

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

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

      [Start]14.03

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

      *-commutative [<=]14.03

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

      associate-/l* [=>]16.27

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

      associate-/r/ [=>]19.88

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

      unpow2 [=>]19.88

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

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

    if 7.0000000000000001e89 < d

    1. Initial program 60.07

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

      \[\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 0 21.95

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

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

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

      [Start]26.81

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

      unpow2 [=>]26.81

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

      associate-/r* [=>]23.01

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

      associate-*r/ [<=]16.34

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.4 \cdot 10^{+103}:\\ \;\;\;\;\left(b + \frac{c}{\frac{d}{a}}\right) \cdot \frac{-1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq -1.02 \cdot 10^{-196}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq 2.15 \cdot 10^{-181}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{c}{\frac{d}{c}}}\\ \mathbf{elif}\;d \leq 7 \cdot 10^{+89}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \end{array} \]

Alternatives

Alternative 1
Error21.02%
Cost14300
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -2.9 \cdot 10^{+94}:\\ \;\;\;\;\left(b + \frac{c}{\frac{d}{a}}\right) \cdot \frac{-1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq -5.3 \cdot 10^{-82}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.1 \cdot 10^{-161}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d \cdot b}{c}}{c}\\ \mathbf{elif}\;d \leq 1.16 \cdot 10^{-97}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.6 \cdot 10^{-34}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq 1.15 \cdot 10^{+77}:\\ \;\;\;\;\frac{\frac{c}{\frac{\mathsf{hypot}\left(d, c\right)}{a}}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq 3.7 \cdot 10^{+102}:\\ \;\;\;\;\frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \end{array} \]
Alternative 2
Error21%
Cost7300
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -1.75 \cdot 10^{+98}:\\ \;\;\;\;\left(b + \frac{c}{\frac{d}{a}}\right) \cdot \frac{-1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq -9.2 \cdot 10^{-82}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.1 \cdot 10^{-161}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d \cdot b}{c}}{c}\\ \mathbf{elif}\;d \leq 5.2 \cdot 10^{-98}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.8 \cdot 10^{+67}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \end{array} \]
Alternative 3
Error21.24%
Cost1488
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -5.2 \cdot 10^{+118}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq -7.2 \cdot 10^{-82}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.1 \cdot 10^{-161}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d \cdot b}{c}}{c}\\ \mathbf{elif}\;d \leq 1.06 \cdot 10^{-97}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.1 \cdot 10^{+67}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \end{array} \]
Alternative 4
Error30.52%
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ t_1 := \frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \mathbf{if}\;d \leq -2.8 \cdot 10^{+162}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -2.4 \cdot 10^{+72}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -2.8 \cdot 10^{+19}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 4.3 \cdot 10^{+67}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Error30.98%
Cost1232
\[\begin{array}{l} t_0 := \frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \mathbf{if}\;d \leq -2.8 \cdot 10^{+162}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -3.8 \cdot 10^{+72}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq -4.5 \cdot 10^{+28}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 4.8 \cdot 10^{+62}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{c}{\frac{d}{c}}}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error28.59%
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{if}\;d \leq -2.8 \cdot 10^{+162}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -3.5 \cdot 10^{+72}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq -1.65 \cdot 10^{+28}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.15 \cdot 10^{+61}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{c}{\frac{d}{c}}}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error28.55%
Cost1232
\[\begin{array}{l} \mathbf{if}\;d \leq -2.8 \cdot 10^{+162}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq -8.2 \cdot 10^{+71}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq -2.9 \cdot 10^{+28}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 2.5 \cdot 10^{+61}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{c}{\frac{d}{c}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \end{array} \]
Alternative 8
Error28.24%
Cost1232
\[\begin{array}{l} \mathbf{if}\;d \leq -2.8 \cdot 10^{+162}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq -2.8 \cdot 10^{+72}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq -4.2 \cdot 10^{+24}:\\ \;\;\;\;\frac{d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 1.22 \cdot 10^{+61}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{c}{\frac{d}{c}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \end{array} \]
Alternative 9
Error29.33%
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -7.8 \cdot 10^{+93} \lor \neg \left(c \leq 1.9 \cdot 10^{+23}\right):\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \end{array} \]
Alternative 10
Error39.31%
Cost722
\[\begin{array}{l} \mathbf{if}\;d \leq -2.8 \cdot 10^{+162} \lor \neg \left(d \leq -2.2 \cdot 10^{+72}\right) \land \left(d \leq -8.6 \cdot 10^{+19} \lor \neg \left(d \leq 1.7 \cdot 10^{+77}\right)\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 11
Error58.76%
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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