Average Error: 14.4 → 0.4
Time: 1.9s
Precision: binary64
\[\frac{1}{x + 1} - \frac{1}{x} \]
\[\begin{array}{l} t_0 := \frac{1}{1 + x} + \frac{-1}{x}\\ \mathbf{if}\;t_0 \leq -1000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;{x}^{-3} + \mathsf{fma}\left(1, {x}^{-5}, \mathsf{fma}\left(-1, {x}^{-2}, -{x}^{-4}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{x + \left(-1 - x\right)}{1 + x}}{x}\\ \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (+ (/ 1.0 (+ 1.0 x)) (/ -1.0 x))))
   (if (<= t_0 -1000.0)
     t_0
     (if (<= t_0 0.0)
       (+
        (pow x -3.0)
        (fma 1.0 (pow x -5.0) (fma -1.0 (pow x -2.0) (- (pow x -4.0)))))
       (/ (/ (+ x (- -1.0 x)) (+ 1.0 x)) x)))))
double code(double x) {
	return (1.0 / (x + 1.0)) - (1.0 / x);
}
double code(double x) {
	double t_0 = (1.0 / (1.0 + x)) + (-1.0 / x);
	double tmp;
	if (t_0 <= -1000.0) {
		tmp = t_0;
	} else if (t_0 <= 0.0) {
		tmp = pow(x, -3.0) + fma(1.0, pow(x, -5.0), fma(-1.0, pow(x, -2.0), -pow(x, -4.0)));
	} else {
		tmp = ((x + (-1.0 - x)) / (1.0 + x)) / x;
	}
	return tmp;
}
function code(x)
	return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / x))
end
function code(x)
	t_0 = Float64(Float64(1.0 / Float64(1.0 + x)) + Float64(-1.0 / x))
	tmp = 0.0
	if (t_0 <= -1000.0)
		tmp = t_0;
	elseif (t_0 <= 0.0)
		tmp = Float64((x ^ -3.0) + fma(1.0, (x ^ -5.0), fma(-1.0, (x ^ -2.0), Float64(-(x ^ -4.0)))));
	else
		tmp = Float64(Float64(Float64(x + Float64(-1.0 - x)) / Float64(1.0 + x)) / x);
	end
	return tmp
end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
code[x_] := Block[{t$95$0 = N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -1000.0], t$95$0, If[LessEqual[t$95$0, 0.0], N[(N[Power[x, -3.0], $MachinePrecision] + N[(1.0 * N[Power[x, -5.0], $MachinePrecision] + N[(-1.0 * N[Power[x, -2.0], $MachinePrecision] + (-N[Power[x, -4.0], $MachinePrecision])), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(x + N[(-1.0 - x), $MachinePrecision]), $MachinePrecision] / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]]]
\frac{1}{x + 1} - \frac{1}{x}
\begin{array}{l}
t_0 := \frac{1}{1 + x} + \frac{-1}{x}\\
\mathbf{if}\;t_0 \leq -1000:\\
\;\;\;\;t_0\\

\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;{x}^{-3} + \mathsf{fma}\left(1, {x}^{-5}, \mathsf{fma}\left(-1, {x}^{-2}, -{x}^{-4}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{x + \left(-1 - x\right)}{1 + x}}{x}\\


\end{array}

Error

Derivation

  1. Split input into 3 regimes
  2. if (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 1 x)) < -1e3

    1. Initial program 0.0

      \[\frac{1}{x + 1} - \frac{1}{x} \]

    if -1e3 < (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 1 x)) < 0.0

    1. Initial program 28.9

      \[\frac{1}{x + 1} - \frac{1}{x} \]
    2. Taylor expanded in x around inf 1.4

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

      \[\leadsto \color{blue}{\frac{1}{{x}^{3}} + \left(\frac{1}{{x}^{5}} + \left(\frac{-1}{x \cdot x} + \frac{-1}{{x}^{4}}\right)\right)} \]
    4. Applied egg-rr0.7

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

      \[\leadsto \color{blue}{{x}^{-3}} + \mathsf{fma}\left(1, {x}^{-5}, \mathsf{fma}\left(-1, {x}^{-2}, -{x}^{-4}\right)\right) \]

    if 0.0 < (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 1 x))

    1. Initial program 0.0

      \[\frac{1}{x + 1} - \frac{1}{x} \]
    2. Applied egg-rr0.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1}{1 + x} + \frac{-1}{x} \leq -1000:\\ \;\;\;\;\frac{1}{1 + x} + \frac{-1}{x}\\ \mathbf{elif}\;\frac{1}{1 + x} + \frac{-1}{x} \leq 0:\\ \;\;\;\;{x}^{-3} + \mathsf{fma}\left(1, {x}^{-5}, \mathsf{fma}\left(-1, {x}^{-2}, -{x}^{-4}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{x + \left(-1 - x\right)}{1 + x}}{x}\\ \end{array} \]

Reproduce

herbie shell --seed 2022190 
(FPCore (x)
  :name "2frac (problem 3.3.1)"
  :precision binary64
  (- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))