Average Error: 9.7 → 0.0
Time: 14.6s
Precision: binary64
Cost: 22984
\[\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 -2 \cdot 10^{-10}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, x, x\right) + \left(x + -1\right) \cdot \left(x - \mathsf{fma}\left(x, 2, 2\right)\right)}{\mathsf{fma}\left(x, x, x\right) \cdot \left(x + -1\right)}\\ \mathbf{elif}\;t_1 \leq 10^{-12}:\\ \;\;\;\;2 \cdot \left({x}^{-5} + {x}^{-3}\right)\\ \mathbf{else}:\\ \;\;\;\;\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)}\\ \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 -2e-10)
     (/
      (+ (fma x x x) (* (+ x -1.0) (- x (fma x 2.0 2.0))))
      (* (fma x x x) (+ x -1.0)))
     (if (<= t_1 1e-12)
       (* 2.0 (+ (pow x -5.0) (pow x -3.0)))
       (/
        (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)))))))
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 <= -2e-10) {
		tmp = (fma(x, x, x) + ((x + -1.0) * (x - fma(x, 2.0, 2.0)))) / (fma(x, x, x) * (x + -1.0));
	} else if (t_1 <= 1e-12) {
		tmp = 2.0 * (pow(x, -5.0) + pow(x, -3.0));
	} else {
		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));
	}
	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 <= -2e-10)
		tmp = Float64(Float64(fma(x, x, x) + Float64(Float64(x + -1.0) * Float64(x - fma(x, 2.0, 2.0)))) / Float64(fma(x, x, x) * Float64(x + -1.0)));
	elseif (t_1 <= 1e-12)
		tmp = Float64(2.0 * Float64((x ^ -5.0) + (x ^ -3.0)));
	else
		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)));
	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, -2e-10], N[(N[(N[(x * x + x), $MachinePrecision] + N[(N[(x + -1.0), $MachinePrecision] * N[(x - N[(x * 2.0 + 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x + x), $MachinePrecision] * N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 1e-12], N[(2.0 * N[(N[Power[x, -5.0], $MachinePrecision] + N[Power[x, -3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 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]]]]]
\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 -2 \cdot 10^{-10}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, x, x\right) + \left(x + -1\right) \cdot \left(x - \mathsf{fma}\left(x, 2, 2\right)\right)}{\mathsf{fma}\left(x, x, x\right) \cdot \left(x + -1\right)}\\

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

\mathbf{else}:\\
\;\;\;\;\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)}\\


\end{array}

Error

Target

Original9.7
Target0.2
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))) < -2.00000000000000007e-10

    1. Initial program 0.1

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

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

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

    if -2.00000000000000007e-10 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 9.9999999999999998e-13

    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.4

      \[\leadsto \color{blue}{2 \cdot \frac{1}{{x}^{5}} + 2 \cdot \frac{1}{{x}^{3}}} \]
    3. Simplified0.4

      \[\leadsto \color{blue}{\frac{2}{{x}^{5}} + \frac{2}{{x}^{3}}} \]
      Proof
      (+.f64 (/.f64 2 (pow.f64 x 5)) (/.f64 2 (pow.f64 x 3))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 (Rewrite<= metadata-eval (*.f64 2 1)) (pow.f64 x 5)) (/.f64 2 (pow.f64 x 3))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= associate-*r/_binary64 (*.f64 2 (/.f64 1 (pow.f64 x 5)))) (/.f64 2 (pow.f64 x 3))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 2 (/.f64 1 (pow.f64 x 5))) (/.f64 (Rewrite<= metadata-eval (*.f64 2 1)) (pow.f64 x 3))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 2 (/.f64 1 (pow.f64 x 5))) (Rewrite<= associate-*r/_binary64 (*.f64 2 (/.f64 1 (pow.f64 x 3))))): 0 points increase in error, 0 points decrease in error
    4. Applied egg-rr0.5

      \[\leadsto \frac{2}{{x}^{5}} + \frac{2}{\color{blue}{\left(x \cdot x\right) \cdot x}} \]
    5. Applied egg-rr0.0

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

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

    1. Initial program 0.2

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

Alternatives

