
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
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
public static double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
def code(x): return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (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 tmp = code(x) tmp = ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (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]
\begin{array}{l}
\\
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
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
public static double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
def code(x): return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (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 tmp = code(x) tmp = ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (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]
\begin{array}{l}
\\
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\end{array}
(FPCore (x) :precision binary64 (* (* (+ (pow x -4.0) 1.0) (fma 2.0 (pow x -2.0) 2.0)) (pow x -3.0)))
double code(double x) {
return ((pow(x, -4.0) + 1.0) * fma(2.0, pow(x, -2.0), 2.0)) * pow(x, -3.0);
}
function code(x) return Float64(Float64(Float64((x ^ -4.0) + 1.0) * fma(2.0, (x ^ -2.0), 2.0)) * (x ^ -3.0)) end
code[x_] := N[(N[(N[(N[Power[x, -4.0], $MachinePrecision] + 1.0), $MachinePrecision] * N[(2.0 * N[Power[x, -2.0], $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision] * N[Power[x, -3.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left({x}^{-4} + 1\right) \cdot \mathsf{fma}\left(2, {x}^{-2}, 2\right)\right) \cdot {x}^{-3}
\end{array}
Initial program 72.1%
+-commutative72.1%
associate-+r-72.1%
sub-neg72.1%
remove-double-neg72.1%
neg-sub072.1%
associate-+l-72.1%
neg-sub072.1%
distribute-neg-frac272.1%
distribute-frac-neg272.1%
associate-+r+72.1%
+-commutative72.1%
remove-double-neg72.1%
distribute-neg-frac272.1%
sub0-neg72.1%
associate-+l-72.1%
neg-sub072.1%
Simplified72.1%
Taylor expanded in x around -inf 98.7%
mul-1-neg98.7%
distribute-neg-frac98.7%
Simplified98.7%
div-inv98.7%
div-inv98.7%
fma-define98.7%
+-commutative98.7%
div-inv98.7%
fma-define98.7%
pow-flip98.7%
metadata-eval98.7%
pow-flip98.7%
metadata-eval98.7%
+-commutative98.7%
div-inv98.7%
fma-define98.7%
pow-flip98.7%
metadata-eval98.7%
pow-flip99.5%
metadata-eval99.5%
Applied egg-rr99.5%
fma-undefine99.5%
*-commutative99.5%
distribute-lft1-in99.5%
Simplified99.5%
(FPCore (x) :precision binary64 (* (fma 2.0 (pow x -2.0) 2.0) (pow x -3.0)))
double code(double x) {
return fma(2.0, pow(x, -2.0), 2.0) * pow(x, -3.0);
}
function code(x) return Float64(fma(2.0, (x ^ -2.0), 2.0) * (x ^ -3.0)) end
code[x_] := N[(N[(2.0 * N[Power[x, -2.0], $MachinePrecision] + 2.0), $MachinePrecision] * N[Power[x, -3.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(2, {x}^{-2}, 2\right) \cdot {x}^{-3}
\end{array}
Initial program 72.1%
+-commutative72.1%
associate-+r-72.1%
sub-neg72.1%
remove-double-neg72.1%
neg-sub072.1%
associate-+l-72.1%
neg-sub072.1%
distribute-neg-frac272.1%
distribute-frac-neg272.1%
associate-+r+72.1%
+-commutative72.1%
remove-double-neg72.1%
distribute-neg-frac272.1%
sub0-neg72.1%
associate-+l-72.1%
neg-sub072.1%
Simplified72.1%
Taylor expanded in x around inf 98.5%
associate-*r/98.5%
metadata-eval98.5%
Simplified98.5%
div-inv98.5%
*-un-lft-identity98.5%
pow-flip99.2%
metadata-eval99.2%
+-commutative99.2%
*-un-lft-identity99.2%
div-inv99.2%
fma-define99.2%
pow-flip99.2%
metadata-eval99.2%
*-un-lft-identity99.2%
associate-*l*99.2%
Applied egg-rr99.2%
*-lft-identity99.2%
Simplified99.2%
(FPCore (x) :precision binary64 (* (/ 1.0 x) (/ (+ 2.0 (/ 2.0 (pow x 2.0))) (pow x 2.0))))
double code(double x) {
return (1.0 / x) * ((2.0 + (2.0 / pow(x, 2.0))) / pow(x, 2.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) * ((2.0d0 + (2.0d0 / (x ** 2.0d0))) / (x ** 2.0d0))
end function
public static double code(double x) {
return (1.0 / x) * ((2.0 + (2.0 / Math.pow(x, 2.0))) / Math.pow(x, 2.0));
}
def code(x): return (1.0 / x) * ((2.0 + (2.0 / math.pow(x, 2.0))) / math.pow(x, 2.0))
function code(x) return Float64(Float64(1.0 / x) * Float64(Float64(2.0 + Float64(2.0 / (x ^ 2.0))) / (x ^ 2.0))) end
function tmp = code(x) tmp = (1.0 / x) * ((2.0 + (2.0 / (x ^ 2.0))) / (x ^ 2.0)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] * N[(N[(2.0 + N[(2.0 / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x} \cdot \frac{2 + \frac{2}{{x}^{2}}}{{x}^{2}}
\end{array}
Initial program 72.1%
+-commutative72.1%
associate-+r-72.1%
sub-neg72.1%
remove-double-neg72.1%
neg-sub072.1%
associate-+l-72.1%
neg-sub072.1%
distribute-neg-frac272.1%
distribute-frac-neg272.1%
associate-+r+72.1%
+-commutative72.1%
remove-double-neg72.1%
distribute-neg-frac272.1%
sub0-neg72.1%
associate-+l-72.1%
neg-sub072.1%
Simplified72.1%
Taylor expanded in x around -inf 98.7%
mul-1-neg98.7%
distribute-neg-frac98.7%
Simplified98.7%
+-commutative98.7%
*-un-lft-identity98.7%
fma-define98.7%
+-commutative98.7%
div-inv98.7%
fma-define98.7%
pow-flip98.7%
metadata-eval98.7%
div-inv98.7%
+-commutative98.7%
div-inv98.7%
fma-define98.7%
pow-flip98.7%
metadata-eval98.7%
pow-flip98.7%
metadata-eval98.7%
Applied egg-rr98.7%
fma-undefine98.7%
*-commutative98.7%
distribute-rgt-out98.7%
Simplified98.7%
*-un-lft-identity98.7%
cube-mult98.7%
unpow298.7%
times-frac99.3%
+-commutative99.3%
*-commutative99.3%
Applied egg-rr99.3%
Taylor expanded in x around inf 99.1%
associate-*r/99.1%
metadata-eval99.1%
Simplified99.1%
(FPCore (x) :precision binary64 (* (/ 1.0 x) (/ 2.0 (pow x 2.0))))
double code(double x) {
return (1.0 / x) * (2.0 / pow(x, 2.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) * (2.0d0 / (x ** 2.0d0))
end function
public static double code(double x) {
return (1.0 / x) * (2.0 / Math.pow(x, 2.0));
}
def code(x): return (1.0 / x) * (2.0 / math.pow(x, 2.0))
function code(x) return Float64(Float64(1.0 / x) * Float64(2.0 / (x ^ 2.0))) end
function tmp = code(x) tmp = (1.0 / x) * (2.0 / (x ^ 2.0)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] * N[(2.0 / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x} \cdot \frac{2}{{x}^{2}}
\end{array}
Initial program 72.1%
+-commutative72.1%
associate-+r-72.1%
sub-neg72.1%
remove-double-neg72.1%
neg-sub072.1%
associate-+l-72.1%
neg-sub072.1%
distribute-neg-frac272.1%
distribute-frac-neg272.1%
associate-+r+72.1%
+-commutative72.1%
remove-double-neg72.1%
distribute-neg-frac272.1%
sub0-neg72.1%
associate-+l-72.1%
neg-sub072.1%
Simplified72.1%
Taylor expanded in x around -inf 98.7%
mul-1-neg98.7%
distribute-neg-frac98.7%
Simplified98.7%
+-commutative98.7%
*-un-lft-identity98.7%
fma-define98.7%
+-commutative98.7%
div-inv98.7%
fma-define98.7%
pow-flip98.7%
metadata-eval98.7%
div-inv98.7%
+-commutative98.7%
div-inv98.7%
fma-define98.7%
pow-flip98.7%
metadata-eval98.7%
pow-flip98.7%
metadata-eval98.7%
Applied egg-rr98.7%
fma-undefine98.7%
*-commutative98.7%
distribute-rgt-out98.7%
Simplified98.7%
*-un-lft-identity98.7%
cube-mult98.7%
unpow298.7%
times-frac99.3%
+-commutative99.3%
*-commutative99.3%
Applied egg-rr99.3%
Taylor expanded in x around inf 98.7%
(FPCore (x) :precision binary64 (/ (+ x (- -1.0 x)) (* x (- 1.0 x))))
double code(double x) {
return (x + (-1.0 - x)) / (x * (1.0 - x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x + ((-1.0d0) - x)) / (x * (1.0d0 - x))
end function
public static double code(double x) {
return (x + (-1.0 - x)) / (x * (1.0 - x));
}
def code(x): return (x + (-1.0 - x)) / (x * (1.0 - x))
function code(x) return Float64(Float64(x + Float64(-1.0 - x)) / Float64(x * Float64(1.0 - x))) end
function tmp = code(x) tmp = (x + (-1.0 - x)) / (x * (1.0 - x)); end
code[x_] := N[(N[(x + N[(-1.0 - x), $MachinePrecision]), $MachinePrecision] / N[(x * N[(1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + \left(-1 - x\right)}{x \cdot \left(1 - x\right)}
\end{array}
Initial program 72.1%
+-commutative72.1%
associate-+r-72.1%
sub-neg72.1%
remove-double-neg72.1%
neg-sub072.1%
associate-+l-72.1%
neg-sub072.1%
distribute-neg-frac272.1%
distribute-frac-neg272.1%
associate-+r+72.1%
+-commutative72.1%
remove-double-neg72.1%
distribute-neg-frac272.1%
sub0-neg72.1%
associate-+l-72.1%
neg-sub072.1%
Simplified72.1%
Taylor expanded in x around inf 70.6%
frac-add70.6%
*-un-lft-identity70.6%
Applied egg-rr70.6%
remove-double-neg70.6%
distribute-frac-neg270.6%
distribute-frac-neg70.6%
+-commutative70.6%
*-commutative70.6%
neg-mul-170.6%
distribute-neg-in70.6%
remove-double-neg70.6%
associate-+l+70.6%
sub-neg70.6%
*-commutative70.6%
distribute-rgt-neg-out70.6%
distribute-neg-in70.6%
metadata-eval70.6%
+-commutative70.6%
unsub-neg70.6%
Simplified70.6%
(FPCore (x) :precision binary64 (+ (/ -1.0 (- 1.0 x)) (/ -1.0 x)))
double code(double x) {
return (-1.0 / (1.0 - x)) + (-1.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((-1.0d0) / (1.0d0 - x)) + ((-1.0d0) / x)
end function
public static double code(double x) {
return (-1.0 / (1.0 - x)) + (-1.0 / x);
}
def code(x): return (-1.0 / (1.0 - x)) + (-1.0 / x)
function code(x) return Float64(Float64(-1.0 / Float64(1.0 - x)) + Float64(-1.0 / x)) end
function tmp = code(x) tmp = (-1.0 / (1.0 - x)) + (-1.0 / x); end
code[x_] := N[(N[(-1.0 / N[(1.0 - x), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{1 - x} + \frac{-1}{x}
\end{array}
Initial program 72.1%
+-commutative72.1%
associate-+r-72.1%
sub-neg72.1%
remove-double-neg72.1%
neg-sub072.1%
associate-+l-72.1%
neg-sub072.1%
distribute-neg-frac272.1%
distribute-frac-neg272.1%
associate-+r+72.1%
+-commutative72.1%
remove-double-neg72.1%
distribute-neg-frac272.1%
sub0-neg72.1%
associate-+l-72.1%
neg-sub072.1%
Simplified72.1%
Taylor expanded in x around inf 70.6%
Final simplification70.6%
(FPCore (x) :precision binary64 (/ (/ (/ 2.0 x) x) x))
double code(double x) {
return ((2.0 / x) / x) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((2.0d0 / x) / x) / x
end function
public static double code(double x) {
return ((2.0 / x) / x) / x;
}
def code(x): return ((2.0 / x) / x) / x
function code(x) return Float64(Float64(Float64(2.0 / x) / x) / x) end
function tmp = code(x) tmp = ((2.0 / x) / x) / x; end
code[x_] := N[(N[(N[(2.0 / x), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\frac{2}{x}}{x}}{x}
\end{array}
Initial program 72.1%
+-commutative72.1%
associate-+r-72.1%
sub-neg72.1%
remove-double-neg72.1%
neg-sub072.1%
associate-+l-72.1%
neg-sub072.1%
distribute-neg-frac272.1%
distribute-frac-neg272.1%
associate-+r+72.1%
+-commutative72.1%
remove-double-neg72.1%
distribute-neg-frac272.1%
sub0-neg72.1%
associate-+l-72.1%
neg-sub072.1%
Simplified72.1%
inv-pow72.1%
add-sqr-sqrt24.4%
unpow-prod-down16.8%
fma-define4.0%
sub-neg4.0%
distribute-neg-frac4.0%
metadata-eval4.0%
Applied egg-rr4.0%
unpow-14.0%
unpow-14.0%
Simplified4.0%
Taylor expanded in x around -inf 0.0%
mul-1-neg0.0%
distribute-neg-frac20.0%
Simplified98.6%
Final simplification98.6%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 72.1%
+-commutative72.1%
associate-+r-72.1%
sub-neg72.1%
remove-double-neg72.1%
neg-sub072.1%
associate-+l-72.1%
neg-sub072.1%
distribute-neg-frac272.1%
distribute-frac-neg272.1%
associate-+r+72.1%
+-commutative72.1%
remove-double-neg72.1%
distribute-neg-frac272.1%
sub0-neg72.1%
associate-+l-72.1%
neg-sub072.1%
Simplified72.1%
inv-pow72.1%
add-sqr-sqrt24.4%
unpow-prod-down16.8%
fma-define4.0%
sub-neg4.0%
distribute-neg-frac4.0%
metadata-eval4.0%
Applied egg-rr4.0%
unpow-14.0%
unpow-14.0%
Simplified4.0%
Taylor expanded in x around -inf 0.0%
associate-*r/0.0%
distribute-rgt-in0.0%
metadata-eval0.0%
unpow20.0%
rem-square-sqrt70.5%
metadata-eval70.5%
metadata-eval70.5%
metadata-eval70.5%
div070.5%
Simplified70.5%
(FPCore (x) :precision binary64 (/ 2.0 (* x (- (* x x) 1.0))))
double code(double x) {
return 2.0 / (x * ((x * x) - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 / (x * ((x * x) - 1.0d0))
end function
public static double code(double x) {
return 2.0 / (x * ((x * x) - 1.0));
}
def code(x): return 2.0 / (x * ((x * x) - 1.0))
function code(x) return Float64(2.0 / Float64(x * Float64(Float64(x * x) - 1.0))) end
function tmp = code(x) tmp = 2.0 / (x * ((x * x) - 1.0)); end
code[x_] := N[(2.0 / N[(x * N[(N[(x * x), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{x \cdot \left(x \cdot x - 1\right)}
\end{array}
herbie shell --seed 2024106
(FPCore (x)
:name "3frac (problem 3.3.3)"
:precision binary64
:pre (> (fabs x) 1.0)
:alt
(/ 2.0 (* x (- (* x x) 1.0)))
(+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))