| Alternative 1 | |
|---|---|
| Error | 0.4 |
| Cost | 9092 |
(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 (+ (+ (/ 1.0 (+ 1.0 x)) (/ -2.0 x)) (/ 1.0 (+ x -1.0))))
(t_2 (* (+ 1.0 x) (- (- -2.0 x) (* x -2.0)))))
(if (<= t_1 -1e-19)
(/ (- (* x x) (+ x t_2)) (* (+ 1.0 x) t_0))
(if (<= t_1 5e-324)
(* 2.0 (pow x -3.0))
(/ (- t_2 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 = ((1.0 / (1.0 + x)) + (-2.0 / x)) + (1.0 / (x + -1.0));
double t_2 = (1.0 + x) * ((-2.0 - x) - (x * -2.0));
double tmp;
if (t_1 <= -1e-19) {
tmp = ((x * x) - (x + t_2)) / ((1.0 + x) * t_0);
} else if (t_1 <= 5e-324) {
tmp = 2.0 * pow(x, -3.0);
} else {
tmp = (t_2 - 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(1.0 / Float64(1.0 + x)) + Float64(-2.0 / x)) + Float64(1.0 / Float64(x + -1.0))) t_2 = Float64(Float64(1.0 + x) * Float64(Float64(-2.0 - x) - Float64(x * -2.0))) tmp = 0.0 if (t_1 <= -1e-19) tmp = Float64(Float64(Float64(x * x) - Float64(x + t_2)) / Float64(Float64(1.0 + x) * t_0)); elseif (t_1 <= 5e-324) tmp = Float64(2.0 * (x ^ -3.0)); else tmp = Float64(Float64(t_2 - 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[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(1.0 + x), $MachinePrecision] * N[(N[(-2.0 - x), $MachinePrecision] - N[(x * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -1e-19], N[(N[(N[(x * x), $MachinePrecision] - N[(x + t$95$2), $MachinePrecision]), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 5e-324], N[(2.0 * N[Power[x, -3.0], $MachinePrecision]), $MachinePrecision], N[(N[(t$95$2 - 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{1}{1 + x} + \frac{-2}{x}\right) + \frac{1}{x + -1}\\
t_2 := \left(1 + x\right) \cdot \left(\left(-2 - x\right) - x \cdot -2\right)\\
\mathbf{if}\;t_1 \leq -1 \cdot 10^{-19}:\\
\;\;\;\;\frac{x \cdot x - \left(x + t_2\right)}{\left(1 + x\right) \cdot t_0}\\
\mathbf{elif}\;t_1 \leq 5 \cdot 10^{-324}:\\
\;\;\;\;2 \cdot {x}^{-3}\\
\mathbf{else}:\\
\;\;\;\;\frac{t_2 - t_0}{t_0 \cdot \left(-1 - x\right)}\\
\end{array}
| Original | 10.0 |
|---|---|
| Target | 0.3 |
| Herbie | 0.2 |
if (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < -9.9999999999999998e-20Initial program 0.5
Simplified0.5
[Start]0.5 | \[ \left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\] |
|---|---|
associate-+l- [=>]0.5 | \[ \color{blue}{\frac{1}{x + 1} - \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]0.5 | \[ \color{blue}{\frac{1}{x + 1} + \left(-\left(\frac{2}{x} - \frac{1}{x - 1}\right)\right)}
\] |
neg-mul-1 [=>]0.5 | \[ \frac{1}{x + 1} + \color{blue}{-1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
metadata-eval [<=]0.5 | \[ \frac{1}{x + 1} + \color{blue}{\left(-1\right)} \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
cancel-sign-sub-inv [<=]0.5 | \[ \color{blue}{\frac{1}{x + 1} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
+-commutative [=>]0.5 | \[ \frac{1}{\color{blue}{1 + x}} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
*-lft-identity [=>]0.5 | \[ \frac{1}{1 + x} - \color{blue}{\left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]0.5 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{\color{blue}{x + \left(-1\right)}}\right)
\] |
metadata-eval [=>]0.5 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{x + \color{blue}{-1}}\right)
\] |
Applied egg-rr0.1
Simplified0.1
[Start]0.1 | \[ \frac{x \cdot x - \left(x + \left(1 + x\right) \cdot \left(-2 + \left(2 \cdot x - x\right)\right)\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
|---|---|
+-commutative [=>]0.1 | \[ \frac{x \cdot x - \left(x + \color{blue}{\left(x + 1\right)} \cdot \left(-2 + \left(2 \cdot x - x\right)\right)\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
+-commutative [=>]0.1 | \[ \frac{x \cdot x - \left(x + \left(x + 1\right) \cdot \color{blue}{\left(\left(2 \cdot x - x\right) + -2\right)}\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
associate-+l- [=>]0.1 | \[ \frac{x \cdot x - \left(x + \left(x + 1\right) \cdot \color{blue}{\left(2 \cdot x - \left(x - -2\right)\right)}\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
*-commutative [=>]0.1 | \[ \frac{x \cdot x - \left(x + \left(x + 1\right) \cdot \left(\color{blue}{x \cdot 2} - \left(x - -2\right)\right)\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
+-commutative [=>]0.1 | \[ \frac{x \cdot x - \left(x + \left(x + 1\right) \cdot \left(x \cdot 2 - \left(x - -2\right)\right)\right)}{\color{blue}{\left(x + 1\right)} \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
if -9.9999999999999998e-20 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 4.94066e-324Initial program 19.5
Simplified19.5
[Start]19.5 | \[ \left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\] |
|---|---|
associate-+l- [=>]19.5 | \[ \color{blue}{\frac{1}{x + 1} - \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]19.5 | \[ \color{blue}{\frac{1}{x + 1} + \left(-\left(\frac{2}{x} - \frac{1}{x - 1}\right)\right)}
\] |
neg-mul-1 [=>]19.5 | \[ \frac{1}{x + 1} + \color{blue}{-1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
metadata-eval [<=]19.5 | \[ \frac{1}{x + 1} + \color{blue}{\left(-1\right)} \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
cancel-sign-sub-inv [<=]19.5 | \[ \color{blue}{\frac{1}{x + 1} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
+-commutative [=>]19.5 | \[ \frac{1}{\color{blue}{1 + x}} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
*-lft-identity [=>]19.5 | \[ \frac{1}{1 + x} - \color{blue}{\left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]19.5 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{\color{blue}{x + \left(-1\right)}}\right)
\] |
metadata-eval [=>]19.5 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{x + \color{blue}{-1}}\right)
\] |
Taylor expanded in x around inf 0.6
Applied egg-rr0.0
if 4.94066e-324 < (+.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.8
Simplified0.8
[Start]0.8 | \[ \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.8 | \[ \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.8 | \[ \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.8 | \[ \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.8 | \[ \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.8 | \[ \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.8 | \[ \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.2
| Alternative 1 | |
|---|---|
| Error | 0.4 |
| Cost | 9092 |
| Alternative 2 | |
|---|---|
| Error | 0.8 |
| Cost | 8712 |
| Alternative 3 | |
|---|---|
| Error | 10.0 |
| Cost | 960 |
| Alternative 4 | |
|---|---|
| Error | 10.9 |
| Cost | 448 |
| Alternative 5 | |
|---|---|
| Error | 10.9 |
| Cost | 448 |
| Alternative 6 | |
|---|---|
| Error | 30.9 |
| Cost | 192 |
herbie shell --seed 2022356
(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))))