Average Error: 9.7 → 0.2
Time: 5.2s
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 -6.251301623204472:\\ \;\;\;\;t_0 + \left(e^{-\mathsf{log1p}\left(x\right)} - \frac{2}{x}\right)\\ \mathbf{elif}\;t_1 \leq 7.464177659202015 \cdot 10^{-13}:\\ \;\;\;\;2 \cdot \left({x}^{-3} + {x}^{-5}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 + \frac{x - \left(1 + x\right) \cdot 2}{x \cdot \left(1 + x\right)}\\ \end{array} \]
\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 -6.251301623204472:\\
\;\;\;\;t_0 + \left(e^{-\mathsf{log1p}\left(x\right)} - \frac{2}{x}\right)\\

\mathbf{elif}\;t_1 \leq 7.464177659202015 \cdot 10^{-13}:\\
\;\;\;\;2 \cdot \left({x}^{-3} + {x}^{-5}\right)\\

\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 -6.251301623204472)
     (+ t_0 (- (exp (- (log1p x))) (/ 2.0 x)))
     (if (<= t_1 7.464177659202015e-13)
       (* 2.0 (+ (pow x -3.0) (pow x -5.0)))
       (+ 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 <= -6.251301623204472) {
		tmp = t_0 + (exp(-log1p(x)) - (2.0 / x));
	} else if (t_1 <= 7.464177659202015e-13) {
		tmp = 2.0 * (pow(x, -3.0) + pow(x, -5.0));
	} else {
		tmp = t_0 + ((x - ((1.0 + x) * 2.0)) / (x * (1.0 + x)));
	}
	return tmp;
}

Error

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original9.7
Target0.3
Herbie0.2
\[\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))) < -6.2513016232044718

    1. Initial program 0.0

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

      \[\leadsto \left(\color{blue}{e^{-\mathsf{log1p}\left(x\right)}} - \frac{2}{x}\right) + \frac{1}{x - 1} \]

    if -6.2513016232044718 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 7.464177659e-13

    1. Initial program 19.3

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

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

      \[\leadsto \color{blue}{\frac{2}{{x}^{3}} + \frac{2}{{x}^{5}}} \]
    4. Applied egg-rr0.2

      \[\leadsto \color{blue}{2 \cdot \left({x}^{-3} + {x}^{-5}\right)} \]

    if 7.464177659e-13 < (+.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.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1} \leq -6.251301623204472:\\ \;\;\;\;\frac{1}{x - 1} + \left(e^{-\mathsf{log1p}\left(x\right)} - \frac{2}{x}\right)\\ \mathbf{elif}\;\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1} \leq 7.464177659202015 \cdot 10^{-13}:\\ \;\;\;\;2 \cdot \left({x}^{-3} + {x}^{-5}\right)\\ \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 2022130 
(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))))