
(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 6 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 (let* ((t_0 (fma 2.0 (pow x -2.0) 2.0))) (* (pow x -3.0) (+ t_0 (/ t_0 (pow x 4.0))))))
double code(double x) {
double t_0 = fma(2.0, pow(x, -2.0), 2.0);
return pow(x, -3.0) * (t_0 + (t_0 / pow(x, 4.0)));
}
function code(x) t_0 = fma(2.0, (x ^ -2.0), 2.0) return Float64((x ^ -3.0) * Float64(t_0 + Float64(t_0 / (x ^ 4.0)))) end
code[x_] := Block[{t$95$0 = N[(2.0 * N[Power[x, -2.0], $MachinePrecision] + 2.0), $MachinePrecision]}, N[(N[Power[x, -3.0], $MachinePrecision] * N[(t$95$0 + N[(t$95$0 / N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(2, {x}^{-2}, 2\right)\\
{x}^{-3} \cdot \left(t\_0 + \frac{t\_0}{{x}^{4}}\right)
\end{array}
\end{array}
Initial program 71.9%
+-commutative71.9%
associate-+r-71.9%
sub-neg71.9%
remove-double-neg71.9%
neg-sub071.9%
associate-+l-71.9%
neg-sub071.9%
distribute-neg-frac271.9%
distribute-frac-neg271.9%
associate-+r+71.9%
+-commutative71.9%
remove-double-neg71.9%
distribute-neg-frac271.9%
sub0-neg71.9%
associate-+l-71.9%
neg-sub071.9%
Simplified71.9%
Taylor expanded in x around -inf 99.4%
mul-1-neg99.4%
mul-1-neg99.4%
associate-*r/99.4%
metadata-eval99.4%
associate-*r/99.4%
metadata-eval99.4%
Simplified99.4%
neg-sub099.4%
div-inv99.4%
distribute-neg-frac299.4%
+-commutative99.4%
div-inv99.4%
fma-define99.4%
pow-flip99.4%
metadata-eval99.4%
+-commutative99.4%
div-inv99.4%
fma-define99.4%
pow-flip99.4%
metadata-eval99.4%
pow-flip99.7%
metadata-eval99.7%
Applied egg-rr99.7%
sub0-neg99.7%
distribute-rgt-neg-in99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (/ -2.0 (* (+ x -1.0) (* x (- -1.0 x)))))
double code(double x) {
return -2.0 / ((x + -1.0) * (x * (-1.0 - x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-2.0d0) / ((x + (-1.0d0)) * (x * ((-1.0d0) - x)))
end function
public static double code(double x) {
return -2.0 / ((x + -1.0) * (x * (-1.0 - x)));
}
def code(x): return -2.0 / ((x + -1.0) * (x * (-1.0 - x)))
function code(x) return Float64(-2.0 / Float64(Float64(x + -1.0) * Float64(x * Float64(-1.0 - x)))) end
function tmp = code(x) tmp = -2.0 / ((x + -1.0) * (x * (-1.0 - x))); end
code[x_] := N[(-2.0 / N[(N[(x + -1.0), $MachinePrecision] * N[(x * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2}{\left(x + -1\right) \cdot \left(x \cdot \left(-1 - x\right)\right)}
\end{array}
Initial program 71.9%
+-commutative71.9%
associate-+r-71.9%
sub-neg71.9%
remove-double-neg71.9%
neg-sub071.9%
associate-+l-71.9%
neg-sub071.9%
distribute-neg-frac271.9%
distribute-frac-neg271.9%
associate-+r+71.9%
+-commutative71.9%
remove-double-neg71.9%
distribute-neg-frac271.9%
sub0-neg71.9%
associate-+l-71.9%
neg-sub071.9%
Simplified71.9%
frac-sub20.5%
frac-add22.4%
*-un-lft-identity22.4%
fma-define20.9%
*-rgt-identity20.9%
fma-neg20.9%
Applied egg-rr20.9%
fma-undefine22.4%
+-commutative22.4%
fma-define20.9%
fma-neg20.9%
Simplified20.9%
fma-undefine22.4%
Applied egg-rr22.4%
Taylor expanded in x around 0 99.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(2.0 / x) / Float64(x * x)) end
function tmp = code(x) tmp = (2.0 / x) / (x * x); end
code[x_] := N[(N[(2.0 / x), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{2}{x}}{x \cdot x}
\end{array}
Initial program 71.9%
+-commutative71.9%
associate-+r-71.9%
sub-neg71.9%
remove-double-neg71.9%
neg-sub071.9%
associate-+l-71.9%
neg-sub071.9%
distribute-neg-frac271.9%
distribute-frac-neg271.9%
associate-+r+71.9%
+-commutative71.9%
remove-double-neg71.9%
distribute-neg-frac271.9%
sub0-neg71.9%
associate-+l-71.9%
neg-sub071.9%
Simplified71.9%
Taylor expanded in x around inf 70.6%
+-commutative70.6%
associate--r+70.6%
unpow270.6%
associate-/r*70.6%
div-sub70.6%
sub-neg70.6%
metadata-eval70.6%
+-commutative70.6%
Simplified70.6%
frac-add70.6%
*-un-lft-identity70.6%
sub-neg70.6%
metadata-eval70.6%
Applied egg-rr70.6%
Taylor expanded in x around inf 97.0%
Taylor expanded in x around inf 98.1%
(FPCore (x) :precision binary64 (+ (/ 1.0 x) (/ -1.0 x)))
double code(double x) {
return (1.0 / x) + (-1.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) + ((-1.0d0) / x)
end function
public static double code(double x) {
return (1.0 / x) + (-1.0 / x);
}
def code(x): return (1.0 / x) + (-1.0 / x)
function code(x) return Float64(Float64(1.0 / x) + Float64(-1.0 / x)) end
function tmp = code(x) tmp = (1.0 / x) + (-1.0 / x); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x} + \frac{-1}{x}
\end{array}
Initial program 71.9%
+-commutative71.9%
associate-+r-71.9%
sub-neg71.9%
remove-double-neg71.9%
neg-sub071.9%
associate-+l-71.9%
neg-sub071.9%
distribute-neg-frac271.9%
distribute-frac-neg271.9%
associate-+r+71.9%
+-commutative71.9%
remove-double-neg71.9%
distribute-neg-frac271.9%
sub0-neg71.9%
associate-+l-71.9%
neg-sub071.9%
Simplified71.9%
Taylor expanded in x around inf 69.8%
Taylor expanded in x around inf 69.5%
(FPCore (x) :precision binary64 (/ -1.0 x))
double code(double x) {
return -1.0 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-1.0d0) / x
end function
public static double code(double x) {
return -1.0 / x;
}
def code(x): return -1.0 / x
function code(x) return Float64(-1.0 / x) end
function tmp = code(x) tmp = -1.0 / x; end
code[x_] := N[(-1.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{x}
\end{array}
Initial program 71.9%
+-commutative71.9%
associate-+r-71.9%
sub-neg71.9%
remove-double-neg71.9%
neg-sub071.9%
associate-+l-71.9%
neg-sub071.9%
distribute-neg-frac271.9%
distribute-frac-neg271.9%
associate-+r+71.9%
+-commutative71.9%
remove-double-neg71.9%
distribute-neg-frac271.9%
sub0-neg71.9%
associate-+l-71.9%
neg-sub071.9%
Simplified71.9%
Taylor expanded in x around inf 69.8%
Taylor expanded in x around 0 4.9%
(FPCore (x) :precision binary64 (/ -2.0 x))
double code(double x) {
return -2.0 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-2.0d0) / x
end function
public static double code(double x) {
return -2.0 / x;
}
def code(x): return -2.0 / x
function code(x) return Float64(-2.0 / x) end
function tmp = code(x) tmp = -2.0 / x; end
code[x_] := N[(-2.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2}{x}
\end{array}
Initial program 71.9%
+-commutative71.9%
associate-+r-71.9%
sub-neg71.9%
remove-double-neg71.9%
neg-sub071.9%
associate-+l-71.9%
neg-sub071.9%
distribute-neg-frac271.9%
distribute-frac-neg271.9%
associate-+r+71.9%
+-commutative71.9%
remove-double-neg71.9%
distribute-neg-frac271.9%
sub0-neg71.9%
associate-+l-71.9%
neg-sub071.9%
Simplified71.9%
Taylor expanded in x around 0 4.9%
(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 2024116
(FPCore (x)
:name "3frac (problem 3.3.3)"
:precision binary64
:pre (> (fabs x) 1.0)
:alt
(! :herbie-platform default (/ 2 (* x (- (* x x) 1))))
(+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))