Average Error: 10.1 → 0.4
Time: 10.6s
Precision: binary64
Cost: 9224
\[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
\[\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 -10:\\ \;\;\;\;t_0 + \left(t_1 + \frac{-2}{x}\right)\\ \mathbf{elif}\;t_2 \leq 10^{-29}:\\ \;\;\;\;\frac{-4 \cdot {x}^{-2}}{\frac{2}{x} + x \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \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)))
        (t_1 (/ 1.0 (+ x -1.0)))
        (t_2 (+ (+ t_0 (/ -2.0 x)) t_1)))
   (if (<= t_2 -10.0)
     (+ t_0 (+ t_1 (/ -2.0 x)))
     (if (<= t_2 1e-29)
       (/ (* -4.0 (pow x -2.0)) (+ (/ 2.0 x) (* x -2.0)))
       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 = 1.0 / (1.0 + x);
	double t_1 = 1.0 / (x + -1.0);
	double t_2 = (t_0 + (-2.0 / x)) + t_1;
	double tmp;
	if (t_2 <= -10.0) {
		tmp = t_0 + (t_1 + (-2.0 / x));
	} else if (t_2 <= 1e-29) {
		tmp = (-4.0 * pow(x, -2.0)) / ((2.0 / x) + (x * -2.0));
	} else {
		tmp = t_2;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((1.0d0 / (x + 1.0d0)) - (2.0d0 / x)) + (1.0d0 / (x - 1.0d0))
end function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = 1.0d0 / (1.0d0 + x)
    t_1 = 1.0d0 / (x + (-1.0d0))
    t_2 = (t_0 + ((-2.0d0) / x)) + t_1
    if (t_2 <= (-10.0d0)) then
        tmp = t_0 + (t_1 + ((-2.0d0) / x))
    else if (t_2 <= 1d-29) then
        tmp = ((-4.0d0) * (x ** (-2.0d0))) / ((2.0d0 / x) + (x * (-2.0d0)))
    else
        tmp = t_2
    end if
    code = tmp
end function
public static double code(double x) {
	return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
public static double code(double x) {
	double t_0 = 1.0 / (1.0 + x);
	double t_1 = 1.0 / (x + -1.0);
	double t_2 = (t_0 + (-2.0 / x)) + t_1;
	double tmp;
	if (t_2 <= -10.0) {
		tmp = t_0 + (t_1 + (-2.0 / x));
	} else if (t_2 <= 1e-29) {
		tmp = (-4.0 * Math.pow(x, -2.0)) / ((2.0 / x) + (x * -2.0));
	} else {
		tmp = t_2;
	}
	return tmp;
}
def code(x):
	return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0))
def code(x):
	t_0 = 1.0 / (1.0 + x)
	t_1 = 1.0 / (x + -1.0)
	t_2 = (t_0 + (-2.0 / x)) + t_1
	tmp = 0
	if t_2 <= -10.0:
		tmp = t_0 + (t_1 + (-2.0 / x))
	elif t_2 <= 1e-29:
		tmp = (-4.0 * math.pow(x, -2.0)) / ((2.0 / x) + (x * -2.0))
	else:
		tmp = 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(1.0 / Float64(1.0 + x))
	t_1 = Float64(1.0 / Float64(x + -1.0))
	t_2 = Float64(Float64(t_0 + Float64(-2.0 / x)) + t_1)
	tmp = 0.0
	if (t_2 <= -10.0)
		tmp = Float64(t_0 + Float64(t_1 + Float64(-2.0 / x)));
	elseif (t_2 <= 1e-29)
		tmp = Float64(Float64(-4.0 * (x ^ -2.0)) / Float64(Float64(2.0 / x) + Float64(x * -2.0)));
	else
		tmp = t_2;
	end
	return tmp
end
function tmp = code(x)
	tmp = ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
end
function tmp_2 = code(x)
	t_0 = 1.0 / (1.0 + x);
	t_1 = 1.0 / (x + -1.0);
	t_2 = (t_0 + (-2.0 / x)) + t_1;
	tmp = 0.0;
	if (t_2 <= -10.0)
		tmp = t_0 + (t_1 + (-2.0 / x));
	elseif (t_2 <= 1e-29)
		tmp = (-4.0 * (x ^ -2.0)) / ((2.0 / x) + (x * -2.0));
	else
		tmp = t_2;
	end
	tmp_2 = 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[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(t$95$0 + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision] + t$95$1), $MachinePrecision]}, If[LessEqual[t$95$2, -10.0], N[(t$95$0 + N[(t$95$1 + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$2, 1e-29], N[(N[(-4.0 * N[Power[x, -2.0], $MachinePrecision]), $MachinePrecision] / N[(N[(2.0 / x), $MachinePrecision] + N[(x * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$2]]]]]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\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 -10:\\
\;\;\;\;t_0 + \left(t_1 + \frac{-2}{x}\right)\\

\mathbf{elif}\;t_2 \leq 10^{-29}:\\
\;\;\;\;\frac{-4 \cdot {x}^{-2}}{\frac{2}{x} + x \cdot -2}\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original10.1
Target0.3
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))) < -10

    1. Initial program 0.0

      \[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
    2. Simplified0.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)))): 1 points increase in error, 2 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

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

    1. Initial program 19.9

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

      \[\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)))): 1 points increase in error, 2 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-rr53.2

      \[\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-rr27.1

      \[\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. Taylor expanded in x around 0 20.3

      \[\leadsto \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}}{\color{blue}{-2 \cdot x} - \frac{-2}{x}} \]
    6. Taylor expanded in x around 0 0.1

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

      \[\leadsto \frac{\color{blue}{-4 \cdot {x}^{-2}}}{-2 \cdot x - \frac{-2}{x}} \]
      Proof
      (*.f64 -4 (pow.f64 x -2)): 0 points increase in error, 0 points decrease in error
      (*.f64 -4 (pow.f64 x (Rewrite<= metadata-eval (*.f64 2 -1)))): 0 points increase in error, 0 points decrease in error
      (*.f64 -4 (Rewrite<= pow-sqr_binary64 (*.f64 (pow.f64 x -1) (pow.f64 x -1)))): 31 points increase in error, 30 points decrease in error
      (*.f64 -4 (*.f64 (Rewrite=> unpow-1_binary64 (/.f64 1 x)) (pow.f64 x -1))): 0 points increase in error, 0 points decrease in error
      (*.f64 -4 (*.f64 (/.f64 1 x) (Rewrite=> unpow-1_binary64 (/.f64 1 x)))): 0 points increase in error, 0 points decrease in error
      (*.f64 -4 (Rewrite=> associate-*l/_binary64 (/.f64 (*.f64 1 (/.f64 1 x)) x))): 17 points increase in error, 10 points decrease in error
      (*.f64 -4 (/.f64 (Rewrite=> *-lft-identity_binary64 (/.f64 1 x)) x)): 0 points increase in error, 0 points decrease in error
      (*.f64 -4 (Rewrite<= associate-/r*_binary64 (/.f64 1 (*.f64 x x)))): 23 points increase in error, 24 points decrease in error
      (*.f64 -4 (/.f64 1 (Rewrite<= unpow2_binary64 (pow.f64 x 2)))): 1 points increase in error, 0 points decrease in error
      (Rewrite=> associate-*r/_binary64 (/.f64 (*.f64 -4 1) (pow.f64 x 2))): 0 points increase in error, 0 points decrease in error
      (/.f64 (Rewrite=> metadata-eval -4) (pow.f64 x 2)): 0 points increase in error, 0 points decrease in error

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

    1. Initial program 1.2

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

