
(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}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (x) :precision binary64 (/ (/ -1.0 x) (+ 1.0 x)))
double code(double x) {
return (-1.0 / x) / (1.0 + x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((-1.0d0) / x) / (1.0d0 + x)
end function
public static double code(double x) {
return (-1.0 / x) / (1.0 + x);
}
def code(x): return (-1.0 / x) / (1.0 + x)
function code(x) return Float64(Float64(-1.0 / x) / Float64(1.0 + x)) end
function tmp = code(x) tmp = (-1.0 / x) / (1.0 + x); end
code[x_] := N[(N[(-1.0 / x), $MachinePrecision] / N[(1.0 + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{-1}{x}}{1 + x}
\end{array}
Initial program 78.9%
frac-sub80.2%
div-inv80.2%
*-un-lft-identity80.2%
*-rgt-identity80.2%
+-commutative80.2%
metadata-eval80.2%
frac-times80.1%
clear-num80.1%
associate-*l/80.2%
*-un-lft-identity80.2%
div-inv80.2%
metadata-eval80.2%
*-rgt-identity80.2%
+-commutative80.2%
Applied egg-rr80.2%
Taylor expanded in x around 0 99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 0.76))) (/ -1.0 (* x x)) (+ 1.0 (/ -1.0 x))))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 0.76)) {
tmp = -1.0 / (x * x);
} else {
tmp = 1.0 + (-1.0 / x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x <= (-1.0d0)) .or. (.not. (x <= 0.76d0))) then
tmp = (-1.0d0) / (x * x)
else
tmp = 1.0d0 + ((-1.0d0) / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 0.76)) {
tmp = -1.0 / (x * x);
} else {
tmp = 1.0 + (-1.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if (x <= -1.0) or not (x <= 0.76): tmp = -1.0 / (x * x) else: tmp = 1.0 + (-1.0 / x) return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 0.76)) tmp = Float64(-1.0 / Float64(x * x)); else tmp = Float64(1.0 + Float64(-1.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -1.0) || ~((x <= 0.76))) tmp = -1.0 / (x * x); else tmp = 1.0 + (-1.0 / x); end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 0.76]], $MachinePrecision]], N[(-1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 0.76\right):\\
\;\;\;\;\frac{-1}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{-1}{x}\\
\end{array}
\end{array}
if x < -1 or 0.76000000000000001 < x Initial program 56.5%
Taylor expanded in x around inf 97.3%
unpow297.3%
Simplified97.3%
if -1 < x < 0.76000000000000001Initial program 100.0%
Taylor expanded in x around 0 97.6%
Final simplification97.4%
(FPCore (x) :precision binary64 (if (<= x -1.0) (/ -1.0 (* x x)) (if (<= x 0.76) (+ 1.0 (/ -1.0 x)) (/ (/ -1.0 x) x))))
double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = -1.0 / (x * x);
} else if (x <= 0.76) {
tmp = 1.0 + (-1.0 / x);
} else {
tmp = (-1.0 / x) / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-1.0d0)) then
tmp = (-1.0d0) / (x * x)
else if (x <= 0.76d0) then
tmp = 1.0d0 + ((-1.0d0) / x)
else
tmp = ((-1.0d0) / x) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = -1.0 / (x * x);
} else if (x <= 0.76) {
tmp = 1.0 + (-1.0 / x);
} else {
tmp = (-1.0 / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= -1.0: tmp = -1.0 / (x * x) elif x <= 0.76: tmp = 1.0 + (-1.0 / x) else: tmp = (-1.0 / x) / x return tmp
function code(x) tmp = 0.0 if (x <= -1.0) tmp = Float64(-1.0 / Float64(x * x)); elseif (x <= 0.76) tmp = Float64(1.0 + Float64(-1.0 / x)); else tmp = Float64(Float64(-1.0 / x) / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -1.0) tmp = -1.0 / (x * x); elseif (x <= 0.76) tmp = 1.0 + (-1.0 / x); else tmp = (-1.0 / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -1.0], N[(-1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 0.76], N[(1.0 + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision], N[(N[(-1.0 / x), $MachinePrecision] / x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;\frac{-1}{x \cdot x}\\
\mathbf{elif}\;x \leq 0.76:\\
\;\;\;\;1 + \frac{-1}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{-1}{x}}{x}\\
\end{array}
\end{array}
if x < -1Initial program 56.8%
Taylor expanded in x around inf 97.7%
unpow297.7%
Simplified97.7%
if -1 < x < 0.76000000000000001Initial program 100.0%
Taylor expanded in x around 0 97.6%
if 0.76000000000000001 < x Initial program 56.1%
sub-neg56.1%
+-commutative56.1%
distribute-neg-frac56.1%
metadata-eval56.1%
Applied egg-rr56.1%
Simplified98.4%
distribute-rgt-in98.4%
*-un-lft-identity98.4%
Applied egg-rr98.4%
Taylor expanded in x around inf 96.9%
unpow296.9%
associate-/r*98.3%
Simplified98.3%
Final simplification97.8%
(FPCore (x) :precision binary64 (/ -1.0 (* x (+ 1.0 x))))
double code(double x) {
return -1.0 / (x * (1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-1.0d0) / (x * (1.0d0 + x))
end function
public static double code(double x) {
return -1.0 / (x * (1.0 + x));
}
def code(x): return -1.0 / (x * (1.0 + x))
function code(x) return Float64(-1.0 / Float64(x * Float64(1.0 + x))) end
function tmp = code(x) tmp = -1.0 / (x * (1.0 + x)); end
code[x_] := N[(-1.0 / N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{x \cdot \left(1 + x\right)}
\end{array}
Initial program 78.9%
sub-neg78.9%
+-commutative78.9%
distribute-neg-frac78.9%
metadata-eval78.9%
Applied egg-rr78.9%
Simplified99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (/ (/ 1.0 (- -1.0 x)) x))
double code(double x) {
return (1.0 / (-1.0 - x)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / ((-1.0d0) - x)) / x
end function
public static double code(double x) {
return (1.0 / (-1.0 - x)) / x;
}
def code(x): return (1.0 / (-1.0 - x)) / x
function code(x) return Float64(Float64(1.0 / Float64(-1.0 - x)) / x) end
function tmp = code(x) tmp = (1.0 / (-1.0 - x)) / x; end
code[x_] := N[(N[(1.0 / N[(-1.0 - x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{-1 - x}}{x}
\end{array}
Initial program 78.9%
frac-sub80.2%
*-rgt-identity80.2%
metadata-eval80.2%
div-inv80.2%
associate-/r*80.1%
*-un-lft-identity80.1%
*-rgt-identity80.1%
+-commutative80.1%
div-inv80.1%
metadata-eval80.1%
*-rgt-identity80.1%
+-commutative80.1%
Applied egg-rr80.1%
frac-2neg80.1%
div-inv80.1%
+-commutative80.1%
+-commutative80.1%
distribute-neg-in80.1%
neg-mul-180.1%
metadata-eval80.1%
fma-def80.1%
Applied egg-rr80.1%
associate-*r/80.1%
*-rgt-identity80.1%
distribute-neg-frac80.1%
neg-mul-180.1%
associate--r+99.9%
+-inverses99.9%
metadata-eval99.9%
associate-*r/99.9%
metadata-eval99.9%
fma-udef99.9%
neg-mul-199.9%
+-commutative99.9%
unsub-neg99.9%
Simplified99.9%
Final simplification99.9%
(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 78.9%
Taylor expanded in x around 0 52.7%
Final simplification52.7%
herbie shell --seed 2023192
(FPCore (x)
:name "2frac (problem 3.3.1)"
:precision binary64
(- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))