Average Error: 9.6 → 0.0
Time: 13.1s
Precision: binary64
Cost: 21956
\[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
\[\begin{array}{l} t_0 := x \cdot \left(1 + x\right)\\ t_1 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\ \mathbf{if}\;t_1 \leq -5 \cdot 10^{-21}:\\ \;\;\;\;\frac{\mathsf{fma}\left(-1, t_0, \mathsf{fma}\left(-1, x, 1\right) \cdot \left(x + 2 \cdot \left(-1 - x\right)\right)\right)}{t_0 \cdot \mathsf{fma}\left(-1, x, 1\right)}\\ \mathbf{elif}\;t_1 \leq 4 \cdot 10^{-25}:\\ \;\;\;\;2 \cdot {x}^{-3}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, x, x\right) + \left(x + -1\right) \cdot \left(-2 - x\right)}{\mathsf{fma}\left(x, x, x\right) \cdot \left(x + -1\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 (* x (+ 1.0 x)))
        (t_1 (+ (+ (/ 1.0 (+ 1.0 x)) (/ -2.0 x)) (/ 1.0 (+ x -1.0)))))
   (if (<= t_1 -5e-21)
     (/
      (fma -1.0 t_0 (* (fma -1.0 x 1.0) (+ x (* 2.0 (- -1.0 x)))))
      (* t_0 (fma -1.0 x 1.0)))
     (if (<= t_1 4e-25)
       (* 2.0 (pow x -3.0))
       (/
        (+ (fma x x x) (* (+ x -1.0) (- -2.0 x)))
        (* (fma x x x) (+ x -1.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 = x * (1.0 + x);
	double t_1 = ((1.0 / (1.0 + x)) + (-2.0 / x)) + (1.0 / (x + -1.0));
	double tmp;
	if (t_1 <= -5e-21) {
		tmp = fma(-1.0, t_0, (fma(-1.0, x, 1.0) * (x + (2.0 * (-1.0 - x))))) / (t_0 * fma(-1.0, x, 1.0));
	} else if (t_1 <= 4e-25) {
		tmp = 2.0 * pow(x, -3.0);
	} else {
		tmp = (fma(x, x, x) + ((x + -1.0) * (-2.0 - x))) / (fma(x, x, x) * (x + -1.0));
	}
	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(x * Float64(1.0 + x))
	t_1 = 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_1 <= -5e-21)
		tmp = Float64(fma(-1.0, t_0, Float64(fma(-1.0, x, 1.0) * Float64(x + Float64(2.0 * Float64(-1.0 - x))))) / Float64(t_0 * fma(-1.0, x, 1.0)));
	elseif (t_1 <= 4e-25)
		tmp = Float64(2.0 * (x ^ -3.0));
	else
		tmp = Float64(Float64(fma(x, x, x) + Float64(Float64(x + -1.0) * Float64(-2.0 - x))) / Float64(fma(x, x, x) * Float64(x + -1.0)));
	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[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = 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$1, -5e-21], N[(N[(-1.0 * t$95$0 + N[(N[(-1.0 * x + 1.0), $MachinePrecision] * N[(x + N[(2.0 * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(t$95$0 * N[(-1.0 * x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 4e-25], N[(2.0 * N[Power[x, -3.0], $MachinePrecision]), $MachinePrecision], N[(N[(N[(x * x + x), $MachinePrecision] + N[(N[(x + -1.0), $MachinePrecision] * N[(-2.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x + x), $MachinePrecision] * N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\begin{array}{l}
t_0 := x \cdot \left(1 + x\right)\\
t_1 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\
\mathbf{if}\;t_1 \leq -5 \cdot 10^{-21}:\\
\;\;\;\;\frac{\mathsf{fma}\left(-1, t_0, \mathsf{fma}\left(-1, x, 1\right) \cdot \left(x + 2 \cdot \left(-1 - x\right)\right)\right)}{t_0 \cdot \mathsf{fma}\left(-1, x, 1\right)}\\

\mathbf{elif}\;t_1 \leq 4 \cdot 10^{-25}:\\
\;\;\;\;2 \cdot {x}^{-3}\\

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


\end{array}

Error

Target

Original9.6
Target0.3
Herbie0.0
\[\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))) < -4.99999999999999973e-21

    1. Initial program 0.4

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

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

    if -4.99999999999999973e-21 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 4.00000000000000015e-25

    1. Initial program 19.2

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

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

      \[\leadsto \color{blue}{{x}^{-3} \cdot 2} \]

    if 4.00000000000000015e-25 < (+.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{\frac{x - \left(1 + x\right) \cdot 2}{1 + x}}{x}} + \frac{1}{x - 1} \]
    3. Taylor expanded in x around 0 0.6

      \[\leadsto \frac{\frac{\color{blue}{-1 \cdot x - 2}}{1 + x}}{x} + \frac{1}{x - 1} \]
    4. Simplified0.6

      \[\leadsto \frac{\frac{\color{blue}{-2 - x}}{1 + x}}{x} + \frac{1}{x - 1} \]
      Proof
      (-.f64 -2 x): 0 points increase in error, 0 points decrease in error
      (Rewrite<= unsub-neg_binary64 (+.f64 -2 (neg.f64 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 -2 (Rewrite<= mul-1-neg_binary64 (*.f64 -1 x))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 -1 x) -2)): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 -1 x) (Rewrite<= metadata-eval (neg.f64 2))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= sub-neg_binary64 (-.f64 (*.f64 -1 x) 2)): 0 points increase in error, 0 points decrease in error
    5. Applied egg-rr0.1

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

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

Alternatives

Alternative 1
Error0.0
Cost16072
\[\begin{array}{l} t_0 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\ t_1 := \frac{\mathsf{fma}\left(x, x, x\right) + \left(x + -1\right) \cdot \left(-2 - x\right)}{\mathsf{fma}\left(x, x, x\right) \cdot \left(x + -1\right)}\\ \mathbf{if}\;t_0 \leq -5 \cdot 10^{-21}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 4 \cdot 10^{-25}:\\ \;\;\;\;2 \cdot {x}^{-3}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error0.3
Cost13508
\[\begin{array}{l} \mathbf{if}\;x \leq -2573.6638080274306:\\ \;\;\;\;\frac{2}{{x}^{5}} + \frac{2}{{x}^{3}}\\ \mathbf{elif}\;x \leq 561.973078342743:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\frac{-2 - x}{1 + x}, x + -1, x\right)}{x}}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot {x}^{-3}\\ \end{array} \]
Alternative 3
Error0.1
Cost7752
\[\begin{array}{l} \mathbf{if}\;x \leq -2573.6638080274306:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{\frac{2}{x} + -2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{elif}\;x \leq 561.973078342743:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{-2 - x}{1 + x}, x + -1, x\right)}{x \cdot x - x}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot {x}^{-3}\\ \end{array} \]
Alternative 4
Error0.1
Cost7752
\[\begin{array}{l} \mathbf{if}\;x \leq -2573.6638080274306:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{\frac{2}{x} + -2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{elif}\;x \leq 561.973078342743:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\frac{-2 - x}{1 + x}, x + -1, x\right)}{x}}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot {x}^{-3}\\ \end{array} \]
Alternative 5
Error0.2
Cost6920
\[\begin{array}{l} \mathbf{if}\;x \leq -6885790.316889787:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{\frac{2}{x} + -2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{elif}\;x \leq 561.973078342743:\\ \;\;\;\;\frac{1}{x + -1} + \frac{\frac{-2 - x}{1 + x}}{x}\\ \mathbf{else}:\\ \;\;\;\;2 \cdot {x}^{-3}\\ \end{array} \]
Alternative 6
Error0.3
Cost3400
\[\begin{array}{l} t_0 := \frac{1}{x + -1}\\ t_1 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + t_0\\ \mathbf{if}\;t_1 \leq -1 \cdot 10^{-10}:\\ \;\;\;\;t_0 + \frac{\frac{-2 - x}{1 + x}}{x}\\ \mathbf{elif}\;t_1 \leq 4 \cdot 10^{-25}:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{\frac{2}{x} + -2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;t_0 + \left(x + 2 \cdot \left(-1 - x\right)\right) \cdot \frac{1}{x \cdot \left(1 + x\right)}\\ \end{array} \]
Alternative 7
Error0.3
Cost3272
\[\begin{array}{l} t_0 := \frac{1}{x + -1}\\ t_1 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + t_0\\ t_2 := t_0 + \frac{\frac{-2 - x}{1 + x}}{x}\\ \mathbf{if}\;t_1 \leq -1 \cdot 10^{-10}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 4 \cdot 10^{-25}:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{\frac{2}{x} + -2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 8
Error0.3
Cost3016
\[\begin{array}{l} t_0 := \frac{1}{x + -1}\\ t_1 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + t_0\\ t_2 := t_0 + \frac{\frac{-2 - x}{1 + x}}{x}\\ \mathbf{if}\;t_1 \leq -1 \cdot 10^{-10}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 4 \cdot 10^{-25}:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{-2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 9
Error1.2
Cost1096
\[\begin{array}{l} t_0 := \frac{\frac{2 + \frac{-2}{x}}{x \cdot x}}{x + -1}\\ \mathbf{if}\;x \leq -32.65887040153858:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 7.687349769880901 \cdot 10^{-12}:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Error1.2
Cost1096
\[\begin{array}{l} \mathbf{if}\;x \leq -32.65887040153858:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{-2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{elif}\;x \leq 7.687349769880901 \cdot 10^{-12}:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{2 + \frac{-2}{x}}{x \cdot x}}{x + -1}\\ \end{array} \]
Alternative 11
Error1.4
Cost840
\[\begin{array}{l} t_0 := \frac{\frac{2}{x \cdot x}}{x + -1}\\ \mathbf{if}\;x \leq -32.65887040153858:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 7.687349769880901 \cdot 10^{-12}:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 12
Error1.4
Cost840
\[\begin{array}{l} \mathbf{if}\;x \leq -32.65887040153858:\\ \;\;\;\;\frac{\frac{\frac{2}{x}}{x}}{x + -1}\\ \mathbf{elif}\;x \leq 7.687349769880901 \cdot 10^{-12}:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{2}{x \cdot x}}{x + -1}\\ \end{array} \]
Alternative 13
Error15.9
Cost584
\[\begin{array}{l} t_0 := \frac{\frac{-1}{x}}{x}\\ \mathbf{if}\;x \leq -32.65887040153858:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 7.687349769880901 \cdot 10^{-12}:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 14
Error15.6
Cost584
\[\begin{array}{l} t_0 := \frac{-1}{x \cdot x}\\ \mathbf{if}\;x \leq -32.65887040153858:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 7.687349769880901 \cdot 10^{-12}:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 15
Error15.9
Cost448
\[\frac{\frac{2}{x}}{x + -1} \]
Alternative 16
Error61.8
Cost192
\[\frac{1}{x} \]
Alternative 17
Error30.5
Cost192
\[\frac{-2}{x} \]

Error

Reproduce

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