Average Error: 9.7 → 0.6
Time: 3.7s
Precision: binary64
\[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
\[\begin{array}{l} t_0 := \left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\\ t_1 := \frac{\mathsf{fma}\left(\frac{1}{1 + x}, x, -2\right) - \log \left(e^{\frac{x}{1 - x}}\right)}{x}\\ \mathbf{if}\;t_0 \leq -13.837372505796882:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;\frac{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{2}{x \cdot x}\right)\right)}{x}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\begin{array}{l}
t_0 := \left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\\
t_1 := \frac{\mathsf{fma}\left(\frac{1}{1 + x}, x, -2\right) - \log \left(e^{\frac{x}{1 - x}}\right)}{x}\\
\mathbf{if}\;t_0 \leq -13.837372505796882:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;\frac{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{2}{x \cdot x}\right)\right)}{x}\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\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)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
        (t_1
         (/ (- (fma (/ 1.0 (+ 1.0 x)) x -2.0) (log (exp (/ x (- 1.0 x))))) x)))
   (if (<= t_0 -13.837372505796882)
     t_1
     (if (<= t_0 0.0) (/ (log1p (expm1 (/ 2.0 (* x x)))) x) 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 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
	double t_1 = (fma((1.0 / (1.0 + x)), x, -2.0) - log(exp((x / (1.0 - x))))) / x;
	double tmp;
	if (t_0 <= -13.837372505796882) {
		tmp = t_1;
	} else if (t_0 <= 0.0) {
		tmp = log1p(expm1((2.0 / (x * x)))) / x;
	} else {
		tmp = t_1;
	}
	return tmp;
}

Error

Bits error versus x

Target

Original9.7
Target0.3
Herbie0.6
\[\frac{2}{x \cdot \left(x \cdot x - 1\right)} \]

Derivation

  1. Split input into 2 regimes
  2. if (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < -13.837372505796882 or 0.0 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1)))

    1. Initial program 0.6

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{1}{1 + x}, x, -2\right) - \frac{1}{1 - x} \cdot x}{x}} \]
    3. Applied egg-rr0.6

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

    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. Applied egg-rr31.6

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{1}{1 + x}, x, -2\right) - \frac{1}{1 - x} \cdot x}{x}} \]
    3. Taylor expanded in x around inf 0.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \leq -13.837372505796882:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{1}{1 + x}, x, -2\right) - \log \left(e^{\frac{x}{1 - x}}\right)}{x}\\ \mathbf{elif}\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \leq 0:\\ \;\;\;\;\frac{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{2}{x \cdot x}\right)\right)}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{1}{1 + x}, x, -2\right) - \log \left(e^{\frac{x}{1 - x}}\right)}{x}\\ \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))))