
(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
(let* ((t_0 (+ (- (/ 1.0 (+ 1.0 x)) (/ 2.0 x)) (/ 1.0 (+ x -1.0))))
(t_1 (- (* x x) x)))
(if (or (<= t_0 -2e-10) (not (<= t_0 1e-31)))
(/ (+ t_1 (* (+ 1.0 x) (- x (fma x 2.0 -2.0)))) (* (+ 1.0 x) t_1))
(/ 2.0 (pow x 3.0)))))
double code(double x) {
double t_0 = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0));
double t_1 = (x * x) - x;
double tmp;
if ((t_0 <= -2e-10) || !(t_0 <= 1e-31)) {
tmp = (t_1 + ((1.0 + x) * (x - fma(x, 2.0, -2.0)))) / ((1.0 + x) * t_1);
} else {
tmp = 2.0 / pow(x, 3.0);
}
return tmp;
}
function code(x) t_0 = Float64(Float64(Float64(1.0 / Float64(1.0 + x)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x + -1.0))) t_1 = Float64(Float64(x * x) - x) tmp = 0.0 if ((t_0 <= -2e-10) || !(t_0 <= 1e-31)) tmp = Float64(Float64(t_1 + Float64(Float64(1.0 + x) * Float64(x - fma(x, 2.0, -2.0)))) / Float64(Float64(1.0 + x) * t_1)); else tmp = Float64(2.0 / (x ^ 3.0)); end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(x * x), $MachinePrecision] - x), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -2e-10], N[Not[LessEqual[t$95$0, 1e-31]], $MachinePrecision]], N[(N[(t$95$1 + N[(N[(1.0 + x), $MachinePrecision] * N[(x - N[(x * 2.0 + -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision], N[(2.0 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x + -1}\\
t_1 := x \cdot x - x\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-10} \lor \neg \left(t_0 \leq 10^{-31}\right):\\
\;\;\;\;\frac{t_1 + \left(1 + x\right) \cdot \left(x - \mathsf{fma}\left(x, 2, -2\right)\right)}{\left(1 + x\right) \cdot t_1}\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{{x}^{3}}\\
\end{array}
\end{array}
if (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < -2.00000000000000007e-10 or 1e-31 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) Initial program 99.6%
Simplified99.6%
frac-sub99.6%
frac-sub100.0%
*-un-lft-identity100.0%
distribute-rgt-in100.0%
neg-mul-1100.0%
sub-neg100.0%
*-rgt-identity100.0%
distribute-rgt-in100.0%
metadata-eval100.0%
metadata-eval100.0%
fma-def100.0%
metadata-eval100.0%
distribute-rgt-in100.0%
neg-mul-1100.0%
sub-neg100.0%
Applied egg-rr100.0%
+-commutative100.0%
remove-double-neg100.0%
metadata-eval100.0%
distribute-neg-in100.0%
neg-mul-1100.0%
*-commutative100.0%
fma-udef100.0%
distribute-lft-neg-in100.0%
distribute-lft-neg-in100.0%
fma-udef100.0%
*-commutative100.0%
neg-mul-1100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
metadata-eval100.0%
+-commutative100.0%
Simplified100.0%
if -2.00000000000000007e-10 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 1e-31Initial program 66.8%
Simplified66.8%
Taylor expanded in x around inf 99.0%
Final simplification99.5%
(FPCore (x)
:precision binary64
(let* ((t_0 (+ (- (/ 1.0 (+ 1.0 x)) (/ 2.0 x)) (/ 1.0 (+ x -1.0))))
(t_1 (- 1.0 (* x x))))
(if (<= t_0 -2e-10)
t_0
(if (<= t_0 1e-31)
(/ 2.0 (pow x 3.0))
(- (/ 1.0 t_1) (+ (+ (/ 2.0 x) (/ -1.0 (+ x -1.0))) (/ x t_1)))))))
double code(double x) {
double t_0 = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0));
double t_1 = 1.0 - (x * x);
double tmp;
if (t_0 <= -2e-10) {
tmp = t_0;
} else if (t_0 <= 1e-31) {
tmp = 2.0 / pow(x, 3.0);
} else {
tmp = (1.0 / t_1) - (((2.0 / x) + (-1.0 / (x + -1.0))) + (x / t_1));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = ((1.0d0 / (1.0d0 + x)) - (2.0d0 / x)) + (1.0d0 / (x + (-1.0d0)))
t_1 = 1.0d0 - (x * x)
if (t_0 <= (-2d-10)) then
tmp = t_0
else if (t_0 <= 1d-31) then
tmp = 2.0d0 / (x ** 3.0d0)
else
tmp = (1.0d0 / t_1) - (((2.0d0 / x) + ((-1.0d0) / (x + (-1.0d0)))) + (x / t_1))
end if
code = tmp
end function
public static double code(double x) {
double t_0 = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0));
double t_1 = 1.0 - (x * x);
double tmp;
if (t_0 <= -2e-10) {
tmp = t_0;
} else if (t_0 <= 1e-31) {
tmp = 2.0 / Math.pow(x, 3.0);
} else {
tmp = (1.0 / t_1) - (((2.0 / x) + (-1.0 / (x + -1.0))) + (x / t_1));
}
return tmp;
}
def code(x): t_0 = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0)) t_1 = 1.0 - (x * x) tmp = 0 if t_0 <= -2e-10: tmp = t_0 elif t_0 <= 1e-31: tmp = 2.0 / math.pow(x, 3.0) else: tmp = (1.0 / t_1) - (((2.0 / x) + (-1.0 / (x + -1.0))) + (x / t_1)) return tmp
function code(x) t_0 = Float64(Float64(Float64(1.0 / Float64(1.0 + x)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x + -1.0))) t_1 = Float64(1.0 - Float64(x * x)) tmp = 0.0 if (t_0 <= -2e-10) tmp = t_0; elseif (t_0 <= 1e-31) tmp = Float64(2.0 / (x ^ 3.0)); else tmp = Float64(Float64(1.0 / t_1) - Float64(Float64(Float64(2.0 / x) + Float64(-1.0 / Float64(x + -1.0))) + Float64(x / t_1))); end return tmp end
function tmp_2 = code(x) t_0 = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0)); t_1 = 1.0 - (x * x); tmp = 0.0; if (t_0 <= -2e-10) tmp = t_0; elseif (t_0 <= 1e-31) tmp = 2.0 / (x ^ 3.0); else tmp = (1.0 / t_1) - (((2.0 / x) + (-1.0 / (x + -1.0))) + (x / t_1)); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 - N[(x * x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -2e-10], t$95$0, If[LessEqual[t$95$0, 1e-31], N[(2.0 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / t$95$1), $MachinePrecision] - N[(N[(N[(2.0 / x), $MachinePrecision] + N[(-1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(x / t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x + -1}\\
t_1 := 1 - x \cdot x\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-10}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;t_0 \leq 10^{-31}:\\
\;\;\;\;\frac{2}{{x}^{3}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{t_1} - \left(\left(\frac{2}{x} + \frac{-1}{x + -1}\right) + \frac{x}{t_1}\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < -2.00000000000000007e-10Initial program 99.6%
if -2.00000000000000007e-10 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 1e-31Initial program 66.8%
Simplified66.8%
Taylor expanded in x around inf 99.0%
if 1e-31 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) Initial program 99.6%
Simplified99.6%
flip-+99.6%
sub-neg99.6%
metadata-eval99.6%
distribute-neg-in99.6%
+-commutative99.6%
associate-/r/99.6%
metadata-eval99.6%
+-commutative99.6%
distribute-neg-in99.6%
metadata-eval99.6%
sub-neg99.6%
Applied egg-rr99.6%
associate-*l/99.6%
*-lft-identity99.6%
Simplified99.6%
*-un-lft-identity99.6%
associate-*l/99.6%
sub-neg99.6%
distribute-rgt-in99.6%
*-un-lft-identity99.6%
Applied egg-rr99.6%
associate--l+99.7%
un-div-inv99.7%
Applied egg-rr99.7%
Final simplification99.3%
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ 1.0 x)) (/ 2.0 x)) (/ 1.0 (+ x -1.0))))
double code(double x) {
return ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / (1.0d0 + x)) - (2.0d0 / x)) + (1.0d0 / (x + (-1.0d0)))
end function
public static double code(double x) {
return ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0));
}
def code(x): return ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0))
function code(x) return Float64(Float64(Float64(1.0 / Float64(1.0 + x)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x + -1.0))) end
function tmp = code(x) tmp = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0)); end
code[x_] := N[(N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x + -1}
\end{array}
Initial program 84.6%
Final simplification84.6%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 1.15e+77))) (/ -1.0 (* x x)) (- 1.0 (/ 2.0 x))))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 1.15e+77)) {
tmp = -1.0 / (x * x);
} else {
tmp = 1.0 - (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.15d+77))) then
tmp = (-1.0d0) / (x * x)
else
tmp = 1.0d0 - (2.0d0 / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 1.15e+77)) {
tmp = -1.0 / (x * x);
} else {
tmp = 1.0 - (2.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if (x <= -1.0) or not (x <= 1.15e+77): tmp = -1.0 / (x * x) else: tmp = 1.0 - (2.0 / x) return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 1.15e+77)) tmp = Float64(-1.0 / Float64(x * x)); else tmp = Float64(1.0 - Float64(2.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -1.0) || ~((x <= 1.15e+77))) tmp = -1.0 / (x * x); else tmp = 1.0 - (2.0 / x); end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.15e+77]], $MachinePrecision]], N[(-1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision], N[(1.0 - N[(2.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1.15 \cdot 10^{+77}\right):\\
\;\;\;\;\frac{-1}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;1 - \frac{2}{x}\\
\end{array}
\end{array}
if x < -1 or 1.14999999999999997e77 < x Initial program 78.9%
Simplified78.9%
flip-+21.5%
sub-neg21.5%
metadata-eval21.5%
distribute-neg-in21.5%
+-commutative21.5%
associate-/r/19.5%
metadata-eval19.5%
+-commutative19.5%
distribute-neg-in19.5%
metadata-eval19.5%
sub-neg19.5%
Applied egg-rr19.5%
Taylor expanded in x around inf 18.8%
Taylor expanded in x around inf 62.7%
unpow262.7%
Simplified62.7%
if -1 < x < 1.14999999999999997e77Initial program 88.3%
Simplified88.3%
Taylor expanded in x around 0 86.7%
Taylor expanded in x around 0 86.3%
Final simplification77.2%
(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 84.6%
Simplified84.6%
Taylor expanded in x around 0 54.3%
Taylor expanded in x around 0 83.1%
Final simplification83.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 84.6%
Simplified84.6%
Taylor expanded in x around 0 54.9%
Final simplification54.9%
(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 84.6%
Simplified84.6%
Taylor expanded in x around 0 54.2%
Taylor expanded in x around inf 3.3%
Final simplification3.3%
(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 2023275
(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))))