Average Error: 9.2 → 0.5
Time: 3.8s
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 -5.505135275469517:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_1 \leq 2.538941946223991 \cdot 10^{-11}:\\ \;\;\;\;\frac{2}{{x}^{5}} + \left(\frac{\frac{2}{x \cdot x}}{x} + \frac{2}{{x}^{7}}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 + \left(1 - 2 \cdot \frac{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 -5.505135275469517:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_1 \leq 2.538941946223991 \cdot 10^{-11}:\\
\;\;\;\;\frac{2}{{x}^{5}} + \left(\frac{\frac{2}{x \cdot x}}{x} + \frac{2}{{x}^{7}}\right)\\

\mathbf{else}:\\
\;\;\;\;t_0 + \left(1 - 2 \cdot \frac{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 -5.505135275469517)
     t_1
     (if (<= t_1 2.538941946223991e-11)
       (+ (/ 2.0 (pow x 5.0)) (+ (/ (/ 2.0 (* x x)) x) (/ 2.0 (pow x 7.0))))
       (+ t_0 (- 1.0 (* 2.0 (/ 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 <= -5.505135275469517) {
		tmp = t_1;
	} else if (t_1 <= 2.538941946223991e-11) {
		tmp = (2.0 / pow(x, 5.0)) + (((2.0 / (x * x)) / x) + (2.0 / pow(x, 7.0)));
	} else {
		tmp = t_0 + (1.0 - (2.0 * (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.2
Target0.3
Herbie0.5
\[\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))) < -5.5051352754695166

    1. Initial program 0.0

      \[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]

    if -5.5051352754695166 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 2.5389419462e-11

    1. Initial program 18.7

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

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

      \[\leadsto \color{blue}{\frac{2}{{x}^{5}} + \left(\frac{2}{{x}^{3}} + \frac{2}{{x}^{7}}\right)} \]
    4. Applied unpow3_binary640.6

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

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

    if 2.5389419462e-11 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1)))

    1. Initial program 0.1

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1} \leq -5.505135275469517:\\ \;\;\;\;\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1}\\ \mathbf{elif}\;\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1} \leq 2.538941946223991 \cdot 10^{-11}:\\ \;\;\;\;\frac{2}{{x}^{5}} + \left(\frac{\frac{2}{x \cdot x}}{x} + \frac{2}{{x}^{7}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x - 1} + \left(1 - 2 \cdot \frac{1}{x}\right)\\ \end{array} \]

Reproduce

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