| Alternative 1 | |
|---|---|
| Error | 0.8 |
| Cost | 8713 |
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
(FPCore (x)
:precision binary64
(let* ((t_0 (fma x x (- x)))
(t_1 (+ (+ (/ -2.0 x) (/ 1.0 (+ 1.0 x))) (/ 1.0 (+ x -1.0)))))
(if (<= t_1 -10.0)
(+ (/ 2.0 (/ (+ -1.0 (* x x)) x)) (/ -2.0 x))
(if (<= t_1 0.0)
(/ 2.0 (pow x 3.0))
(/
(- (* (+ (* x 2.0) (- -2.0 x)) (+ 1.0 x)) t_0)
(* t_0 (- -1.0 x)))))))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 = fma(x, x, -x);
double t_1 = ((-2.0 / x) + (1.0 / (1.0 + x))) + (1.0 / (x + -1.0));
double tmp;
if (t_1 <= -10.0) {
tmp = (2.0 / ((-1.0 + (x * x)) / x)) + (-2.0 / x);
} else if (t_1 <= 0.0) {
tmp = 2.0 / pow(x, 3.0);
} else {
tmp = ((((x * 2.0) + (-2.0 - x)) * (1.0 + x)) - t_0) / (t_0 * (-1.0 - x));
}
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 = fma(x, x, Float64(-x)) t_1 = Float64(Float64(Float64(-2.0 / x) + Float64(1.0 / Float64(1.0 + x))) + Float64(1.0 / Float64(x + -1.0))) tmp = 0.0 if (t_1 <= -10.0) tmp = Float64(Float64(2.0 / Float64(Float64(-1.0 + Float64(x * x)) / x)) + Float64(-2.0 / x)); elseif (t_1 <= 0.0) tmp = Float64(2.0 / (x ^ 3.0)); else tmp = Float64(Float64(Float64(Float64(Float64(x * 2.0) + Float64(-2.0 - x)) * Float64(1.0 + x)) - t_0) / Float64(t_0 * Float64(-1.0 - x))); 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 * x + (-x)), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(-2.0 / x), $MachinePrecision] + N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -10.0], N[(N[(2.0 / N[(N[(-1.0 + N[(x * x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 0.0], N[(2.0 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(N[(x * 2.0), $MachinePrecision] + N[(-2.0 - x), $MachinePrecision]), $MachinePrecision] * N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision] / N[(t$95$0 * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\begin{array}{l}
t_0 := \mathsf{fma}\left(x, x, -x\right)\\
t_1 := \left(\frac{-2}{x} + \frac{1}{1 + x}\right) + \frac{1}{x + -1}\\
\mathbf{if}\;t_1 \leq -10:\\
\;\;\;\;\frac{2}{\frac{-1 + x \cdot x}{x}} + \frac{-2}{x}\\
\mathbf{elif}\;t_1 \leq 0:\\
\;\;\;\;\frac{2}{{x}^{3}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(x \cdot 2 + \left(-2 - x\right)\right) \cdot \left(1 + x\right) - t_0}{t_0 \cdot \left(-1 - x\right)}\\
\end{array}
| Original | 10.2 |
|---|---|
| Target | 0.3 |
| Herbie | 0.7 |
if (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < -10Initial program 0.0
Simplified0.0
[Start]0.0 | \[ \left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\] |
|---|---|
associate-+l- [=>]0.0 | \[ \color{blue}{\frac{1}{x + 1} - \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]0.0 | \[ \color{blue}{\frac{1}{x + 1} + \left(-\left(\frac{2}{x} - \frac{1}{x - 1}\right)\right)}
\] |
neg-mul-1 [=>]0.0 | \[ \frac{1}{x + 1} + \color{blue}{-1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
metadata-eval [<=]0.0 | \[ \frac{1}{x + 1} + \color{blue}{\left(-1\right)} \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
cancel-sign-sub-inv [<=]0.0 | \[ \color{blue}{\frac{1}{x + 1} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
+-commutative [=>]0.0 | \[ \frac{1}{\color{blue}{1 + x}} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
*-lft-identity [=>]0.0 | \[ \frac{1}{1 + x} - \color{blue}{\left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]0.0 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{\color{blue}{x + \left(-1\right)}}\right)
\] |
metadata-eval [=>]0.0 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{x + \color{blue}{-1}}\right)
\] |
Applied egg-rr0.0
Applied egg-rr0.0
Simplified0.0
[Start]0.0 | \[ \frac{-\left(x + x\right)}{-\mathsf{fma}\left(x, x, -1\right)} + \left(-\frac{2}{x}\right)
\] |
|---|---|
neg-mul-1 [=>]0.0 | \[ \frac{\color{blue}{-1 \cdot \left(x + x\right)}}{-\mathsf{fma}\left(x, x, -1\right)} + \left(-\frac{2}{x}\right)
\] |
count-2 [=>]0.0 | \[ \frac{-1 \cdot \color{blue}{\left(2 \cdot x\right)}}{-\mathsf{fma}\left(x, x, -1\right)} + \left(-\frac{2}{x}\right)
\] |
associate-*r* [=>]0.0 | \[ \frac{\color{blue}{\left(-1 \cdot 2\right) \cdot x}}{-\mathsf{fma}\left(x, x, -1\right)} + \left(-\frac{2}{x}\right)
\] |
metadata-eval [=>]0.0 | \[ \frac{\color{blue}{-2} \cdot x}{-\mathsf{fma}\left(x, x, -1\right)} + \left(-\frac{2}{x}\right)
\] |
neg-mul-1 [=>]0.0 | \[ \frac{-2 \cdot x}{\color{blue}{-1 \cdot \mathsf{fma}\left(x, x, -1\right)}} + \left(-\frac{2}{x}\right)
\] |
fma-udef [=>]0.0 | \[ \frac{-2 \cdot x}{-1 \cdot \color{blue}{\left(x \cdot x + -1\right)}} + \left(-\frac{2}{x}\right)
\] |
distribute-lft-in [=>]0.0 | \[ \frac{-2 \cdot x}{\color{blue}{-1 \cdot \left(x \cdot x\right) + -1 \cdot -1}} + \left(-\frac{2}{x}\right)
\] |
associate-*l* [<=]0.0 | \[ \frac{-2 \cdot x}{\color{blue}{\left(-1 \cdot x\right) \cdot x} + -1 \cdot -1} + \left(-\frac{2}{x}\right)
\] |
neg-mul-1 [<=]0.0 | \[ \frac{-2 \cdot x}{\color{blue}{\left(-x\right)} \cdot x + -1 \cdot -1} + \left(-\frac{2}{x}\right)
\] |
metadata-eval [=>]0.0 | \[ \frac{-2 \cdot x}{\left(-x\right) \cdot x + \color{blue}{1}} + \left(-\frac{2}{x}\right)
\] |
+-commutative [<=]0.0 | \[ \frac{-2 \cdot x}{\color{blue}{1 + \left(-x\right) \cdot x}} + \left(-\frac{2}{x}\right)
\] |
cancel-sign-sub-inv [<=]0.0 | \[ \frac{-2 \cdot x}{\color{blue}{1 - x \cdot x}} + \left(-\frac{2}{x}\right)
\] |
Applied egg-rr0.0
Simplified0.0
[Start]0.0 | \[ \frac{-2}{-1 + x \cdot x} \cdot \left(-x\right) + \left(-\frac{2}{x}\right)
\] |
|---|---|
distribute-rgt-neg-out [=>]0.0 | \[ \color{blue}{\left(-\frac{-2}{-1 + x \cdot x} \cdot x\right)} + \left(-\frac{2}{x}\right)
\] |
associate-*l/ [=>]0.0 | \[ \left(-\color{blue}{\frac{-2 \cdot x}{-1 + x \cdot x}}\right) + \left(-\frac{2}{x}\right)
\] |
neg-mul-1 [=>]0.0 | \[ \color{blue}{-1 \cdot \frac{-2 \cdot x}{-1 + x \cdot x}} + \left(-\frac{2}{x}\right)
\] |
associate-/l* [=>]0.0 | \[ -1 \cdot \color{blue}{\frac{-2}{\frac{-1 + x \cdot x}{x}}} + \left(-\frac{2}{x}\right)
\] |
associate-*r/ [=>]0.0 | \[ \color{blue}{\frac{-1 \cdot -2}{\frac{-1 + x \cdot x}{x}}} + \left(-\frac{2}{x}\right)
\] |
metadata-eval [=>]0.0 | \[ \frac{\color{blue}{2}}{\frac{-1 + x \cdot x}{x}} + \left(-\frac{2}{x}\right)
\] |
if -10 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 0.0Initial program 19.8
Simplified19.8
[Start]19.8 | \[ \left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\] |
|---|---|
associate-+l- [=>]19.8 | \[ \color{blue}{\frac{1}{x + 1} - \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]19.8 | \[ \color{blue}{\frac{1}{x + 1} + \left(-\left(\frac{2}{x} - \frac{1}{x - 1}\right)\right)}
\] |
neg-mul-1 [=>]19.8 | \[ \frac{1}{x + 1} + \color{blue}{-1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
metadata-eval [<=]19.8 | \[ \frac{1}{x + 1} + \color{blue}{\left(-1\right)} \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
cancel-sign-sub-inv [<=]19.8 | \[ \color{blue}{\frac{1}{x + 1} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
+-commutative [=>]19.8 | \[ \frac{1}{\color{blue}{1 + x}} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
*-lft-identity [=>]19.8 | \[ \frac{1}{1 + x} - \color{blue}{\left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]19.8 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{\color{blue}{x + \left(-1\right)}}\right)
\] |
metadata-eval [=>]19.8 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{x + \color{blue}{-1}}\right)
\] |
Taylor expanded in x around inf 1.0
if 0.0 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) Initial program 1.3
Simplified1.3
[Start]1.3 | \[ \left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\] |
|---|---|
associate-+l- [=>]1.3 | \[ \color{blue}{\frac{1}{x + 1} - \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]1.3 | \[ \color{blue}{\frac{1}{x + 1} + \left(-\left(\frac{2}{x} - \frac{1}{x - 1}\right)\right)}
\] |
neg-mul-1 [=>]1.3 | \[ \frac{1}{x + 1} + \color{blue}{-1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
metadata-eval [<=]1.3 | \[ \frac{1}{x + 1} + \color{blue}{\left(-1\right)} \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
cancel-sign-sub-inv [<=]1.3 | \[ \color{blue}{\frac{1}{x + 1} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
+-commutative [=>]1.3 | \[ \frac{1}{\color{blue}{1 + x}} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
*-lft-identity [=>]1.3 | \[ \frac{1}{1 + x} - \color{blue}{\left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]1.3 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{\color{blue}{x + \left(-1\right)}}\right)
\] |
metadata-eval [=>]1.3 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{x + \color{blue}{-1}}\right)
\] |
Applied egg-rr0.7
Simplified0.7
[Start]0.7 | \[ \frac{-1 \cdot \mathsf{fma}\left(x, x, -x\right) - \left(-1 - x\right) \cdot \left(-2 + \left(2 \cdot x - x\right)\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
|---|---|
mul-1-neg [=>]0.7 | \[ \frac{\color{blue}{\left(-\mathsf{fma}\left(x, x, -x\right)\right)} - \left(-1 - x\right) \cdot \left(-2 + \left(2 \cdot x - x\right)\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
*-commutative [=>]0.7 | \[ \frac{\left(-\mathsf{fma}\left(x, x, -x\right)\right) - \color{blue}{\left(-2 + \left(2 \cdot x - x\right)\right) \cdot \left(-1 - x\right)}}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
+-commutative [=>]0.7 | \[ \frac{\left(-\mathsf{fma}\left(x, x, -x\right)\right) - \color{blue}{\left(\left(2 \cdot x - x\right) + -2\right)} \cdot \left(-1 - x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
associate-+l- [=>]0.7 | \[ \frac{\left(-\mathsf{fma}\left(x, x, -x\right)\right) - \color{blue}{\left(2 \cdot x - \left(x - -2\right)\right)} \cdot \left(-1 - x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
*-commutative [=>]0.7 | \[ \frac{\left(-\mathsf{fma}\left(x, x, -x\right)\right) - \left(\color{blue}{x \cdot 2} - \left(x - -2\right)\right) \cdot \left(-1 - x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
*-commutative [=>]0.7 | \[ \frac{\left(-\mathsf{fma}\left(x, x, -x\right)\right) - \left(x \cdot 2 - \left(x - -2\right)\right) \cdot \left(-1 - x\right)}{\color{blue}{\mathsf{fma}\left(x, x, -x\right) \cdot \left(-1 - x\right)}}
\] |
Final simplification0.7
| Alternative 1 | |
|---|---|
| Error | 0.8 |
| Cost | 8713 |
| Alternative 2 | |
|---|---|
| Error | 10.2 |
| Cost | 1096 |
| Alternative 3 | |
|---|---|
| Error | 10.2 |
| Cost | 1096 |
| Alternative 4 | |
|---|---|
| Error | 10.2 |
| Cost | 1088 |
| Alternative 5 | |
|---|---|
| Error | 10.2 |
| Cost | 960 |
| Alternative 6 | |
|---|---|
| Error | 10.2 |
| Cost | 960 |
| Alternative 7 | |
|---|---|
| Error | 11.1 |
| Cost | 448 |
| Alternative 8 | |
|---|---|
| Error | 31.1 |
| Cost | 192 |
| Alternative 9 | |
|---|---|
| Error | 61.9 |
| Cost | 64 |
herbie shell --seed 2023055
(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))))