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

Error

Target

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

Derivation

  1. Initial program 9.7

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

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

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

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

    \[\leadsto \color{blue}{\frac{\frac{2}{x}}{\mathsf{fma}\left(x, x, -1\right)}} \]
    Proof
    (/.f64 (/.f64 2 x) (fma.f64 x x -1)): 0 points increase in error, 0 points decrease in error
    (Rewrite<= associate-/r*_binary64 (/.f64 2 (*.f64 x (fma.f64 x x -1)))): 9 points increase in error, 8 points decrease in error
    (Rewrite<= +-lft-identity_binary64 (+.f64 0 (/.f64 2 (*.f64 x (fma.f64 x x -1))))): 0 points increase in error, 0 points decrease in error
  6. Final simplification0.1

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

Alternatives

Alternative 1
Error0.8
Cost712
\[\begin{array}{l} t_0 := \frac{2}{x \cdot \left(x \cdot x\right)}\\ \mathbf{if}\;x \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1:\\ \;\;\;\;\frac{-2}{x} - x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error0.7
Cost712
\[\begin{array}{l} t_0 := \frac{2}{x \cdot \left(x \cdot x\right)}\\ \mathbf{if}\;x \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1:\\ \;\;\;\;x \cdot -2 + \frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error0.3
Cost704
\[\frac{2}{\left(x \cdot \left(x + 1\right)\right) \cdot \left(x + -1\right)} \]
Alternative 4
Error15.3
Cost584
\[\begin{array}{l} t_0 := \frac{1}{x \cdot x}\\ \mathbf{if}\;x \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1:\\ \;\;\;\;\frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error15.3
Cost584
\[\begin{array}{l} t_0 := \frac{1}{x \cdot x}\\ \mathbf{if}\;x \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1:\\ \;\;\;\;\frac{-2}{x} - x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error56.4
Cost192
\[\frac{-1}{x} \]
Alternative 7
Error30.6
Cost192
\[\frac{-2}{x} \]
Alternative 8
Error61.9
Cost64
\[1 \]

Error

Reproduce

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