Average Error: 9.7 → 0.7
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 - \left(2 + x \cdot 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{\log \left(e^{-2 - x}\right)}{\mathsf{fma}\left(x, x, 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 (+ 2.0 (* x 2.0))) (fma x x x)))
     (if (<= t_1 0.0)
       (/ (/ (/ 2.0 x) x) x)
       (+ t_0 (/ (log (exp (- -2.0 x))) (fma x x 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 - (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 + (log(exp((-2.0 - x))) / fma(x, x, 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 - Float64(2.0 + Float64(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(log(exp(Float64(-2.0 - x))) / fma(x, x, 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 + N[(x * 2.0), $MachinePrecision]), $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[Log[N[Exp[N[(-2.0 - x), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] / N[(x * x + x), $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 - \left(2 + x \cdot 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{\log \left(e^{-2 - x}\right)}{\mathsf{fma}\left(x, x, x\right)}\\


\end{array}

Error

Bits error versus x

Target

Original9.7
Target0.3
Herbie0.7
\[\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} \]

    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. Simplified1.3

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

      \[\leadsto \frac{x - \left(2 + x \cdot 2\right)}{\color{blue}{\mathsf{fma}\left(x, x, x\right)}} + \frac{1}{x - 1} \]
    5. Applied add-log-exp_binary641.9

      \[\leadsto \frac{x - \left(2 + \color{blue}{\log \left(e^{x \cdot 2}\right)}\right)}{\mathsf{fma}\left(x, x, x\right)} + \frac{1}{x - 1} \]
    6. Applied add-log-exp_binary641.9

      \[\leadsto \frac{x - \left(\color{blue}{\log \left(e^{2}\right)} + \log \left(e^{x \cdot 2}\right)\right)}{\mathsf{fma}\left(x, x, x\right)} + \frac{1}{x - 1} \]
    7. Applied sum-log_binary641.9

      \[\leadsto \frac{x - \color{blue}{\log \left(e^{2} \cdot e^{x \cdot 2}\right)}}{\mathsf{fma}\left(x, x, x\right)} + \frac{1}{x - 1} \]
    8. Applied add-log-exp_binary641.9

      \[\leadsto \frac{\color{blue}{\log \left(e^{x}\right)} - \log \left(e^{2} \cdot e^{x \cdot 2}\right)}{\mathsf{fma}\left(x, x, x\right)} + \frac{1}{x - 1} \]
    9. Applied diff-log_binary641.9

      \[\leadsto \frac{\color{blue}{\log \left(\frac{e^{x}}{e^{2} \cdot e^{x \cdot 2}}\right)}}{\mathsf{fma}\left(x, x, x\right)} + \frac{1}{x - 1} \]
    10. Simplified1.7

      \[\leadsto \frac{\log \color{blue}{\left(e^{-2 - x}\right)}}{\mathsf{fma}\left(x, x, x\right)} + \frac{1}{x - 1} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.7

    \[\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 - \left(2 + x \cdot 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{\log \left(e^{-2 - x}\right)}{\mathsf{fma}\left(x, x, 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))))