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

\mathbf{elif}\;t_3 \leq 10^{-30}:\\
\;\;\;\;\frac{2}{{x}^{3}}\\

\mathbf{else}:\\
\;\;\;\;\frac{-2}{x} + \left(t_1 + t_2\right)\\


\end{array}

Error

Target

Original10.2
Target0.3
Herbie0.6
\[\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.0000000000000002e-23

    1. Initial program 0.5

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

      \[\leadsto \color{blue}{\frac{1}{1 + x} + \left(\frac{1}{x + -1} + \frac{-2}{x}\right)} \]
      Proof
      (+.f64 (/.f64 1 (+.f64 1 x)) (+.f64 (/.f64 1 (+.f64 x -1)) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (Rewrite<= +-commutative_binary64 (+.f64 x 1))) (+.f64 (/.f64 1 (+.f64 x -1)) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (+.f64 x (Rewrite<= metadata-eval (neg.f64 1)))) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (Rewrite<= sub-neg_binary64 (-.f64 x 1))) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (-.f64 x 1)) (/.f64 (Rewrite<= metadata-eval (neg.f64 2)) x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (-.f64 x 1)) (Rewrite<= distribute-neg-frac_binary64 (neg.f64 (/.f64 2 x))))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (Rewrite<= +-commutative_binary64 (+.f64 (neg.f64 (/.f64 2 x)) (/.f64 1 (-.f64 x 1))))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-+l+_binary64 (+.f64 (+.f64 (/.f64 1 (+.f64 x 1)) (neg.f64 (/.f64 2 x))) (/.f64 1 (-.f64 x 1)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= sub-neg_binary64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x))) (/.f64 1 (-.f64 x 1))): 0 points increase in error, 0 points decrease in error
    3. Applied egg-rr0.0

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

    if -5.0000000000000002e-23 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 1e-30

    1. Initial program 20.0

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

      \[\leadsto \color{blue}{\frac{1}{1 + x} + \left(\frac{1}{x + -1} + \frac{-2}{x}\right)} \]
      Proof
      (+.f64 (/.f64 1 (+.f64 1 x)) (+.f64 (/.f64 1 (+.f64 x -1)) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (Rewrite<= +-commutative_binary64 (+.f64 x 1))) (+.f64 (/.f64 1 (+.f64 x -1)) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (+.f64 x (Rewrite<= metadata-eval (neg.f64 1)))) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (Rewrite<= sub-neg_binary64 (-.f64 x 1))) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (-.f64 x 1)) (/.f64 (Rewrite<= metadata-eval (neg.f64 2)) x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (-.f64 x 1)) (Rewrite<= distribute-neg-frac_binary64 (neg.f64 (/.f64 2 x))))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (Rewrite<= +-commutative_binary64 (+.f64 (neg.f64 (/.f64 2 x)) (/.f64 1 (-.f64 x 1))))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-+l+_binary64 (+.f64 (+.f64 (/.f64 1 (+.f64 x 1)) (neg.f64 (/.f64 2 x))) (/.f64 1 (-.f64 x 1)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= sub-neg_binary64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x))) (/.f64 1 (-.f64 x 1))): 0 points increase in error, 0 points decrease in error
    3. Applied egg-rr20.0

      \[\leadsto \frac{1}{1 + x} + \color{blue}{\frac{\frac{\mathsf{fma}\left(x + -1, -2, x\right)}{x + -1}}{x}} \]
    4. Taylor expanded in x around 0 20.0

      \[\leadsto \frac{1}{1 + x} + \frac{\frac{\color{blue}{2 + -1 \cdot x}}{x + -1}}{x} \]
    5. Simplified20.0

      \[\leadsto \frac{1}{1 + x} + \frac{\frac{\color{blue}{2 - x}}{x + -1}}{x} \]
      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
    6. Applied egg-rr20.0

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

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

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

    1. Initial program 1.3

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

      \[\leadsto \color{blue}{\frac{1}{1 + x} + \left(\frac{1}{x + -1} + \frac{-2}{x}\right)} \]
      Proof
      (+.f64 (/.f64 1 (+.f64 1 x)) (+.f64 (/.f64 1 (+.f64 x -1)) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (Rewrite<= +-commutative_binary64 (+.f64 x 1))) (+.f64 (/.f64 1 (+.f64 x -1)) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (+.f64 x (Rewrite<= metadata-eval (neg.f64 1)))) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (Rewrite<= sub-neg_binary64 (-.f64 x 1))) (/.f64 -2 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (-.f64 x 1)) (/.f64 (Rewrite<= metadata-eval (neg.f64 2)) x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (+.f64 (/.f64 1 (-.f64 x 1)) (Rewrite<= distribute-neg-frac_binary64 (neg.f64 (/.f64 2 x))))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 1 (+.f64 x 1)) (Rewrite<= +-commutative_binary64 (+.f64 (neg.f64 (/.f64 2 x)) (/.f64 1 (-.f64 x 1))))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-+l+_binary64 (+.f64 (+.f64 (/.f64 1 (+.f64 x 1)) (neg.f64 (/.f64 2 x))) (/.f64 1 (-.f64 x 1)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= sub-neg_binary64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x))) (/.f64 1 (-.f64 x 1))): 0 points increase in error, 0 points decrease in error
    3. Applied egg-rr1.3

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

      \[\leadsto \color{blue}{\frac{\left(\frac{1}{1 + x} + \frac{1}{x + -1}\right) \cdot \left(\frac{1}{1 + x} + \frac{1}{x + -1}\right) - \frac{4}{x \cdot x}}{\left(\frac{1}{1 + x} + \frac{1}{x + -1}\right) - \frac{-2}{x}}} \]
    5. Applied egg-rr1.4

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

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

