Average Error: 10.4 → 0.4
Time: 2.4s
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 -50:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{-19}:\\ \;\;\;\;2 \cdot \left({x}^{-3} + {x}^{-5}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-2, x + {x}^{3}, \frac{-2}{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 (+ 1.0 x)) (/ -2.0 x)) (/ 1.0 (+ x -1.0)))))
   (if (<= t_0 -50.0)
     t_0
     (if (<= t_0 2e-19)
       (* 2.0 (+ (pow x -3.0) (pow x -5.0)))
       (fma -2.0 (+ x (pow x 3.0)) (/ -2.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 / (1.0 + x)) + (-2.0 / x)) + (1.0 / (x + -1.0));
	double tmp;
	if (t_0 <= -50.0) {
		tmp = t_0;
	} else if (t_0 <= 2e-19) {
		tmp = 2.0 * (pow(x, -3.0) + pow(x, -5.0));
	} else {
		tmp = fma(-2.0, (x + pow(x, 3.0)), (-2.0 / x));
	}
	return tmp;
}
function code(x)
	return Float64(Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x - 1.0)))
end
function code(x)
	t_0 = Float64(Float64(Float64(1.0 / Float64(1.0 + x)) + Float64(-2.0 / x)) + Float64(1.0 / Float64(x + -1.0)))
	tmp = 0.0
	if (t_0 <= -50.0)
		tmp = t_0;
	elseif (t_0 <= 2e-19)
		tmp = Float64(2.0 * Float64((x ^ -3.0) + (x ^ -5.0)));
	else
		tmp = fma(-2.0, Float64(x + (x ^ 3.0)), Float64(-2.0 / x));
	end
	return tmp
end
code[x_] := N[(N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_] := Block[{t$95$0 = N[(N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -50.0], t$95$0, If[LessEqual[t$95$0, 2e-19], N[(2.0 * N[(N[Power[x, -3.0], $MachinePrecision] + N[Power[x, -5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-2.0 * N[(x + N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision]]]]
\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 -50:\\
\;\;\;\;t_0\\

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

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-2, x + {x}^{3}, \frac{-2}{x}\right)\\


\end{array}

Error

Target

Original10.4
Target0.4
Herbie0.4
\[\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))) < -50

    1. Initial program 0.0

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

    if -50 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 2e-19

    1. Initial program 20.6

      \[\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}^{5}} + \frac{2}{{x}^{3}}} \]
    4. Applied egg-rr0.2

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

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

    1. Initial program 0.3

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1} \leq -50:\\ \;\;\;\;\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 \cdot 10^{-19}:\\ \;\;\;\;2 \cdot \left({x}^{-3} + {x}^{-5}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-2, x + {x}^{3}, \frac{-2}{x}\right)\\ \end{array} \]

Reproduce

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