
(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 (/ -2.0 (- x (pow x 3.0))))
double code(double x) {
return -2.0 / (x - pow(x, 3.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-2.0d0) / (x - (x ** 3.0d0))
end function
public static double code(double x) {
return -2.0 / (x - Math.pow(x, 3.0));
}
def code(x): return -2.0 / (x - math.pow(x, 3.0))
function code(x) return Float64(-2.0 / Float64(x - (x ^ 3.0))) end
function tmp = code(x) tmp = -2.0 / (x - (x ^ 3.0)); end
code[x_] := N[(-2.0 / N[(x - N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2}{x - {x}^{3}}
\end{array}
Initial program 86.2%
sub-neg86.2%
distribute-neg-frac86.2%
metadata-eval86.2%
metadata-eval86.2%
metadata-eval86.2%
associate-/r*86.2%
metadata-eval86.2%
neg-mul-186.2%
+-commutative86.2%
associate-+l+86.2%
+-commutative86.2%
neg-mul-186.2%
metadata-eval86.2%
associate-/r*86.2%
metadata-eval86.2%
metadata-eval86.2%
+-commutative86.2%
+-commutative86.2%
Simplified86.2%
+-commutative86.2%
frac-add55.3%
frac-add57.8%
*-un-lft-identity57.8%
*-commutative57.8%
neg-mul-157.8%
distribute-neg-in57.8%
metadata-eval57.8%
Applied egg-rr57.8%
Taylor expanded in x around 0 99.9%
Taylor expanded in x around 0 99.9%
mul-1-neg99.9%
unsub-neg99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= x 1.0) (- (* -2.0 x) (/ 2.0 x)) (/ (/ 2.0 x) (* x (+ x 2.0)))))
double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = (-2.0 * x) - (2.0 / x);
} else {
tmp = (2.0 / x) / (x * (x + 2.0));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.0d0) then
tmp = ((-2.0d0) * x) - (2.0d0 / x)
else
tmp = (2.0d0 / x) / (x * (x + 2.0d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = (-2.0 * x) - (2.0 / x);
} else {
tmp = (2.0 / x) / (x * (x + 2.0));
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.0: tmp = (-2.0 * x) - (2.0 / x) else: tmp = (2.0 / x) / (x * (x + 2.0)) return tmp
function code(x) tmp = 0.0 if (x <= 1.0) tmp = Float64(Float64(-2.0 * x) - Float64(2.0 / x)); else tmp = Float64(Float64(2.0 / x) / Float64(x * Float64(x + 2.0))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.0) tmp = (-2.0 * x) - (2.0 / x); else tmp = (2.0 / x) / (x * (x + 2.0)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.0], N[(N[(-2.0 * x), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 / x), $MachinePrecision] / N[(x * N[(x + 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;-2 \cdot x - \frac{2}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{2}{x}}{x \cdot \left(x + 2\right)}\\
\end{array}
\end{array}
if x < 1Initial program 91.3%
sub-neg91.3%
distribute-neg-frac91.3%
metadata-eval91.3%
metadata-eval91.3%
metadata-eval91.3%
associate-/r*91.3%
metadata-eval91.3%
neg-mul-191.3%
+-commutative91.3%
associate-+l+91.3%
+-commutative91.3%
neg-mul-191.3%
metadata-eval91.3%
associate-/r*91.3%
metadata-eval91.3%
metadata-eval91.3%
+-commutative91.3%
+-commutative91.3%
Simplified91.3%
Taylor expanded in x around 0 62.7%
associate-*r/62.7%
metadata-eval62.7%
Simplified62.7%
if 1 < x Initial program 71.3%
sub-neg71.3%
distribute-neg-frac71.3%
metadata-eval71.3%
metadata-eval71.3%
metadata-eval71.3%
associate-/r*71.3%
metadata-eval71.3%
neg-mul-171.3%
+-commutative71.3%
associate-+l+71.2%
+-commutative71.2%
neg-mul-171.2%
metadata-eval71.2%
associate-/r*71.2%
metadata-eval71.2%
metadata-eval71.2%
+-commutative71.2%
+-commutative71.2%
Simplified71.2%
+-commutative71.2%
frac-add13.5%
frac-add18.6%
*-un-lft-identity18.6%
*-commutative18.6%
neg-mul-118.6%
distribute-neg-in18.6%
metadata-eval18.6%
Applied egg-rr18.6%
Taylor expanded in x around 0 99.9%
frac-2neg99.9%
metadata-eval99.9%
div-inv99.9%
distribute-rgt-neg-in99.9%
add-sqr-sqrt0.0%
sqrt-unprod69.0%
sqr-neg69.0%
sqrt-unprod69.0%
add-sqr-sqrt69.0%
*-commutative69.0%
associate-/r*69.0%
sub-neg69.0%
distribute-rgt-in69.0%
add-sqr-sqrt0.0%
sqrt-unprod97.2%
sqr-neg97.2%
sqrt-unprod97.1%
add-sqr-sqrt97.2%
distribute-rgt-in97.2%
pow297.2%
Applied egg-rr97.2%
associate-*r/97.2%
associate-*r/97.2%
metadata-eval97.2%
+-commutative97.2%
Simplified97.2%
Taylor expanded in x around inf 97.2%
+-commutative97.2%
unpow297.2%
distribute-rgt-out97.2%
Simplified97.2%
Final simplification71.4%
(FPCore (x) :precision binary64 (/ -2.0 (* x (* (+ x 1.0) (- 1.0 x)))))
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 = (-2.0d0) / (x * ((x + 1.0d0) * (1.0d0 - x)))
end function
public static double code(double x) {
return -2.0 / (x * ((x + 1.0) * (1.0 - x)));
}
def code(x): return -2.0 / (x * ((x + 1.0) * (1.0 - x)))
function code(x) return Float64(-2.0 / Float64(x * Float64(Float64(x + 1.0) * Float64(1.0 - x)))) end
function tmp = code(x) tmp = -2.0 / (x * ((x + 1.0) * (1.0 - x))); end
code[x_] := N[(-2.0 / N[(x * N[(N[(x + 1.0), $MachinePrecision] * N[(1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2}{x \cdot \left(\left(x + 1\right) \cdot \left(1 - x\right)\right)}
\end{array}
Initial program 86.2%
sub-neg86.2%
distribute-neg-frac86.2%
metadata-eval86.2%
metadata-eval86.2%
metadata-eval86.2%
associate-/r*86.2%
metadata-eval86.2%
neg-mul-186.2%
+-commutative86.2%
associate-+l+86.2%
+-commutative86.2%
neg-mul-186.2%
metadata-eval86.2%
associate-/r*86.2%
metadata-eval86.2%
metadata-eval86.2%
+-commutative86.2%
+-commutative86.2%
Simplified86.2%
+-commutative86.2%
frac-add55.3%
frac-add57.8%
*-un-lft-identity57.8%
*-commutative57.8%
neg-mul-157.8%
distribute-neg-in57.8%
metadata-eval57.8%
Applied egg-rr57.8%
Taylor expanded in x around 0 99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= x 1.0) (- (/ -2.0 x) x) (+ (/ 2.0 x) (/ -2.0 x))))
double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = (-2.0 / x) - x;
} else {
tmp = (2.0 / x) + (-2.0 / x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.0d0) then
tmp = ((-2.0d0) / x) - x
else
tmp = (2.0d0 / x) + ((-2.0d0) / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = (-2.0 / x) - x;
} else {
tmp = (2.0 / x) + (-2.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.0: tmp = (-2.0 / x) - x else: tmp = (2.0 / x) + (-2.0 / x) return tmp
function code(x) tmp = 0.0 if (x <= 1.0) tmp = Float64(Float64(-2.0 / x) - x); else tmp = Float64(Float64(2.0 / x) + Float64(-2.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.0) tmp = (-2.0 / x) - x; else tmp = (2.0 / x) + (-2.0 / x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.0], N[(N[(-2.0 / x), $MachinePrecision] - x), $MachinePrecision], N[(N[(2.0 / x), $MachinePrecision] + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;\frac{-2}{x} - x\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{x} + \frac{-2}{x}\\
\end{array}
\end{array}
if x < 1Initial program 91.3%
associate-+l-91.3%
sub-neg91.3%
+-commutative91.3%
sub-neg91.3%
distribute-neg-in91.3%
distribute-neg-frac91.3%
metadata-eval91.3%
remove-double-neg91.3%
sub-neg91.3%
metadata-eval91.3%
Simplified91.3%
Taylor expanded in x around 0 62.5%
Taylor expanded in x around 0 62.2%
mul-1-neg62.2%
associate-*r/62.2%
metadata-eval62.2%
Simplified62.2%
Taylor expanded in x around 0 62.2%
sub-neg62.2%
associate-*r/62.2%
metadata-eval62.2%
distribute-neg-frac62.2%
metadata-eval62.2%
+-commutative62.2%
mul-1-neg62.2%
sub-neg62.2%
Simplified62.2%
if 1 < x Initial program 71.3%
sub-neg71.3%
distribute-neg-frac71.3%
metadata-eval71.3%
metadata-eval71.3%
metadata-eval71.3%
associate-/r*71.3%
metadata-eval71.3%
neg-mul-171.3%
+-commutative71.3%
associate-+l+71.2%
+-commutative71.2%
neg-mul-171.2%
metadata-eval71.2%
associate-/r*71.2%
metadata-eval71.2%
metadata-eval71.2%
+-commutative71.2%
+-commutative71.2%
Simplified71.2%
Taylor expanded in x around inf 69.5%
Final simplification64.0%
(FPCore (x) :precision binary64 (if (<= x 1.0) (- (* -2.0 x) (/ 2.0 x)) (+ (/ 2.0 x) (/ -2.0 x))))
double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = (-2.0 * x) - (2.0 / x);
} else {
tmp = (2.0 / x) + (-2.0 / x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.0d0) then
tmp = ((-2.0d0) * x) - (2.0d0 / x)
else
tmp = (2.0d0 / x) + ((-2.0d0) / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = (-2.0 * x) - (2.0 / x);
} else {
tmp = (2.0 / x) + (-2.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.0: tmp = (-2.0 * x) - (2.0 / x) else: tmp = (2.0 / x) + (-2.0 / x) return tmp
function code(x) tmp = 0.0 if (x <= 1.0) tmp = Float64(Float64(-2.0 * x) - Float64(2.0 / x)); else tmp = Float64(Float64(2.0 / x) + Float64(-2.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.0) tmp = (-2.0 * x) - (2.0 / x); else tmp = (2.0 / x) + (-2.0 / x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.0], N[(N[(-2.0 * x), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 / x), $MachinePrecision] + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;-2 \cdot x - \frac{2}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{x} + \frac{-2}{x}\\
\end{array}
\end{array}
if x < 1Initial program 91.3%
sub-neg91.3%
distribute-neg-frac91.3%
metadata-eval91.3%
metadata-eval91.3%
metadata-eval91.3%
associate-/r*91.3%
metadata-eval91.3%
neg-mul-191.3%
+-commutative91.3%
associate-+l+91.3%
+-commutative91.3%
neg-mul-191.3%
metadata-eval91.3%
associate-/r*91.3%
metadata-eval91.3%
metadata-eval91.3%
+-commutative91.3%
+-commutative91.3%
Simplified91.3%
Taylor expanded in x around 0 62.7%
associate-*r/62.7%
metadata-eval62.7%
Simplified62.7%
if 1 < x Initial program 71.3%
sub-neg71.3%
distribute-neg-frac71.3%
metadata-eval71.3%
metadata-eval71.3%
metadata-eval71.3%
associate-/r*71.3%
metadata-eval71.3%
neg-mul-171.3%
+-commutative71.3%
associate-+l+71.2%
+-commutative71.2%
neg-mul-171.2%
metadata-eval71.2%
associate-/r*71.2%
metadata-eval71.2%
metadata-eval71.2%
+-commutative71.2%
+-commutative71.2%
Simplified71.2%
Taylor expanded in x around inf 69.5%
Final simplification64.4%
(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 86.2%
sub-neg86.2%
distribute-neg-frac86.2%
metadata-eval86.2%
metadata-eval86.2%
metadata-eval86.2%
associate-/r*86.2%
metadata-eval86.2%
neg-mul-186.2%
+-commutative86.2%
associate-+l+86.2%
+-commutative86.2%
neg-mul-186.2%
metadata-eval86.2%
associate-/r*86.2%
metadata-eval86.2%
metadata-eval86.2%
+-commutative86.2%
+-commutative86.2%
Simplified86.2%
Taylor expanded in x around 0 48.5%
Final simplification48.5%
(FPCore (x) :precision binary64 (- x))
double code(double x) {
return -x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = -x
end function
public static double code(double x) {
return -x;
}
def code(x): return -x
function code(x) return Float64(-x) end
function tmp = code(x) tmp = -x; end
code[x_] := (-x)
\begin{array}{l}
\\
-x
\end{array}
Initial program 86.2%
associate-+l-86.2%
sub-neg86.2%
+-commutative86.2%
sub-neg86.2%
distribute-neg-in86.2%
distribute-neg-frac86.2%
metadata-eval86.2%
remove-double-neg86.2%
sub-neg86.2%
metadata-eval86.2%
Simplified86.2%
Taylor expanded in x around 0 47.6%
Taylor expanded in x around 0 46.9%
mul-1-neg46.9%
associate-*r/46.9%
metadata-eval46.9%
Simplified46.9%
Taylor expanded in x around inf 3.0%
mul-1-neg3.0%
Simplified3.0%
Final simplification3.0%
(FPCore (x) :precision binary64 1.0)
double code(double x) {
return 1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0
end function
public static double code(double x) {
return 1.0;
}
def code(x): return 1.0
function code(x) return 1.0 end
function tmp = code(x) tmp = 1.0; end
code[x_] := 1.0
\begin{array}{l}
\\
1
\end{array}
Initial program 86.2%
associate-+l-86.2%
sub-neg86.2%
+-commutative86.2%
sub-neg86.2%
distribute-neg-in86.2%
distribute-neg-frac86.2%
metadata-eval86.2%
remove-double-neg86.2%
sub-neg86.2%
metadata-eval86.2%
Simplified86.2%
Taylor expanded in x around 0 47.6%
Taylor expanded in x around inf 3.5%
Final simplification3.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 2023319
(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))))