Alternatives

Alternative 1
Error0.8
Cost15432
\[\begin{array}{l} t_0 := \frac{1}{1 + x}\\ t_1 := \frac{1}{x + -1}\\ t_2 := \left(t_0 - \frac{2}{x}\right) + t_1\\ \mathbf{if}\;t_2 \leq -50:\\ \;\;\;\;t_0 + \frac{\frac{2 - x}{x + -1}}{x}\\ \mathbf{elif}\;t_2 \leq 10^{-30}:\\ \;\;\;\;\frac{2}{{x}^{3}} + \frac{2}{{x}^{5}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-2}{x} + \left(t_0 + t_1\right)\\ \end{array} \]
Alternative 2
Error0.9
Cost8712
\[\begin{array}{l} t_0 := \frac{1}{1 + x}\\ t_1 := \frac{1}{x + -1}\\ t_2 := \left(t_0 - \frac{2}{x}\right) + t_1\\ \mathbf{if}\;t_2 \leq -50:\\ \;\;\;\;t_0 + \frac{\frac{2 - x}{x + -1}}{x}\\ \mathbf{elif}\;t_2 \leq 10^{-30}:\\ \;\;\;\;\frac{2}{{x}^{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-2}{x} + \left(t_0 + t_1\right)\\ \end{array} \]
Alternative 3
Error10.2
Cost1088
\[\frac{1}{1 + x} + \frac{-1}{1 - x} \cdot \frac{2 - x}{x} \]
Alternative 4
Error10.2
Cost960
\[\frac{-2}{x} + \left(\frac{1}{1 + x} + \frac{1}{x + -1}\right) \]
Alternative 5
Error15.6
Cost584
\[\begin{array}{l} t_0 := \frac{-1}{x \cdot x}\\ \mathbf{if}\;x \leq -3808397.5679019354:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.027639669428479896:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error15.5
Cost584
\[\begin{array}{l} t_0 := \frac{-1}{x \cdot x}\\ \mathbf{if}\;x \leq -3808397.5679019354:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.027639669428479896:\\ \;\;\;\;\frac{-2}{x} - x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error11.2
Cost448
\[1 + \left(-1 - \frac{2}{x}\right) \]
Alternative 8
Error61.7
Cost192
\[\frac{1}{x} \]
Alternative 9
Error30.9
Cost192
\[\frac{-2}{x} \]
Alternative 10
Error61.9
Cost64
\[-1 \]

Error

Reproduce

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