Average Error: 9.4 → 0.1
Time: 2.9s
Precision: binary64
\[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
\[\begin{array}{l} t_0 := x \cdot \left(1 + x\right)\\ t_1 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\ t_2 := \frac{t_0 + \left(x + -1\right) \cdot \left(x + 2 \cdot \left(-1 - x\right)\right)}{t_0 \cdot \left(x + -1\right)}\\ \mathbf{if}\;t_1 \leq -0.5:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 2 \cdot 10^{-18}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(2, {x}^{-5}, \mathsf{fma}\left(2, {x}^{-3}, \mathsf{fma}\left(2, {x}^{-7}, 2 \cdot {x}^{-9}\right)\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
(FPCore (x)
 :precision binary64
 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (* x (+ 1.0 x)))
        (t_1 (+ (+ (/ 1.0 (+ 1.0 x)) (/ -2.0 x)) (/ 1.0 (+ x -1.0))))
        (t_2
         (/
          (+ t_0 (* (+ x -1.0) (+ x (* 2.0 (- -1.0 x)))))
          (* t_0 (+ x -1.0)))))
   (if (<= t_1 -0.5)
     t_2
     (if (<= t_1 2e-18)
       (expm1
        (log1p
         (fma
          2.0
          (pow x -5.0)
          (fma 2.0 (pow x -3.0) (fma 2.0 (pow x -7.0) (* 2.0 (pow x -9.0)))))))
       t_2))))
double code(double x) {
	return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
double code(double x) {
	double t_0 = x * (1.0 + x);
	double t_1 = ((1.0 / (1.0 + x)) + (-2.0 / x)) + (1.0 / (x + -1.0));
	double t_2 = (t_0 + ((x + -1.0) * (x + (2.0 * (-1.0 - x))))) / (t_0 * (x + -1.0));
	double tmp;
	if (t_1 <= -0.5) {
		tmp = t_2;
	} else if (t_1 <= 2e-18) {
		tmp = expm1(log1p(fma(2.0, pow(x, -5.0), fma(2.0, pow(x, -3.0), fma(2.0, pow(x, -7.0), (2.0 * pow(x, -9.0)))))));
	} else {
		tmp = t_2;
	}
	return tmp;
}
function code(x)
	return Float64(Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x - 1.0)))
end
function code(x)
	t_0 = Float64(x * Float64(1.0 + x))
	t_1 = Float64(Float64(Float64(1.0 / Float64(1.0 + x)) + Float64(-2.0 / x)) + Float64(1.0 / Float64(x + -1.0)))
	t_2 = Float64(Float64(t_0 + Float64(Float64(x + -1.0) * Float64(x + Float64(2.0 * Float64(-1.0 - x))))) / Float64(t_0 * Float64(x + -1.0)))
	tmp = 0.0
	if (t_1 <= -0.5)
		tmp = t_2;
	elseif (t_1 <= 2e-18)
		tmp = expm1(log1p(fma(2.0, (x ^ -5.0), fma(2.0, (x ^ -3.0), fma(2.0, (x ^ -7.0), Float64(2.0 * (x ^ -9.0)))))));
	else
		tmp = t_2;
	end
	return tmp
end
code[x_] := N[(N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_] := Block[{t$95$0 = N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(t$95$0 + N[(N[(x + -1.0), $MachinePrecision] * N[(x + N[(2.0 * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(t$95$0 * N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -0.5], t$95$2, If[LessEqual[t$95$1, 2e-18], N[(Exp[N[Log[1 + N[(2.0 * N[Power[x, -5.0], $MachinePrecision] + N[(2.0 * N[Power[x, -3.0], $MachinePrecision] + N[(2.0 * N[Power[x, -7.0], $MachinePrecision] + N[(2.0 * N[Power[x, -9.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]] - 1), $MachinePrecision], t$95$2]]]]]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\begin{array}{l}
t_0 := x \cdot \left(1 + x\right)\\
t_1 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\
t_2 := \frac{t_0 + \left(x + -1\right) \cdot \left(x + 2 \cdot \left(-1 - x\right)\right)}{t_0 \cdot \left(x + -1\right)}\\
\mathbf{if}\;t_1 \leq -0.5:\\
\;\;\;\;t_2\\

\mathbf{elif}\;t_1 \leq 2 \cdot 10^{-18}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(2, {x}^{-5}, \mathsf{fma}\left(2, {x}^{-3}, \mathsf{fma}\left(2, {x}^{-7}, 2 \cdot {x}^{-9}\right)\right)\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}

Error

Bits error versus x

Target

Original9.4
Target0.2
Herbie0.1
\[\frac{2}{x \cdot \left(x \cdot x - 1\right)} \]

Derivation

  1. Split input into 2 regimes
  2. if (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < -0.5 or 2.0000000000000001e-18 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1)))

    1. Initial program 0.2

      \[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
    2. Applied egg-rr0.0

      \[\leadsto \color{blue}{\frac{x \cdot \left(1 + x\right) + \left(x + -1\right) \cdot \left(x - \left(1 + x\right) \cdot 2\right)}{\left(x + -1\right) \cdot \left(x \cdot \left(1 + x\right)\right)}} \]

    if -0.5 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 2.0000000000000001e-18

    1. Initial program 19.2

      \[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
    2. Taylor expanded in x around inf 0.5

      \[\leadsto \color{blue}{2 \cdot \frac{1}{{x}^{5}} + \left(2 \cdot \frac{1}{{x}^{7}} + \left(2 \cdot \frac{1}{{x}^{3}} + 2 \cdot \frac{1}{{x}^{9}}\right)\right)} \]
    3. Simplified0.5

      \[\leadsto \color{blue}{\frac{2}{{x}^{5}} + \left(\frac{2}{{x}^{3}} + \left(\frac{2}{{x}^{7}} + \frac{2}{{x}^{9}}\right)\right)} \]
    4. Applied egg-rr0.1

      \[\leadsto \frac{2}{{x}^{5}} + \left(\color{blue}{2 \cdot {x}^{-3}} + \left(\frac{2}{{x}^{7}} + \frac{2}{{x}^{9}}\right)\right) \]
    5. Applied egg-rr0.1

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(2, {x}^{-5}, \mathsf{fma}\left(2, {x}^{-3}, \mathsf{fma}\left(2, {x}^{-7}, 2 \cdot {x}^{-9}\right)\right)\right)\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1} \leq -0.5:\\ \;\;\;\;\frac{x \cdot \left(1 + x\right) + \left(x + -1\right) \cdot \left(x + 2 \cdot \left(-1 - x\right)\right)}{\left(x \cdot \left(1 + x\right)\right) \cdot \left(x + -1\right)}\\ \mathbf{elif}\;\left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1} \leq 2 \cdot 10^{-18}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(2, {x}^{-5}, \mathsf{fma}\left(2, {x}^{-3}, \mathsf{fma}\left(2, {x}^{-7}, 2 \cdot {x}^{-9}\right)\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot \left(1 + x\right) + \left(x + -1\right) \cdot \left(x + 2 \cdot \left(-1 - x\right)\right)}{\left(x \cdot \left(1 + x\right)\right) \cdot \left(x + -1\right)}\\ \end{array} \]

Reproduce

herbie shell --seed 2022172 
(FPCore (x)
  :name "3frac (problem 3.3.3)"
  :precision binary64

  :herbie-target
  (/ 2.0 (* x (- (* x x) 1.0)))

  (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))