Average Error: 10.1 → 0.5
Time: 4.6s
Precision: binary64
\[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
\[\begin{array}{l} t_0 := \left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1}\\ \mathbf{if}\;t_0 \leq -17.63953329044322:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 1.2570223047137993 \cdot 10^{-16}:\\ \;\;\;\;\frac{\frac{2}{x}}{x \cdot x} + \frac{2}{{x}^{5}}\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot \left(\frac{1}{x} + \mathsf{fma}\left(x, \mathsf{fma}\left(x, x, 1\right), {x}^{5}\right)\right)\\ \end{array} \]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\begin{array}{l}
t_0 := \left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1}\\
\mathbf{if}\;t_0 \leq -17.63953329044322:\\
\;\;\;\;t_0\\

\mathbf{elif}\;t_0 \leq 1.2570223047137993 \cdot 10^{-16}:\\
\;\;\;\;\frac{\frac{2}{x}}{x \cdot x} + \frac{2}{{x}^{5}}\\

\mathbf{else}:\\
\;\;\;\;-2 \cdot \left(\frac{1}{x} + \mathsf{fma}\left(x, \mathsf{fma}\left(x, x, 1\right), {x}^{5}\right)\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 (+ 1.0 x)) (/ 2.0 x)) (/ 1.0 (- x 1.0)))))
   (if (<= t_0 -17.63953329044322)
     t_0
     (if (<= t_0 1.2570223047137993e-16)
       (+ (/ (/ 2.0 x) (* x x)) (/ 2.0 (pow x 5.0)))
       (* -2.0 (+ (/ 1.0 x) (fma x (fma x x 1.0) (pow x 5.0))))))))
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 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x - 1.0));
	double tmp;
	if (t_0 <= -17.63953329044322) {
		tmp = t_0;
	} else if (t_0 <= 1.2570223047137993e-16) {
		tmp = ((2.0 / x) / (x * x)) + (2.0 / pow(x, 5.0));
	} else {
		tmp = -2.0 * ((1.0 / x) + fma(x, fma(x, x, 1.0), pow(x, 5.0)));
	}
	return tmp;
}

Error

Bits error versus x

Target

Original10.1
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))) < -17.6395332904432216

    1. Initial program 0.0

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

    if -17.6395332904432216 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 1.2570223e-16

    1. Initial program 20.2

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

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

      \[\leadsto \color{blue}{\frac{2}{{x}^{3}} + \frac{2}{{x}^{5}}} \]
    4. Applied cube-mult_binary640.9

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

      \[\leadsto \color{blue}{\frac{\frac{2}{x}}{x \cdot x}} + \frac{2}{{x}^{5}} \]

    if 1.2570223e-16 < (+.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 expm1-log1p-u_binary645.8

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\right)\right)} \]
    3. Taylor expanded in x around 0 1.1

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

      \[\leadsto \color{blue}{-2 \cdot \left(\frac{1}{x} + \mathsf{fma}\left(x, \mathsf{fma}\left(x, x, 1\right), {x}^{5}\right)\right)} \]
  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 -17.63953329044322:\\ \;\;\;\;\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 1.2570223047137993 \cdot 10^{-16}:\\ \;\;\;\;\frac{\frac{2}{x}}{x \cdot x} + \frac{2}{{x}^{5}}\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot \left(\frac{1}{x} + \mathsf{fma}\left(x, \mathsf{fma}\left(x, x, 1\right), {x}^{5}\right)\right)\\ \end{array} \]

Reproduce

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