
(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 (/ 2.0 (- (pow x 3.0) x)))
double code(double x) {
return 2.0 / (pow(x, 3.0) - x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 / ((x ** 3.0d0) - x)
end function
public static double code(double x) {
return 2.0 / (Math.pow(x, 3.0) - x);
}
def code(x): return 2.0 / (math.pow(x, 3.0) - x)
function code(x) return Float64(2.0 / Float64((x ^ 3.0) - x)) end
function tmp = code(x) tmp = 2.0 / ((x ^ 3.0) - x); end
code[x_] := N[(2.0 / N[(N[Power[x, 3.0], $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{{x}^{3} - x}
\end{array}
Initial program 82.7%
associate-+l-82.7%
sub-neg82.7%
neg-mul-182.7%
metadata-eval82.7%
cancel-sign-sub-inv82.7%
+-commutative82.7%
*-lft-identity82.7%
sub-neg82.7%
metadata-eval82.7%
Simplified82.7%
frac-sub58.1%
frac-sub59.2%
*-un-lft-identity59.2%
distribute-rgt-in59.2%
neg-mul-159.2%
sub-neg59.2%
*-rgt-identity59.2%
distribute-rgt-in59.2%
metadata-eval59.2%
metadata-eval59.2%
fma-def59.2%
metadata-eval59.2%
distribute-rgt-in59.2%
neg-mul-159.2%
sub-neg59.2%
Applied egg-rr59.2%
+-commutative59.2%
remove-double-neg59.2%
metadata-eval59.2%
distribute-neg-in59.2%
neg-mul-159.2%
*-commutative59.2%
fma-udef59.2%
distribute-lft-neg-in59.2%
distribute-lft-neg-in59.2%
fma-udef59.2%
*-commutative59.2%
neg-mul-159.2%
distribute-neg-in59.2%
remove-double-neg59.2%
metadata-eval59.2%
+-commutative59.2%
Simplified59.2%
Taylor expanded in x around 0 99.9%
Taylor expanded in x around 0 99.9%
neg-mul-199.9%
+-commutative99.9%
unsub-neg99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (or (<= x -0.86) (not (<= x 1.0))) (* 2.0 (/ (/ 1.0 (* x x)) (+ x 1.0))) (- (* x -2.0) (/ 2.0 x))))
double code(double x) {
double tmp;
if ((x <= -0.86) || !(x <= 1.0)) {
tmp = 2.0 * ((1.0 / (x * x)) / (x + 1.0));
} else {
tmp = (x * -2.0) - (2.0 / x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x <= (-0.86d0)) .or. (.not. (x <= 1.0d0))) then
tmp = 2.0d0 * ((1.0d0 / (x * x)) / (x + 1.0d0))
else
tmp = (x * (-2.0d0)) - (2.0d0 / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -0.86) || !(x <= 1.0)) {
tmp = 2.0 * ((1.0 / (x * x)) / (x + 1.0));
} else {
tmp = (x * -2.0) - (2.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if (x <= -0.86) or not (x <= 1.0): tmp = 2.0 * ((1.0 / (x * x)) / (x + 1.0)) else: tmp = (x * -2.0) - (2.0 / x) return tmp
function code(x) tmp = 0.0 if ((x <= -0.86) || !(x <= 1.0)) tmp = Float64(2.0 * Float64(Float64(1.0 / Float64(x * x)) / Float64(x + 1.0))); else tmp = Float64(Float64(x * -2.0) - Float64(2.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -0.86) || ~((x <= 1.0))) tmp = 2.0 * ((1.0 / (x * x)) / (x + 1.0)); else tmp = (x * -2.0) - (2.0 / x); end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -0.86], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], N[(2.0 * N[(N[(1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * -2.0), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.86 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;2 \cdot \frac{\frac{1}{x \cdot x}}{x + 1}\\
\mathbf{else}:\\
\;\;\;\;x \cdot -2 - \frac{2}{x}\\
\end{array}
\end{array}
if x < -0.859999999999999987 or 1 < x Initial program 67.3%
associate-+l-67.3%
sub-neg67.3%
neg-mul-167.3%
metadata-eval67.3%
cancel-sign-sub-inv67.3%
+-commutative67.3%
*-lft-identity67.3%
sub-neg67.3%
metadata-eval67.3%
Simplified67.3%
frac-sub20.6%
frac-sub22.7%
*-un-lft-identity22.7%
distribute-rgt-in22.7%
neg-mul-122.7%
sub-neg22.7%
*-rgt-identity22.7%
distribute-rgt-in22.7%
metadata-eval22.7%
metadata-eval22.7%
fma-def22.7%
metadata-eval22.7%
distribute-rgt-in22.7%
neg-mul-122.7%
sub-neg22.7%
Applied egg-rr22.7%
+-commutative22.7%
remove-double-neg22.7%
metadata-eval22.7%
distribute-neg-in22.7%
neg-mul-122.7%
*-commutative22.7%
fma-udef22.7%
distribute-lft-neg-in22.7%
distribute-lft-neg-in22.7%
fma-udef22.7%
*-commutative22.7%
neg-mul-122.7%
distribute-neg-in22.7%
remove-double-neg22.7%
metadata-eval22.7%
+-commutative22.7%
Simplified22.7%
Taylor expanded in x around 0 99.8%
clear-num99.8%
associate-/r/99.8%
*-commutative99.8%
associate-/r*99.8%
*-un-lft-identity99.8%
distribute-rgt-out--99.8%
sub-neg99.8%
metadata-eval99.8%
Applied egg-rr99.8%
Taylor expanded in x around inf 96.4%
unpow296.4%
Simplified96.4%
if -0.859999999999999987 < x < 1Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
neg-mul-1100.0%
metadata-eval100.0%
cancel-sign-sub-inv100.0%
+-commutative100.0%
*-lft-identity100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 98.5%
associate-*r/98.5%
metadata-eval98.5%
Simplified98.5%
Final simplification97.4%
(FPCore (x) :precision binary64 (* 2.0 (/ (/ 1.0 (* x (+ x -1.0))) (+ x 1.0))))
double code(double x) {
return 2.0 * ((1.0 / (x * (x + -1.0))) / (x + 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 * ((1.0d0 / (x * (x + (-1.0d0)))) / (x + 1.0d0))
end function
public static double code(double x) {
return 2.0 * ((1.0 / (x * (x + -1.0))) / (x + 1.0));
}
def code(x): return 2.0 * ((1.0 / (x * (x + -1.0))) / (x + 1.0))
function code(x) return Float64(2.0 * Float64(Float64(1.0 / Float64(x * Float64(x + -1.0))) / Float64(x + 1.0))) end
function tmp = code(x) tmp = 2.0 * ((1.0 / (x * (x + -1.0))) / (x + 1.0)); end
code[x_] := N[(2.0 * N[(N[(1.0 / N[(x * N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \frac{\frac{1}{x \cdot \left(x + -1\right)}}{x + 1}
\end{array}
Initial program 82.7%
associate-+l-82.7%
sub-neg82.7%
neg-mul-182.7%
metadata-eval82.7%
cancel-sign-sub-inv82.7%
+-commutative82.7%
*-lft-identity82.7%
sub-neg82.7%
metadata-eval82.7%
Simplified82.7%
frac-sub58.1%
frac-sub59.2%
*-un-lft-identity59.2%
distribute-rgt-in59.2%
neg-mul-159.2%
sub-neg59.2%
*-rgt-identity59.2%
distribute-rgt-in59.2%
metadata-eval59.2%
metadata-eval59.2%
fma-def59.2%
metadata-eval59.2%
distribute-rgt-in59.2%
neg-mul-159.2%
sub-neg59.2%
Applied egg-rr59.2%
+-commutative59.2%
remove-double-neg59.2%
metadata-eval59.2%
distribute-neg-in59.2%
neg-mul-159.2%
*-commutative59.2%
fma-udef59.2%
distribute-lft-neg-in59.2%
distribute-lft-neg-in59.2%
fma-udef59.2%
*-commutative59.2%
neg-mul-159.2%
distribute-neg-in59.2%
remove-double-neg59.2%
metadata-eval59.2%
+-commutative59.2%
Simplified59.2%
Taylor expanded in x around 0 99.9%
clear-num99.9%
associate-/r/99.9%
*-commutative99.9%
associate-/r*99.9%
*-un-lft-identity99.9%
distribute-rgt-out--99.9%
sub-neg99.9%
metadata-eval99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (/ 2.0 (* (+ x 1.0) (- (* x x) x))))
double code(double x) {
return 2.0 / ((x + 1.0) * ((x * x) - x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 / ((x + 1.0d0) * ((x * x) - x))
end function
public static double code(double x) {
return 2.0 / ((x + 1.0) * ((x * x) - x));
}
def code(x): return 2.0 / ((x + 1.0) * ((x * x) - x))
function code(x) return Float64(2.0 / Float64(Float64(x + 1.0) * Float64(Float64(x * x) - x))) end
function tmp = code(x) tmp = 2.0 / ((x + 1.0) * ((x * x) - x)); end
code[x_] := N[(2.0 / N[(N[(x + 1.0), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{\left(x + 1\right) \cdot \left(x \cdot x - x\right)}
\end{array}
Initial program 82.7%
associate-+l-82.7%
sub-neg82.7%
neg-mul-182.7%
metadata-eval82.7%
cancel-sign-sub-inv82.7%
+-commutative82.7%
*-lft-identity82.7%
sub-neg82.7%
metadata-eval82.7%
Simplified82.7%
frac-sub58.1%
frac-sub59.2%
*-un-lft-identity59.2%
distribute-rgt-in59.2%
neg-mul-159.2%
sub-neg59.2%
*-rgt-identity59.2%
distribute-rgt-in59.2%
metadata-eval59.2%
metadata-eval59.2%
fma-def59.2%
metadata-eval59.2%
distribute-rgt-in59.2%
neg-mul-159.2%
sub-neg59.2%
Applied egg-rr59.2%
+-commutative59.2%
remove-double-neg59.2%
metadata-eval59.2%
distribute-neg-in59.2%
neg-mul-159.2%
*-commutative59.2%
fma-udef59.2%
distribute-lft-neg-in59.2%
distribute-lft-neg-in59.2%
fma-udef59.2%
*-commutative59.2%
neg-mul-159.2%
distribute-neg-in59.2%
remove-double-neg59.2%
metadata-eval59.2%
+-commutative59.2%
Simplified59.2%
Taylor expanded in x around 0 99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 1.0))) (/ -1.0 (* x x)) (/ -2.0 x)))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = -1.0 / (x * x);
} else {
tmp = -2.0 / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x <= (-1.0d0)) .or. (.not. (x <= 1.0d0))) then
tmp = (-1.0d0) / (x * x)
else
tmp = (-2.0d0) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = -1.0 / (x * x);
} else {
tmp = -2.0 / x;
}
return tmp;
}
def code(x): tmp = 0 if (x <= -1.0) or not (x <= 1.0): tmp = -1.0 / (x * x) else: tmp = -2.0 / x return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 1.0)) tmp = Float64(-1.0 / Float64(x * x)); else tmp = Float64(-2.0 / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -1.0) || ~((x <= 1.0))) tmp = -1.0 / (x * x); else tmp = -2.0 / x; end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], N[(-1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision], N[(-2.0 / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;\frac{-1}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;\frac{-2}{x}\\
\end{array}
\end{array}
if x < -1 or 1 < x Initial program 67.3%
associate-+l-67.3%
sub-neg67.3%
neg-mul-167.3%
metadata-eval67.3%
cancel-sign-sub-inv67.3%
+-commutative67.3%
*-lft-identity67.3%
sub-neg67.3%
metadata-eval67.3%
Simplified67.3%
Taylor expanded in x around inf 64.6%
Taylor expanded in x around inf 50.8%
unpow250.8%
Simplified50.8%
if -1 < x < 1Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
neg-mul-1100.0%
metadata-eval100.0%
cancel-sign-sub-inv100.0%
+-commutative100.0%
*-lft-identity100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 97.7%
Final simplification73.0%
(FPCore (x) :precision binary64 (+ 1.0 (- -1.0 (/ 2.0 x))))
double code(double x) {
return 1.0 + (-1.0 - (2.0 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 + ((-1.0d0) - (2.0d0 / x))
end function
public static double code(double x) {
return 1.0 + (-1.0 - (2.0 / x));
}
def code(x): return 1.0 + (-1.0 - (2.0 / x))
function code(x) return Float64(1.0 + Float64(-1.0 - Float64(2.0 / x))) end
function tmp = code(x) tmp = 1.0 + (-1.0 - (2.0 / x)); end
code[x_] := N[(1.0 + N[(-1.0 - N[(2.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 + \left(-1 - \frac{2}{x}\right)
\end{array}
Initial program 82.7%
associate-+l-82.7%
sub-neg82.7%
neg-mul-182.7%
metadata-eval82.7%
cancel-sign-sub-inv82.7%
+-commutative82.7%
*-lft-identity82.7%
sub-neg82.7%
metadata-eval82.7%
Simplified82.7%
Taylor expanded in x around 0 48.0%
Taylor expanded in x around 0 80.1%
Final simplification80.1%
(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 82.7%
associate-+l-82.7%
sub-neg82.7%
neg-mul-182.7%
metadata-eval82.7%
cancel-sign-sub-inv82.7%
+-commutative82.7%
*-lft-identity82.7%
sub-neg82.7%
metadata-eval82.7%
Simplified82.7%
Taylor expanded in x around 0 48.9%
Final simplification48.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 2023261
(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))))