Alternative 1
Error0.0
Cost22600
\[\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(x - \mathsf{fma}\left(x, 2, 2\right)\right)}{\mathsf{fma}\left(x, x, x\right) \cdot \left(x + -1\right)}\\ \mathbf{if}\;t_0 \leq -2 \cdot 10^{-10}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 10^{-12}:\\ \;\;\;\;2 \cdot \left({x}^{-5} + {x}^{-3}\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error0.3
Cost20360
\[\begin{array}{l} \mathbf{if}\;x \leq -1972.7549491055456:\\ \;\;\;\;2 \cdot \left({x}^{-5} + {x}^{-3}\right)\\ \mathbf{elif}\;x \leq 0.05591673386039308:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\frac{x - \mathsf{fma}\left(x, 2, 2\right)}{1 + x}, x + -1, x\right)}{x}}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{{x}^{5}} + \left(\frac{2}{{x}^{3}} + \frac{2}{{x}^{7}}\right)\\ \end{array} \]
Alternative 3
Error0.2
Cost16072
\[\begin{array}{l} t_0 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\ \mathbf{if}\;t_0 \leq -20:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 10^{-12}:\\ \;\;\;\;2 \cdot \left({x}^{-5} + {x}^{-3}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\frac{x - \mathsf{fma}\left(x, 2, 2\right)}{1 + x}, x + -1, x\right)}{x}}{x + -1}\\ \end{array} \]
Alternative 4
Error0.2
Cost15304
\[\begin{array}{l} t_0 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\ \mathbf{if}\;t_0 \leq -20:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 10^{-12}:\\ \;\;\;\;2 \cdot \left({x}^{-5} + {x}^{-3}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\frac{-2 - x}{1 + x}, x + -1, x\right)}{x}}{x + -1}\\ \end{array} \]
Alternative 5
Error0.5
Cost7752
\[\begin{array}{l} \mathbf{if}\;x \leq -51841659923.4166:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{-2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{elif}\;x \leq 365.63712801122784:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\frac{-2 - x}{1 + x}, x + -1, x\right)}{x}}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{{x}^{5}} + \frac{2}{x \cdot \left(x \cdot x\right)}\\ \end{array} \]
Alternative 6
Error0.5
Cost7432
\[\begin{array}{l} \mathbf{if}\;x \leq -51841659923.4166:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{-2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{elif}\;x \leq 365.63712801122784:\\ \;\;\;\;\frac{1}{x + -1} + \frac{x + 2 \cdot \left(-1 - x\right)}{x \cdot \left(1 + x\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{{x}^{5}} + \frac{2}{x \cdot \left(x \cdot x\right)}\\ \end{array} \]
Alternative 7
Error0.2
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{x + 2 \cdot \left(-1 - x\right)}{x \cdot \left(1 + x\right)}\\ \mathbf{if}\;t_1 \leq -2 \cdot 10^{-10}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{-14}:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{-2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 8
Error0.2
Cost3144
\[\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 -2 \cdot 10^{-10}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{-14}:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{-2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;t_0 + \left(-2 - x\right) \cdot \frac{1}{x \cdot \left(1 + x\right)}\\ \end{array} \]
Alternative 9
Error0.2
Cost3144
\[\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 -2 \cdot 10^{-10}:\\ \;\;\;\;\frac{\frac{x + \left(x + -1\right) \cdot \frac{-2 - x}{1 + x}}{x}}{x + -1}\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{-14}:\\ \;\;\;\;\frac{\frac{\frac{2}{x} + \frac{-2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;t_0 + \left(-2 - x\right) \cdot \frac{1}{x \cdot \left(1 + x\right)}\\ \end{array} \]
Alternative 10
Error0.7
Cost1224
\[\begin{array}{l} t_0 := \frac{\frac{\frac{2}{x} + \frac{-2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{if}\;x \leq -1972.7549491055456:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.05591673386039308:\\ \;\;\;\;x \cdot -2 + \frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 11
Error0.5
Cost1224
\[\begin{array}{l} t_0 := \frac{\frac{\frac{2}{x} + \frac{-2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{if}\;x \leq -51841659923.4166:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 365.63712801122784:\\ \;\;\;\;\frac{1}{x + -1} + \frac{\frac{-2 - x}{1 + x}}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 12
Error0.5
Cost1224
\[\begin{array}{l} t_0 := \frac{\frac{\frac{2}{x} + \frac{-2}{x \cdot x}}{x}}{x + -1}\\ \mathbf{if}\;x \leq -51841659923.4166:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 365.63712801122784:\\ \;\;\;\;\left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 13
Error0.7
Cost1096
\[\begin{array}{l} t_0 := \frac{\frac{2 + \frac{-2}{x}}{x \cdot x}}{x + -1}\\ \mathbf{if}\;x \leq -1972.7549491055456:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.05591673386039308:\\ \;\;\;\;x \cdot -2 + \frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 14
Error1.0
Cost840
\[\begin{array}{l} t_0 := \frac{\frac{2}{x \cdot x}}{x + -1}\\ \mathbf{if}\;x \leq -1972.7549491055456:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.05591673386039308:\\ \;\;\;\;x \cdot -2 + \frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 15
Error1.0
Cost840
\[\begin{array}{l} t_0 := \frac{\frac{\frac{2}{x}}{x}}{x + -1}\\ \mathbf{if}\;x \leq -1972.7549491055456:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.05591673386039308:\\ \;\;\;\;x \cdot -2 + \frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 16
Error15.9
Cost448
\[\frac{\frac{2}{x}}{x + -1} \]
Alternative 17
Error10.6
Cost448
\[-1 + \left(1 + \frac{-2}{x}\right) \]
Alternative 18
Error31.1
Cost192
\[\frac{-2}{x} \]
Alternative 19
Error61.9
Cost64
\[1 \]

Error

Reproduce

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