Alternatives

Alternative 1
Error0.2
Cost6920
\[\begin{array}{l} \mathbf{if}\;x \leq -1:\\ \;\;\;\;\frac{\frac{-4}{x \cdot x}}{\frac{2}{x} + x \cdot -2}\\ \mathbf{elif}\;x \leq 11500:\\ \;\;\;\;\left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{{x}^{3}}\\ \end{array} \]
Alternative 2
Error0.4
Cost3016
\[\begin{array}{l} t_0 := \left(\frac{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\ \mathbf{if}\;t_0 \leq -10:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 10^{-29}:\\ \;\;\;\;\frac{\frac{-4}{x \cdot x}}{\frac{2}{x} + x \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error0.4
Cost3016
\[\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 -10:\\ \;\;\;\;t_0 + \left(t_1 + \frac{-2}{x}\right)\\ \mathbf{elif}\;t_2 \leq 10^{-29}:\\ \;\;\;\;\frac{\frac{-4}{x \cdot x}}{\frac{2}{x} + x \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 4
Error0.1
Cost1096
\[\begin{array}{l} t_0 := \frac{\frac{-4}{x \cdot x}}{\frac{2}{x} + x \cdot -2}\\ \mathbf{if}\;x \leq -0.0001:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 4 \cdot 10^{-6}:\\ \;\;\;\;\frac{-2}{x} + x \cdot -2\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error10.7
Cost840
\[\begin{array}{l} \mathbf{if}\;x \leq -1:\\ \;\;\;\;\frac{-1}{x} + \frac{1}{x}\\ \mathbf{elif}\;x \leq 0.65:\\ \;\;\;\;\frac{-2}{x} + x \cdot -2\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x + -1} + \frac{-1}{x}\\ \end{array} \]
Alternative 6
Error10.6
Cost840
\[\begin{array}{l} \mathbf{if}\;x \leq -0.65:\\ \;\;\;\;\frac{1}{1 + x} + \frac{-1}{x}\\ \mathbf{elif}\;x \leq 0.65:\\ \;\;\;\;\frac{-2}{x} + x \cdot -2\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x + -1} + \frac{-1}{x}\\ \end{array} \]
Alternative 7
Error11.0
Cost712
\[\begin{array}{l} t_0 := \frac{-1}{x} + \frac{1}{x}\\ \mathbf{if}\;x \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error10.8
Cost712
\[\begin{array}{l} t_0 := \frac{-1}{x} + \frac{1}{x}\\ \mathbf{if}\;x \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1:\\ \;\;\;\;\frac{-2}{x} + x \cdot -2\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 9
Error15.9
Cost584
\[\begin{array}{l} t_0 := \frac{-1}{x \cdot x}\\ \mathbf{if}\;x \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.2 \cdot 10^{+77}:\\ \;\;\;\;1 + \frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Error30.8
Cost192
\[\frac{-2}{x} \]
Alternative 11
Error61.9
Cost64
\[1 \]

Error

Reproduce

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