?

Average Error: 41.47% → 15.65%
Time: 12.3s
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)}\\ \mathbf{if}\;c \leq -1.5 \cdot 10^{+80}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{elif}\;c \leq -5.8 \cdot 10^{-190}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.8 \cdot 10^{-125}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{\frac{d}{c}}\right)\\ \mathbf{elif}\;c \leq 3.3 \cdot 10^{+72}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{c}}{\frac{c}{b}}\\ \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)))))
   (if (<= c -1.5e+80)
     (+ (/ a c) (/ (* (/ b c) d) c))
     (if (<= c -5.8e-190)
       t_0
       (if (<= c 5.8e-125)
         (* (/ 1.0 d) (+ b (/ a (/ d c))))
         (if (<= c 3.3e+72) t_0 (+ (/ a c) (/ (/ d c) (/ c b)))))))))
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 tmp;
	if (c <= -1.5e+80) {
		tmp = (a / c) + (((b / c) * d) / c);
	} else if (c <= -5.8e-190) {
		tmp = t_0;
	} else if (c <= 5.8e-125) {
		tmp = (1.0 / d) * (b + (a / (d / c)));
	} else if (c <= 3.3e+72) {
		tmp = t_0;
	} else {
		tmp = (a / c) + ((d / c) / (c / b));
	}
	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)))
	tmp = 0.0
	if (c <= -1.5e+80)
		tmp = Float64(Float64(a / c) + Float64(Float64(Float64(b / c) * d) / c));
	elseif (c <= -5.8e-190)
		tmp = t_0;
	elseif (c <= 5.8e-125)
		tmp = Float64(Float64(1.0 / d) * Float64(b + Float64(a / Float64(d / c))));
	elseif (c <= 3.3e+72)
		tmp = t_0;
	else
		tmp = Float64(Float64(a / c) + Float64(Float64(d / c) / Float64(c / b)));
	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]}, If[LessEqual[c, -1.5e+80], N[(N[(a / c), $MachinePrecision] + N[(N[(N[(b / c), $MachinePrecision] * d), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -5.8e-190], t$95$0, If[LessEqual[c, 5.8e-125], N[(N[(1.0 / d), $MachinePrecision] * N[(b + N[(a / N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 3.3e+72], t$95$0, N[(N[(a / c), $MachinePrecision] + N[(N[(d / c), $MachinePrecision] / N[(c / b), $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, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;c \leq -1.5 \cdot 10^{+80}:\\
\;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\

\mathbf{elif}\;c \leq -5.8 \cdot 10^{-190}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;c \leq 5.8 \cdot 10^{-125}:\\
\;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{\frac{d}{c}}\right)\\

\mathbf{elif}\;c \leq 3.3 \cdot 10^{+72}:\\
\;\;\;\;t_0\\

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


\end{array}

Error?

Target

Original41.47%
Target0.75%
Herbie15.65%
\[\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 < -1.49999999999999993e80

    1. Initial program 60.16

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

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

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

      [Start]28.07

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

      *-commutative [<=]28.07

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

      unpow2 [=>]28.07

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

      times-frac [=>]18.81

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

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

    if -1.49999999999999993e80 < c < -5.8000000000000004e-190 or 5.8000000000000004e-125 < c < 3.3e72

    1. Initial program 24.89

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

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

    if -5.8000000000000004e-190 < c < 5.8000000000000004e-125

    1. Initial program 37.47

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

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

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

      [Start]16.45

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

      *-commutative [<=]16.45

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

      unpow2 [=>]16.45

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

      times-frac [=>]13.02

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

      \[\leadsto \color{blue}{\frac{1}{d} \cdot \left(\frac{a}{d} \cdot c + b\right)} \]
    5. Applied egg-rr10.73

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

    if 3.3e72 < c

    1. Initial program 58.32

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

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

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

      [Start]25.7

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

      *-commutative [<=]25.7

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

      unpow2 [=>]25.7

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

      times-frac [=>]17.83

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

      \[\leadsto \frac{a}{c} + \color{blue}{\frac{\frac{d}{c}}{\frac{c}{b}}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification15.65

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -1.5 \cdot 10^{+80}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{elif}\;c \leq -5.8 \cdot 10^{-190}:\\ \;\;\;\;\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 5.8 \cdot 10^{-125}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{\frac{d}{c}}\right)\\ \mathbf{elif}\;c \leq 3.3 \cdot 10^{+72}:\\ \;\;\;\;\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{\frac{d}{c}}{\frac{c}{b}}\\ \end{array} \]

Alternatives

Alternative 1
Error19.02%
Cost1752
\[\begin{array}{l} t_0 := \frac{b \cdot d + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -3.3 \cdot 10^{+80}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{elif}\;c \leq -1.12 \cdot 10^{-122}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 9 \cdot 10^{-125}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{\frac{d}{c}}\right)\\ \mathbf{elif}\;c \leq 5.5 \cdot 10^{-19}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 640000000000:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 2.7 \cdot 10^{+72}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{c}}{\frac{c}{b}}\\ \end{array} \]
Alternative 2
Error25.08%
Cost1233
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{if}\;c \leq -1.5 \cdot 10^{+64}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.8 \cdot 10^{-100}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{\frac{d}{c}}\right)\\ \mathbf{elif}\;c \leq 2.5 \cdot 10^{-75} \lor \neg \left(c \leq 980000000000\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ \end{array} \]
Alternative 3
Error24.91%
Cost1233
\[\begin{array}{l} \mathbf{if}\;c \leq -1.06 \cdot 10^{+65}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{elif}\;c \leq 2.8 \cdot 10^{-100}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{\frac{d}{c}}\right)\\ \mathbf{elif}\;c \leq 1.22 \cdot 10^{-75} \lor \neg \left(c \leq 900000000000\right):\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ \end{array} \]
Alternative 4
Error24.86%
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -1.45 \cdot 10^{+64}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{elif}\;c \leq 8 \cdot 10^{-101}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{\frac{d}{c}}\right)\\ \mathbf{elif}\;c \leq 1.22 \cdot 10^{-75}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq 580000000000:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{c}}{\frac{c}{b}}\\ \end{array} \]
Alternative 5
Error24.9%
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -1.06 \cdot 10^{+65}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{elif}\;c \leq 2.2 \cdot 10^{-101}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{\frac{d}{c}}\right)\\ \mathbf{elif}\;c \leq 1.36 \cdot 10^{-76}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq 700000000000:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{c}}{\frac{c}{b}}\\ \end{array} \]
Alternative 6
Error25.28%
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -1.45 \cdot 10^{+65}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c} \cdot d}{c}\\ \mathbf{elif}\;c \leq 5.3 \cdot 10^{-99}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{\frac{d}{c}}\right)\\ \mathbf{elif}\;c \leq 2.6 \cdot 10^{-21}:\\ \;\;\;\;\frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 1450000000000:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{c}}{\frac{c}{b}}\\ \end{array} \]
Alternative 7
Error29.2%
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -1.65 \cdot 10^{+65} \lor \neg \left(c \leq 1.4 \cdot 10^{+14}\right):\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{a}{\frac{d}{c}}\right)\\ \end{array} \]
Alternative 8
Error29.52%
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -3.8 \cdot 10^{+66}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 36000000000000:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + c \cdot \frac{a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 9
Error36.12%
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -9.2 \cdot 10^{+82}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 2.8 \cdot 10^{+29}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 10
Error58.21%
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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