| Alternative 1 | |
|---|---|
| Error | 1.0 |
| Cost | 841 |
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
(FPCore (x) :precision binary64 (/ -2.0 (* x (* (+ x -1.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) {
return -2.0 / (x * ((x + -1.0) * (-1.0 - x)));
}
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
code = (-2.0d0) / (x * ((x + (-1.0d0)) * ((-1.0d0) - x)))
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) {
return -2.0 / (x * ((x + -1.0) * (-1.0 - x)));
}
def code(x): return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0))
def code(x): return -2.0 / (x * ((x + -1.0) * (-1.0 - 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(-2.0 / Float64(x * Float64(Float64(x + -1.0) * Float64(-1.0 - x)))) end
function tmp = code(x) tmp = ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0)); end
function tmp = code(x) tmp = -2.0 / (x * ((x + -1.0) * (-1.0 - 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[(-2.0 / N[(x * N[(N[(x + -1.0), $MachinePrecision] * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\frac{-2}{x \cdot \left(\left(x + -1\right) \cdot \left(-1 - x\right)\right)}
Results
| Original | 9.7 |
|---|---|
| Target | 0.3 |
| Herbie | 0.3 |
Initial program 9.7
Simplified9.7
[Start]9.7 | \[ \left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\] |
|---|---|
associate-+l- [=>]9.7 | \[ \color{blue}{\frac{1}{x + 1} - \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]9.7 | \[ \color{blue}{\frac{1}{x + 1} + \left(-\left(\frac{2}{x} - \frac{1}{x - 1}\right)\right)}
\] |
neg-mul-1 [=>]9.7 | \[ \frac{1}{x + 1} + \color{blue}{-1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
metadata-eval [<=]9.7 | \[ \frac{1}{x + 1} + \color{blue}{\left(-1\right)} \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
cancel-sign-sub-inv [<=]9.7 | \[ \color{blue}{\frac{1}{x + 1} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
+-commutative [=>]9.7 | \[ \frac{1}{\color{blue}{1 + x}} - 1 \cdot \left(\frac{2}{x} - \frac{1}{x - 1}\right)
\] |
*-lft-identity [=>]9.7 | \[ \frac{1}{1 + x} - \color{blue}{\left(\frac{2}{x} - \frac{1}{x - 1}\right)}
\] |
sub-neg [=>]9.7 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{\color{blue}{x + \left(-1\right)}}\right)
\] |
metadata-eval [=>]9.7 | \[ \frac{1}{1 + x} - \left(\frac{2}{x} - \frac{1}{x + \color{blue}{-1}}\right)
\] |
Applied egg-rr25.5
Simplified25.5
[Start]25.5 | \[ \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)}
\] |
|---|---|
sub-neg [=>]25.5 | \[ \frac{\color{blue}{-1 \cdot \mathsf{fma}\left(x, x, -x\right) + \left(-\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 [=>]25.5 | \[ \frac{\color{blue}{\left(-\left(-1 - x\right) \cdot \left(-2 + \left(2 \cdot x - x\right)\right)\right) + -1 \cdot \mathsf{fma}\left(x, x, -x\right)}}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
distribute-rgt-neg-in [=>]25.5 | \[ \frac{\color{blue}{\left(-1 - x\right) \cdot \left(-\left(-2 + \left(2 \cdot x - x\right)\right)\right)} + -1 \cdot \mathsf{fma}\left(x, x, -x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
neg-sub0 [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \color{blue}{\left(0 - \left(-2 + \left(2 \cdot x - x\right)\right)\right)} + -1 \cdot \mathsf{fma}\left(x, x, -x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
associate--r+ [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \color{blue}{\left(\left(0 - -2\right) - \left(2 \cdot x - x\right)\right)} + -1 \cdot \mathsf{fma}\left(x, x, -x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
metadata-eval [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(\color{blue}{2} - \left(2 \cdot x - x\right)\right) + -1 \cdot \mathsf{fma}\left(x, x, -x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
sub-neg [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - \color{blue}{\left(2 \cdot x + \left(-x\right)\right)}\right) + -1 \cdot \mathsf{fma}\left(x, x, -x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
neg-mul-1 [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - \left(2 \cdot x + \color{blue}{-1 \cdot x}\right)\right) + -1 \cdot \mathsf{fma}\left(x, x, -x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
distribute-rgt-out [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - \color{blue}{x \cdot \left(2 + -1\right)}\right) + -1 \cdot \mathsf{fma}\left(x, x, -x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
metadata-eval [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x \cdot \color{blue}{1}\right) + -1 \cdot \mathsf{fma}\left(x, x, -x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
*-rgt-identity [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - \color{blue}{x}\right) + -1 \cdot \mathsf{fma}\left(x, x, -x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
fma-udef [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + -1 \cdot \color{blue}{\left(x \cdot x + \left(-x\right)\right)}}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
distribute-lft-in [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + \color{blue}{\left(-1 \cdot \left(x \cdot x\right) + -1 \cdot \left(-x\right)\right)}}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
associate-*l* [<=]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + \left(\color{blue}{\left(-1 \cdot x\right) \cdot x} + -1 \cdot \left(-x\right)\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
neg-mul-1 [<=]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + \left(\color{blue}{\left(-x\right)} \cdot x + -1 \cdot \left(-x\right)\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
*-commutative [<=]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + \left(\left(-x\right) \cdot x + \color{blue}{\left(-x\right) \cdot -1}\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
+-commutative [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + \color{blue}{\left(\left(-x\right) \cdot -1 + \left(-x\right) \cdot x\right)}}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
*-commutative [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + \left(\color{blue}{-1 \cdot \left(-x\right)} + \left(-x\right) \cdot x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
neg-mul-1 [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + \left(-1 \cdot \color{blue}{\left(-1 \cdot x\right)} + \left(-x\right) \cdot x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
associate-*r* [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + \left(\color{blue}{\left(-1 \cdot -1\right) \cdot x} + \left(-x\right) \cdot x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
metadata-eval [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + \left(\color{blue}{1} \cdot x + \left(-x\right) \cdot x\right)}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
distribute-rgt-in [<=]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + \color{blue}{x \cdot \left(1 + \left(-x\right)\right)}}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
sub-neg [<=]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + x \cdot \color{blue}{\left(1 - x\right)}}{\left(-1 - x\right) \cdot \mathsf{fma}\left(x, x, -x\right)}
\] |
*-commutative [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + x \cdot \left(1 - x\right)}{\color{blue}{\mathsf{fma}\left(x, x, -x\right) \cdot \left(-1 - x\right)}}
\] |
fma-udef [=>]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + x \cdot \left(1 - x\right)}{\color{blue}{\left(x \cdot x + \left(-x\right)\right)} \cdot \left(-1 - x\right)}
\] |
sub-neg [<=]25.5 | \[ \frac{\left(-1 - x\right) \cdot \left(2 - x\right) + x \cdot \left(1 - x\right)}{\color{blue}{\left(x \cdot x - x\right)} \cdot \left(-1 - x\right)}
\] |
Taylor expanded in x around 0 0.3
Applied egg-rr27.1
Simplified0.1
[Start]27.1 | \[ e^{\mathsf{log1p}\left(\frac{-2}{\mathsf{fma}\left(x, x, -x\right) \cdot \left(-1 - x\right)}\right)} - 1
\] |
|---|---|
expm1-def [=>]17.5 | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{-2}{\mathsf{fma}\left(x, x, -x\right) \cdot \left(-1 - x\right)}\right)\right)}
\] |
expm1-log1p [=>]0.3 | \[ \color{blue}{\frac{-2}{\mathsf{fma}\left(x, x, -x\right) \cdot \left(-1 - x\right)}}
\] |
associate-/r* [=>]0.1 | \[ \color{blue}{\frac{\frac{-2}{\mathsf{fma}\left(x, x, -x\right)}}{-1 - x}}
\] |
fma-neg [<=]0.1 | \[ \frac{\frac{-2}{\color{blue}{x \cdot x - x}}}{-1 - x}
\] |
Applied egg-rr27.1
Simplified0.3
[Start]27.1 | \[ e^{\mathsf{log1p}\left(\frac{-2}{\left(x \cdot \left(x + -1\right)\right) \cdot \left(-1 - x\right)}\right)} - 1
\] |
|---|---|
expm1-def [=>]17.5 | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{-2}{\left(x \cdot \left(x + -1\right)\right) \cdot \left(-1 - x\right)}\right)\right)}
\] |
expm1-log1p [=>]0.3 | \[ \color{blue}{\frac{-2}{\left(x \cdot \left(x + -1\right)\right) \cdot \left(-1 - x\right)}}
\] |
associate-*l* [=>]0.3 | \[ \frac{-2}{\color{blue}{x \cdot \left(\left(x + -1\right) \cdot \left(-1 - x\right)\right)}}
\] |
Final simplification0.3
| Alternative 1 | |
|---|---|
| Error | 1.0 |
| Cost | 841 |
| Alternative 2 | |
|---|---|
| Error | 1.0 |
| Cost | 840 |
| Alternative 3 | |
|---|---|
| Error | 10.4 |
| Cost | 708 |
| Alternative 4 | |
|---|---|
| Error | 15.2 |
| Cost | 585 |
| Alternative 5 | |
|---|---|
| Error | 10.4 |
| Cost | 457 |
| Alternative 6 | |
|---|---|
| Error | 30.7 |
| Cost | 192 |
| Alternative 7 | |
|---|---|
| Error | 61.9 |
| Cost | 64 |
herbie shell --seed 2023011
(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))))