Average Error: 9.6 → 0.3
Time: 3.9s
Precision: binary64
\[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
\[\begin{array}{l} t_0 := \frac{1}{1 + x} - \frac{2}{x}\\ t_1 := \frac{1}{x - 1}\\ t_2 := t_0 + t_1\\ \mathbf{if}\;t_2 \leq -0.0005924544417727495:\\ \;\;\;\;t_1 + \mathsf{fma}\left(\frac{1}{x \cdot x - 1}, x - 1, \frac{-2}{x}\right)\\ \mathbf{elif}\;t_2 \leq 1.4452477223878338 \cdot 10^{-9}:\\ \;\;\;\;\frac{2}{{x}^{5}} + \left(\frac{2}{{x}^{3}} + \frac{2}{{x}^{7}}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 + \mathsf{expm1}\left(\mathsf{log1p}\left(t_1\right)\right)\\ \end{array} \]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\begin{array}{l}
t_0 := \frac{1}{1 + x} - \frac{2}{x}\\
t_1 := \frac{1}{x - 1}\\
t_2 := t_0 + t_1\\
\mathbf{if}\;t_2 \leq -0.0005924544417727495:\\
\;\;\;\;t_1 + \mathsf{fma}\left(\frac{1}{x \cdot x - 1}, x - 1, \frac{-2}{x}\right)\\

\mathbf{elif}\;t_2 \leq 1.4452477223878338 \cdot 10^{-9}:\\
\;\;\;\;\frac{2}{{x}^{5}} + \left(\frac{2}{{x}^{3}} + \frac{2}{{x}^{7}}\right)\\

\mathbf{else}:\\
\;\;\;\;t_0 + \mathsf{expm1}\left(\mathsf{log1p}\left(t_1\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)))
        (t_1 (/ 1.0 (- x 1.0)))
        (t_2 (+ t_0 t_1)))
   (if (<= t_2 -0.0005924544417727495)
     (+ t_1 (fma (/ 1.0 (- (* x x) 1.0)) (- x 1.0) (/ -2.0 x)))
     (if (<= t_2 1.4452477223878338e-9)
       (+ (/ 2.0 (pow x 5.0)) (+ (/ 2.0 (pow x 3.0)) (/ 2.0 (pow x 7.0))))
       (+ t_0 (expm1 (log1p t_1)))))))
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);
	double t_1 = 1.0 / (x - 1.0);
	double t_2 = t_0 + t_1;
	double tmp;
	if (t_2 <= -0.0005924544417727495) {
		tmp = t_1 + fma((1.0 / ((x * x) - 1.0)), (x - 1.0), (-2.0 / x));
	} else if (t_2 <= 1.4452477223878338e-9) {
		tmp = (2.0 / pow(x, 5.0)) + ((2.0 / pow(x, 3.0)) + (2.0 / pow(x, 7.0)));
	} else {
		tmp = t_0 + expm1(log1p(t_1));
	}
	return tmp;
}

Error

Bits error versus x

Target

Original9.6
Target0.3
Herbie0.3
\[\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.9245444177275e-4

    1. Initial program 0.0

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

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

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

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

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

    if -5.9245444177275e-4 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 1.44524772239e-9

    1. Initial program 18.9

      \[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
    2. Taylor expanded 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)} \]

    if 1.44524772239e-9 < (+.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. Applied expm1-log1p-u_binary640.1

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1} \leq -0.0005924544417727495:\\ \;\;\;\;\frac{1}{x - 1} + \mathsf{fma}\left(\frac{1}{x \cdot x - 1}, x - 1, \frac{-2}{x}\right)\\ \mathbf{elif}\;\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x - 1} \leq 1.4452477223878338 \cdot 10^{-9}:\\ \;\;\;\;\frac{2}{{x}^{5}} + \left(\frac{2}{{x}^{3}} + \frac{2}{{x}^{7}}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{x - 1}\right)\right)\\ \end{array} \]

Reproduce

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