Average Error: 9.5 → 0.1
Time: 11.0s
Precision: binary64
Cost: 13440
\[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
\[\frac{\frac{-2}{\mathsf{fma}\left(x, -1, -1\right)}}{\mathsf{fma}\left(x, x, -x\right)} \]
(FPCore (x)
 :precision binary64
 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
(FPCore (x) :precision binary64 (/ (/ -2.0 (fma x -1.0 -1.0)) (fma x x (- x))))
double code(double x) {
	return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
double code(double x) {
	return (-2.0 / fma(x, -1.0, -1.0)) / fma(x, x, -x);
}
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)
	return Float64(Float64(-2.0 / fma(x, -1.0, -1.0)) / fma(x, x, Float64(-x)))
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_] := N[(N[(-2.0 / N[(x * -1.0 + -1.0), $MachinePrecision]), $MachinePrecision] / N[(x * x + (-x)), $MachinePrecision]), $MachinePrecision]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\frac{\frac{-2}{\mathsf{fma}\left(x, -1, -1\right)}}{\mathsf{fma}\left(x, x, -x\right)}

Error

Target

Original9.5
Target0.2
Herbie0.1
\[\frac{2}{x \cdot \left(x \cdot x - 1\right)} \]

Derivation

  1. Initial program 9.5

    \[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \]
  2. Simplified9.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-rr25.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)}} \]
  4. Taylor expanded in x around 0 0.2

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

    \[\leadsto \color{blue}{0 + \frac{\frac{-2}{\mathsf{fma}\left(x, -1, -1\right)}}{\mathsf{fma}\left(x, x, -x\right)}} \]
  6. Final simplification0.1

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

Alternatives

Alternative 1
Error0.1
Cost13568
\[-2 \cdot \frac{\frac{1}{\mathsf{fma}\left(x, x, -x\right)}}{\mathsf{fma}\left(x, -1, -1\right)} \]
Alternative 2
Error0.3
Cost8712
\[\begin{array}{l} t_0 := \left(\frac{1}{x + 1} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\ t_1 := \frac{-2}{x \cdot \left(1 - x\right) - x \cdot \left(x \cdot \left(x + -1\right)\right)}\\ \mathbf{if}\;t_0 \leq -5 \cdot 10^{-24}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;\frac{2}{{x}^{3}}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error0.2
Cost7104
\[\frac{-2}{\mathsf{fma}\left(x, -1, -1\right) \cdot \left(x \cdot \left(x + -1\right)\right)} \]
Alternative 4
Error0.1
Cost7104
\[\frac{-1}{\mathsf{fma}\left(x, x, x\right)} \cdot \frac{-2}{x + -1} \]
Alternative 5
Error0.2
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq -1.0910662012536967 \cdot 10^{+63}:\\ \;\;\;\;\frac{\frac{2}{x}}{\mathsf{fma}\left(x, x, -x\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{-2}{x \cdot \left(1 - x\right) - x \cdot \left(x \cdot \left(x + -1\right)\right)}\\ \end{array} \]
Alternative 6
Error0.2
Cost1092
\[\begin{array}{l} \mathbf{if}\;x \leq -6.577260038621995 \cdot 10^{+105}:\\ \;\;\;\;\frac{1}{x} + \frac{-1}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-2}{x \cdot \left(1 - x\right) - x \cdot \left(x \cdot \left(x + -1\right)\right)}\\ \end{array} \]
Alternative 7
Error9.5
Cost960
\[\frac{-2}{x} + \left(\frac{1}{x + 1} + \frac{1}{x + -1}\right) \]
Alternative 8
Error9.5
Cost960
\[\frac{1}{x + 1} + \left(\frac{1}{x + -1} + \frac{-2}{x}\right) \]
Alternative 9
Error10.3
Cost712
\[\begin{array}{l} t_0 := \frac{1}{x} + \frac{-1}{x}\\ \mathbf{if}\;x \leq -707.7274072453035:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.10170565312564367:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Error15.1
Cost584
\[\begin{array}{l} t_0 := \frac{-1}{x \cdot x}\\ \mathbf{if}\;x \leq -707.7274072453035:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.10170565312564367:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 11
Error14.9
Cost584
\[\begin{array}{l} \mathbf{if}\;x \leq -707.7274072453035:\\ \;\;\;\;\frac{-1}{x \cdot x}\\ \mathbf{elif}\;x \leq 0.10170565312564367:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x \cdot x}\\ \end{array} \]
Alternative 12
Error10.3
Cost448
\[1 + \left(-1 - \frac{2}{x}\right) \]
Alternative 13
Error56.3
Cost192
\[\frac{-1}{x} \]
Alternative 14
Error30.3
Cost192
\[\frac{-2}{x} \]

Error

Reproduce

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