Average Error: 9.7 → 0.6
Time: 4.3s
Precision: binary64
\[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
\[\begin{array}{l} t_0 := \frac{1}{x - 1}\\ t_1 := \left(\frac{1}{1 + x} - \frac{2}{x}\right) + t_0\\ \mathbf{if}\;t_1 \leq -13.837372505796882:\\ \;\;\;\;t_0 + \frac{x + \mathsf{fma}\left(-2, x, -2\right)}{\mathsf{fma}\left(x, x, x\right)}\\ \mathbf{elif}\;t_1 \leq 0:\\ \;\;\;\;\frac{\frac{\frac{2}{x}}{x}}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0 + \frac{x - \left(1 + x\right) \cdot 2}{x \cdot \left(1 + x\right)}\\ \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 (/ 1.0 (- x 1.0))) (t_1 (+ (- (/ 1.0 (+ 1.0 x)) (/ 2.0 x)) t_0)))
   (if (<= t_1 -13.837372505796882)
     (+ t_0 (/ (+ x (fma -2.0 x -2.0)) (fma x x x)))
     (if (<= t_1 0.0)
       (/ (/ (/ 2.0 x) x) x)
       (+ t_0 (/ (- x (* (+ 1.0 x) 2.0)) (* x (+ 1.0 x))))))))
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 = 1.0 / (x - 1.0);
	double t_1 = ((1.0 / (1.0 + x)) - (2.0 / x)) + t_0;
	double tmp;
	if (t_1 <= -13.837372505796882) {
		tmp = t_0 + ((x + fma(-2.0, x, -2.0)) / fma(x, x, x));
	} else if (t_1 <= 0.0) {
		tmp = ((2.0 / x) / x) / x;
	} else {
		tmp = t_0 + ((x - ((1.0 + x) * 2.0)) / (x * (1.0 + x)));
	}
	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(1.0 / Float64(x - 1.0))
	t_1 = Float64(Float64(Float64(1.0 / Float64(1.0 + x)) - Float64(2.0 / x)) + t_0)
	tmp = 0.0
	if (t_1 <= -13.837372505796882)
		tmp = Float64(t_0 + Float64(Float64(x + fma(-2.0, x, -2.0)) / fma(x, x, x)));
	elseif (t_1 <= 0.0)
		tmp = Float64(Float64(Float64(2.0 / x) / x) / x);
	else
		tmp = Float64(t_0 + Float64(Float64(x - Float64(Float64(1.0 + x) * 2.0)) / Float64(x * Float64(1.0 + x))));
	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[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]}, If[LessEqual[t$95$1, -13.837372505796882], N[(t$95$0 + N[(N[(x + N[(-2.0 * x + -2.0), $MachinePrecision]), $MachinePrecision] / N[(x * x + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 0.0], N[(N[(N[(2.0 / x), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision], N[(t$95$0 + N[(N[(x - N[(N[(1.0 + x), $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision] / N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\begin{array}{l}
t_0 := \frac{1}{x - 1}\\
t_1 := \left(\frac{1}{1 + x} - \frac{2}{x}\right) + t_0\\
\mathbf{if}\;t_1 \leq -13.837372505796882:\\
\;\;\;\;t_0 + \frac{x + \mathsf{fma}\left(-2, x, -2\right)}{\mathsf{fma}\left(x, x, x\right)}\\

\mathbf{elif}\;t_1 \leq 0:\\
\;\;\;\;\frac{\frac{\frac{2}{x}}{x}}{x}\\

\mathbf{else}:\\
\;\;\;\;t_0 + \frac{x - \left(1 + x\right) \cdot 2}{x \cdot \left(1 + x\right)}\\


\end{array}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 3 regimes
  2. if (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < -13.837372505796882

    1. Initial program 0.0

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

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

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

      \[\leadsto \frac{x - \left(2 + x \cdot 2\right)}{\color{blue}{\mathsf{fma}\left(x, x, x\right)}} + \frac{1}{x - 1} \]
    5. Applied sub-neg_binary640.0

      \[\leadsto \frac{\color{blue}{x + \left(-\left(2 + x \cdot 2\right)\right)}}{\mathsf{fma}\left(x, x, x\right)} + \frac{1}{x - 1} \]
    6. Simplified0.0

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

    if -13.837372505796882 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 0.0

    1. Initial program 19.0

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

      \[\leadsto \color{blue}{\frac{2}{{x}^{3}}} \]
    3. Applied cube-mult_binary641.0

      \[\leadsto \frac{2}{\color{blue}{x \cdot \left(x \cdot x\right)}} \]
    4. Applied associate-/r*_binary640.5

      \[\leadsto \color{blue}{\frac{\frac{2}{x}}{x \cdot x}} \]
    5. Applied associate-/r*_binary640.5

      \[\leadsto \color{blue}{\frac{\frac{\frac{2}{x}}{x}}{x}} \]

    if 0.0 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1)))

    1. Initial program 1.2

      \[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
    2. Applied frac-sub_binary641.3

      \[\leadsto \color{blue}{\frac{1 \cdot x - \left(x + 1\right) \cdot 2}{\left(x + 1\right) \cdot x}} + \frac{1}{x - 1} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1} \leq -13.837372505796882:\\ \;\;\;\;\frac{1}{x - 1} + \frac{x + \mathsf{fma}\left(-2, x, -2\right)}{\mathsf{fma}\left(x, x, x\right)}\\ \mathbf{elif}\;\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1} \leq 0:\\ \;\;\;\;\frac{\frac{\frac{2}{x}}{x}}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x - 1} + \frac{x - \left(1 + x\right) \cdot 2}{x \cdot \left(1 + x\right)}\\ \end{array} \]

Reproduce

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