
(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 (+ (* 2.0 (pow x -3.0)) (/ 2.0 (pow x 5.0))))
double code(double x) {
return (2.0 * pow(x, -3.0)) + (2.0 / pow(x, 5.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (2.0d0 * (x ** (-3.0d0))) + (2.0d0 / (x ** 5.0d0))
end function
public static double code(double x) {
return (2.0 * Math.pow(x, -3.0)) + (2.0 / Math.pow(x, 5.0));
}
def code(x): return (2.0 * math.pow(x, -3.0)) + (2.0 / math.pow(x, 5.0))
function code(x) return Float64(Float64(2.0 * (x ^ -3.0)) + Float64(2.0 / (x ^ 5.0))) end
function tmp = code(x) tmp = (2.0 * (x ^ -3.0)) + (2.0 / (x ^ 5.0)); end
code[x_] := N[(N[(2.0 * N[Power[x, -3.0], $MachinePrecision]), $MachinePrecision] + N[(2.0 / N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot {x}^{-3} + \frac{2}{{x}^{5}}
\end{array}
Initial program 71.9%
sub-neg71.9%
distribute-neg-frac71.9%
metadata-eval71.9%
metadata-eval71.9%
metadata-eval71.9%
associate-/r*71.9%
metadata-eval71.9%
neg-mul-171.9%
+-commutative71.9%
associate-+l+71.8%
+-commutative71.8%
neg-mul-171.8%
metadata-eval71.8%
associate-/r*71.8%
metadata-eval71.8%
metadata-eval71.8%
+-commutative71.8%
+-commutative71.8%
sub-neg71.8%
metadata-eval71.8%
Simplified71.8%
Taylor expanded in x around inf 98.5%
associate-*r/98.5%
metadata-eval98.5%
associate-*r/98.5%
metadata-eval98.5%
Simplified98.5%
div-inv98.5%
pow-flip99.1%
metadata-eval99.1%
Applied egg-rr99.1%
Final simplification99.1%
(FPCore (x) :precision binary64 (* 2.0 (pow x -3.0)))
double code(double x) {
return 2.0 * pow(x, -3.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 * (x ** (-3.0d0))
end function
public static double code(double x) {
return 2.0 * Math.pow(x, -3.0);
}
def code(x): return 2.0 * math.pow(x, -3.0)
function code(x) return Float64(2.0 * (x ^ -3.0)) end
function tmp = code(x) tmp = 2.0 * (x ^ -3.0); end
code[x_] := N[(2.0 * N[Power[x, -3.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot {x}^{-3}
\end{array}
Initial program 71.9%
sub-neg71.9%
distribute-neg-frac71.9%
metadata-eval71.9%
metadata-eval71.9%
metadata-eval71.9%
associate-/r*71.9%
metadata-eval71.9%
neg-mul-171.9%
+-commutative71.9%
associate-+l+71.8%
+-commutative71.8%
neg-mul-171.8%
metadata-eval71.8%
associate-/r*71.8%
metadata-eval71.8%
metadata-eval71.8%
+-commutative71.8%
+-commutative71.8%
sub-neg71.8%
metadata-eval71.8%
Simplified71.8%
Taylor expanded in x around inf 97.8%
div-inv98.5%
pow-flip99.1%
metadata-eval99.1%
Applied egg-rr98.4%
Final simplification98.4%
(FPCore (x) :precision binary64 (if (<= x 5e+85) (+ (/ -2.0 x) (* x (/ (/ 2.0 (+ x 1.0)) (+ x -1.0)))) (+ (/ -2.0 x) (/ 2.0 x))))
double code(double x) {
double tmp;
if (x <= 5e+85) {
tmp = (-2.0 / x) + (x * ((2.0 / (x + 1.0)) / (x + -1.0)));
} 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 <= 5d+85) then
tmp = ((-2.0d0) / x) + (x * ((2.0d0 / (x + 1.0d0)) / (x + (-1.0d0))))
else
tmp = ((-2.0d0) / x) + (2.0d0 / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 5e+85) {
tmp = (-2.0 / x) + (x * ((2.0 / (x + 1.0)) / (x + -1.0)));
} else {
tmp = (-2.0 / x) + (2.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 5e+85: tmp = (-2.0 / x) + (x * ((2.0 / (x + 1.0)) / (x + -1.0))) else: tmp = (-2.0 / x) + (2.0 / x) return tmp
function code(x) tmp = 0.0 if (x <= 5e+85) tmp = Float64(Float64(-2.0 / x) + Float64(x * Float64(Float64(2.0 / Float64(x + 1.0)) / Float64(x + -1.0)))); 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 <= 5e+85) tmp = (-2.0 / x) + (x * ((2.0 / (x + 1.0)) / (x + -1.0))); else tmp = (-2.0 / x) + (2.0 / x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 5e+85], N[(N[(-2.0 / x), $MachinePrecision] + N[(x * N[(N[(2.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(-2.0 / x), $MachinePrecision] + N[(2.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5 \cdot 10^{+85}:\\
\;\;\;\;\frac{-2}{x} + x \cdot \frac{\frac{2}{x + 1}}{x + -1}\\
\mathbf{else}:\\
\;\;\;\;\frac{-2}{x} + \frac{2}{x}\\
\end{array}
\end{array}
if x < 5.0000000000000001e85Initial program 57.6%
sub-neg57.6%
distribute-neg-frac57.6%
metadata-eval57.6%
metadata-eval57.6%
metadata-eval57.6%
associate-/r*57.6%
metadata-eval57.6%
neg-mul-157.6%
+-commutative57.6%
associate-+l+57.5%
+-commutative57.5%
neg-mul-157.5%
metadata-eval57.5%
associate-/r*57.5%
metadata-eval57.5%
metadata-eval57.5%
+-commutative57.5%
+-commutative57.5%
sub-neg57.5%
metadata-eval57.5%
Simplified57.5%
frac-add19.6%
div-inv18.4%
*-un-lft-identity18.4%
*-rgt-identity18.4%
+-commutative18.4%
+-commutative18.4%
metadata-eval18.4%
frac-times15.8%
clear-num15.8%
frac-times18.4%
metadata-eval18.4%
+-commutative18.4%
/-rgt-identity18.4%
Applied egg-rr18.4%
associate-*r/19.6%
*-rgt-identity19.6%
associate-+l+19.6%
+-commutative19.6%
associate-+r+19.6%
metadata-eval19.6%
*-commutative19.6%
Simplified19.6%
Applied egg-rr54.2%
frac-add57.4%
*-commutative57.4%
associate-*l/20.3%
fma-def9.1%
add-exp-log2.1%
log1p-expm1-u2.1%
expm1-def2.1%
add-exp-log2.1%
*-un-lft-identity2.1%
fma-neg2.1%
metadata-eval2.1%
fma-def2.1%
*-un-lft-identity2.1%
log1p-udef2.1%
add-exp-log9.1%
associate-+l+9.1%
fma-def20.3%
Applied egg-rr19.6%
associate-/r*57.5%
div-inv57.5%
*-un-lft-identity57.5%
times-frac57.5%
metadata-eval57.5%
Applied egg-rr57.5%
associate-*r/57.5%
*-rgt-identity57.5%
associate-*r/57.5%
*-commutative57.5%
associate-*r/57.4%
*-lft-identity57.4%
times-frac20.3%
/-rgt-identity20.3%
Simplified20.3%
if 5.0000000000000001e85 < x Initial program 94.9%
sub-neg94.9%
distribute-neg-frac94.9%
metadata-eval94.9%
metadata-eval94.9%
metadata-eval94.9%
associate-/r*94.9%
metadata-eval94.9%
neg-mul-194.9%
+-commutative94.9%
associate-+l+94.9%
+-commutative94.9%
neg-mul-194.9%
metadata-eval94.9%
associate-/r*94.9%
metadata-eval94.9%
metadata-eval94.9%
+-commutative94.9%
+-commutative94.9%
sub-neg94.9%
metadata-eval94.9%
Simplified94.9%
Taylor expanded in x around inf 94.9%
Final simplification48.8%
(FPCore (x) :precision binary64 (+ (* (/ x (+ x 1.0)) (/ 2.0 (+ x -1.0))) (/ -2.0 x)))
double code(double x) {
return ((x / (x + 1.0)) * (2.0 / (x + -1.0))) + (-2.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((x / (x + 1.0d0)) * (2.0d0 / (x + (-1.0d0)))) + ((-2.0d0) / x)
end function
public static double code(double x) {
return ((x / (x + 1.0)) * (2.0 / (x + -1.0))) + (-2.0 / x);
}
def code(x): return ((x / (x + 1.0)) * (2.0 / (x + -1.0))) + (-2.0 / x)
function code(x) return Float64(Float64(Float64(x / Float64(x + 1.0)) * Float64(2.0 / Float64(x + -1.0))) + Float64(-2.0 / x)) end
function tmp = code(x) tmp = ((x / (x + 1.0)) * (2.0 / (x + -1.0))) + (-2.0 / x); end
code[x_] := N[(N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] * N[(2.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{x + 1} \cdot \frac{2}{x + -1} + \frac{-2}{x}
\end{array}
Initial program 71.9%
sub-neg71.9%
distribute-neg-frac71.9%
metadata-eval71.9%
metadata-eval71.9%
metadata-eval71.9%
associate-/r*71.9%
metadata-eval71.9%
neg-mul-171.9%
+-commutative71.9%
associate-+l+71.8%
+-commutative71.8%
neg-mul-171.8%
metadata-eval71.8%
associate-/r*71.8%
metadata-eval71.8%
metadata-eval71.8%
+-commutative71.8%
+-commutative71.8%
sub-neg71.8%
metadata-eval71.8%
Simplified71.8%
frac-add19.3%
div-inv17.5%
*-un-lft-identity17.5%
*-rgt-identity17.5%
+-commutative17.5%
+-commutative17.5%
metadata-eval17.5%
frac-times16.7%
clear-num16.7%
frac-times17.5%
metadata-eval17.5%
+-commutative17.5%
/-rgt-identity17.5%
Applied egg-rr17.5%
associate-*r/19.3%
*-rgt-identity19.3%
associate-+l+19.3%
+-commutative19.3%
associate-+r+19.3%
metadata-eval19.3%
*-commutative19.3%
Simplified19.3%
Applied egg-rr68.3%
frac-add71.8%
*-commutative71.8%
associate-*l/21.3%
fma-def8.0%
add-exp-log3.7%
log1p-expm1-u3.7%
expm1-def3.7%
add-exp-log3.7%
*-un-lft-identity3.7%
fma-neg3.7%
metadata-eval3.7%
fma-def3.7%
*-un-lft-identity3.7%
log1p-udef3.7%
add-exp-log8.0%
associate-+l+8.0%
fma-def21.3%
Applied egg-rr19.3%
*-commutative19.3%
times-frac71.8%
Applied egg-rr71.8%
Final simplification71.8%
(FPCore (x) :precision binary64 (+ (/ -2.0 x) (/ 2.0 x)))
double code(double x) {
return (-2.0 / x) + (2.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((-2.0d0) / x) + (2.0d0 / x)
end function
public static double code(double x) {
return (-2.0 / x) + (2.0 / x);
}
def code(x): return (-2.0 / x) + (2.0 / x)
function code(x) return Float64(Float64(-2.0 / x) + Float64(2.0 / x)) end
function tmp = code(x) tmp = (-2.0 / x) + (2.0 / x); end
code[x_] := N[(N[(-2.0 / x), $MachinePrecision] + N[(2.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2}{x} + \frac{2}{x}
\end{array}
Initial program 71.9%
sub-neg71.9%
distribute-neg-frac71.9%
metadata-eval71.9%
metadata-eval71.9%
metadata-eval71.9%
associate-/r*71.9%
metadata-eval71.9%
neg-mul-171.9%
+-commutative71.9%
associate-+l+71.8%
+-commutative71.8%
neg-mul-171.8%
metadata-eval71.8%
associate-/r*71.8%
metadata-eval71.8%
metadata-eval71.8%
+-commutative71.8%
+-commutative71.8%
sub-neg71.8%
metadata-eval71.8%
Simplified71.8%
Taylor expanded in x around inf 69.5%
Final simplification69.5%
(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%
sub-neg71.9%
distribute-neg-frac71.9%
metadata-eval71.9%
metadata-eval71.9%
metadata-eval71.9%
associate-/r*71.9%
metadata-eval71.9%
neg-mul-171.9%
+-commutative71.9%
associate-+l+71.8%
+-commutative71.8%
neg-mul-171.8%
metadata-eval71.8%
associate-/r*71.8%
metadata-eval71.8%
metadata-eval71.8%
+-commutative71.8%
+-commutative71.8%
sub-neg71.8%
metadata-eval71.8%
Simplified71.8%
Taylor expanded in x around 0 5.0%
Final simplification5.0%
(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 2024039
(FPCore (x)
:name "3frac (problem 3.3.3)"
:precision binary64
:pre (> (fabs x) 1.0)
:herbie-target
(/ 2.0 (* x (- (* x x) 1.0)))
(+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))