
(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 7 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 (* (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 66.3%
+-commutative66.3%
associate-+r-66.3%
sub-neg66.3%
remove-double-neg66.3%
neg-sub066.3%
associate-+l-66.3%
neg-sub066.3%
distribute-neg-frac266.3%
distribute-frac-neg266.3%
associate-+r+66.3%
+-commutative66.3%
remove-double-neg66.3%
distribute-neg-frac266.3%
sub0-neg66.3%
associate-+l-66.3%
neg-sub066.3%
Simplified66.3%
Taylor expanded in x around inf 98.9%
associate-*r/98.9%
metadata-eval98.9%
Simplified98.9%
div-inv98.9%
+-commutative98.9%
div-inv98.9%
fma-define98.9%
pow-flip98.9%
metadata-eval98.9%
pow-flip99.4%
metadata-eval99.4%
Applied egg-rr99.4%
(FPCore (x) :precision binary64 (/ 1.0 (* (* -0.5 (* x (- -1.0 x))) (+ x -1.0))))
double code(double x) {
return 1.0 / ((-0.5 * (x * (-1.0 - x))) * (x + -1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (((-0.5d0) * (x * ((-1.0d0) - x))) * (x + (-1.0d0)))
end function
public static double code(double x) {
return 1.0 / ((-0.5 * (x * (-1.0 - x))) * (x + -1.0));
}
def code(x): return 1.0 / ((-0.5 * (x * (-1.0 - x))) * (x + -1.0))
function code(x) return Float64(1.0 / Float64(Float64(-0.5 * Float64(x * Float64(-1.0 - x))) * Float64(x + -1.0))) end
function tmp = code(x) tmp = 1.0 / ((-0.5 * (x * (-1.0 - x))) * (x + -1.0)); end
code[x_] := N[(1.0 / N[(N[(-0.5 * N[(x * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\left(-0.5 \cdot \left(x \cdot \left(-1 - x\right)\right)\right) \cdot \left(x + -1\right)}
\end{array}
Initial program 66.3%
+-commutative66.3%
associate-+r-66.3%
sub-neg66.3%
remove-double-neg66.3%
neg-sub066.3%
associate-+l-66.3%
neg-sub066.3%
distribute-neg-frac266.3%
distribute-frac-neg266.3%
associate-+r+66.3%
+-commutative66.3%
remove-double-neg66.3%
distribute-neg-frac266.3%
sub0-neg66.3%
associate-+l-66.3%
neg-sub066.3%
Simplified66.3%
clear-num66.3%
frac-sub17.3%
*-un-lft-identity17.3%
div-inv17.3%
metadata-eval17.3%
div-inv17.3%
metadata-eval17.3%
Applied egg-rr17.3%
sub-neg17.3%
*-rgt-identity17.3%
sub-neg17.3%
neg-mul-117.3%
*-commutative17.3%
associate-+l+17.3%
neg-mul-117.3%
*-commutative17.3%
associate-*l*17.3%
distribute-lft-out17.3%
metadata-eval17.3%
metadata-eval17.3%
*-commutative17.3%
associate-*r*17.3%
*-commutative17.3%
Simplified17.3%
+-commutative17.3%
frac-add22.8%
+-commutative22.8%
fma-define22.8%
*-commutative22.8%
*-commutative22.8%
Applied egg-rr22.8%
Taylor expanded in x around 0 22.8%
Taylor expanded in x around 0 99.4%
(FPCore (x) :precision binary64 (/ (/ 1.0 x) (* x (+ x -1.0))))
double code(double x) {
return (1.0 / x) / (x * (x + -1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / (x * (x + (-1.0d0)))
end function
public static double code(double x) {
return (1.0 / x) / (x * (x + -1.0));
}
def code(x): return (1.0 / x) / (x * (x + -1.0))
function code(x) return Float64(Float64(1.0 / x) / Float64(x * Float64(x + -1.0))) end
function tmp = code(x) tmp = (1.0 / x) / (x * (x + -1.0)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(x * N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{x \cdot \left(x + -1\right)}
\end{array}
Initial program 66.3%
+-commutative66.3%
associate-+r-66.3%
sub-neg66.3%
remove-double-neg66.3%
neg-sub066.3%
associate-+l-66.3%
neg-sub066.3%
distribute-neg-frac266.3%
distribute-frac-neg266.3%
associate-+r+66.3%
+-commutative66.3%
remove-double-neg66.3%
distribute-neg-frac266.3%
sub0-neg66.3%
associate-+l-66.3%
neg-sub066.3%
Simplified66.3%
Taylor expanded in x around inf 65.1%
associate-*r/65.1%
neg-mul-165.1%
distribute-neg-in65.1%
metadata-eval65.1%
distribute-neg-frac65.1%
metadata-eval65.1%
Simplified65.1%
frac-add65.1%
*-un-lft-identity65.1%
Applied egg-rr65.1%
Taylor expanded in x around 0 69.5%
Final simplification69.5%
(FPCore (x) :precision binary64 (+ (/ 1.0 (+ x -1.0)) (/ -1.0 x)))
double code(double x) {
return (1.0 / (x + -1.0)) + (-1.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x + (-1.0d0))) + ((-1.0d0) / x)
end function
public static double code(double x) {
return (1.0 / (x + -1.0)) + (-1.0 / x);
}
def code(x): return (1.0 / (x + -1.0)) + (-1.0 / x)
function code(x) return Float64(Float64(1.0 / Float64(x + -1.0)) + Float64(-1.0 / x)) end
function tmp = code(x) tmp = (1.0 / (x + -1.0)) + (-1.0 / x); end
code[x_] := N[(N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + -1} + \frac{-1}{x}
\end{array}
Initial program 66.3%
+-commutative66.3%
associate-+r-66.3%
sub-neg66.3%
remove-double-neg66.3%
neg-sub066.3%
associate-+l-66.3%
neg-sub066.3%
distribute-neg-frac266.3%
distribute-frac-neg266.3%
associate-+r+66.3%
+-commutative66.3%
remove-double-neg66.3%
distribute-neg-frac266.3%
sub0-neg66.3%
associate-+l-66.3%
neg-sub066.3%
Simplified66.3%
Taylor expanded in x around inf 64.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 66.3%
+-commutative66.3%
associate-+r-66.3%
sub-neg66.3%
remove-double-neg66.3%
neg-sub066.3%
associate-+l-66.3%
neg-sub066.3%
distribute-neg-frac266.3%
distribute-frac-neg266.3%
associate-+r+66.3%
+-commutative66.3%
remove-double-neg66.3%
distribute-neg-frac266.3%
sub0-neg66.3%
associate-+l-66.3%
neg-sub066.3%
Simplified66.3%
clear-num66.3%
frac-sub17.3%
*-un-lft-identity17.3%
div-inv17.3%
metadata-eval17.3%
div-inv17.3%
metadata-eval17.3%
Applied egg-rr17.3%
sub-neg17.3%
*-rgt-identity17.3%
sub-neg17.3%
neg-mul-117.3%
*-commutative17.3%
associate-+l+17.3%
neg-mul-117.3%
*-commutative17.3%
associate-*l*17.3%
distribute-lft-out17.3%
metadata-eval17.3%
metadata-eval17.3%
*-commutative17.3%
associate-*r*17.3%
*-commutative17.3%
Simplified17.3%
Taylor expanded in x around inf 15.7%
Taylor expanded in x around 0 4.8%
div-inv4.8%
metadata-eval4.8%
add-sqr-sqrt2.4%
sqrt-prod48.2%
sqr-neg48.2%
pow248.2%
sqrt-pow16.3%
metadata-eval6.3%
pow16.3%
frac-2neg6.3%
Applied egg-rr6.3%
associate-*r/6.3%
metadata-eval6.3%
Simplified6.3%
(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 66.3%
+-commutative66.3%
associate-+r-66.3%
sub-neg66.3%
remove-double-neg66.3%
neg-sub066.3%
associate-+l-66.3%
neg-sub066.3%
distribute-neg-frac266.3%
distribute-frac-neg266.3%
associate-+r+66.3%
+-commutative66.3%
remove-double-neg66.3%
distribute-neg-frac266.3%
sub0-neg66.3%
associate-+l-66.3%
neg-sub066.3%
Simplified66.3%
clear-num66.3%
frac-sub17.3%
*-un-lft-identity17.3%
div-inv17.3%
metadata-eval17.3%
div-inv17.3%
metadata-eval17.3%
Applied egg-rr17.3%
sub-neg17.3%
*-rgt-identity17.3%
sub-neg17.3%
neg-mul-117.3%
*-commutative17.3%
associate-+l+17.3%
neg-mul-117.3%
*-commutative17.3%
associate-*l*17.3%
distribute-lft-out17.3%
metadata-eval17.3%
metadata-eval17.3%
*-commutative17.3%
associate-*r*17.3%
*-commutative17.3%
Simplified17.3%
Taylor expanded in x around inf 15.7%
Taylor expanded in x around 0 4.8%
(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 66.3%
+-commutative66.3%
associate-+r-66.3%
sub-neg66.3%
remove-double-neg66.3%
neg-sub066.3%
associate-+l-66.3%
neg-sub066.3%
distribute-neg-frac266.3%
distribute-frac-neg266.3%
associate-+r+66.3%
+-commutative66.3%
remove-double-neg66.3%
distribute-neg-frac266.3%
sub0-neg66.3%
associate-+l-66.3%
neg-sub066.3%
Simplified66.3%
Taylor expanded in x around 0 4.7%
(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 2024148
(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))))