
(FPCore (N) :precision binary64 (- (log (+ N 1.0)) (log N)))
double code(double N) {
return log((N + 1.0)) - log(N);
}
real(8) function code(n)
real(8), intent (in) :: n
code = log((n + 1.0d0)) - log(n)
end function
public static double code(double N) {
return Math.log((N + 1.0)) - Math.log(N);
}
def code(N): return math.log((N + 1.0)) - math.log(N)
function code(N) return Float64(log(Float64(N + 1.0)) - log(N)) end
function tmp = code(N) tmp = log((N + 1.0)) - log(N); end
code[N_] := N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\log \left(N + 1\right) - \log N
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (N) :precision binary64 (- (log (+ N 1.0)) (log N)))
double code(double N) {
return log((N + 1.0)) - log(N);
}
real(8) function code(n)
real(8), intent (in) :: n
code = log((n + 1.0d0)) - log(n)
end function
public static double code(double N) {
return Math.log((N + 1.0)) - Math.log(N);
}
def code(N): return math.log((N + 1.0)) - math.log(N)
function code(N) return Float64(log(Float64(N + 1.0)) - log(N)) end
function tmp = code(N) tmp = log((N + 1.0)) - log(N); end
code[N_] := N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\log \left(N + 1\right) - \log N
\end{array}
(FPCore (N) :precision binary64 (log1p (/ 1.0 N)))
double code(double N) {
return log1p((1.0 / N));
}
public static double code(double N) {
return Math.log1p((1.0 / N));
}
def code(N): return math.log1p((1.0 / N))
function code(N) return log1p(Float64(1.0 / N)) end
code[N_] := N[Log[1 + N[(1.0 / N), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{log1p}\left(\frac{1}{N}\right)
\end{array}
Initial program 48.5%
diff-log48.6%
Applied egg-rr48.6%
log1p-expm1-u48.6%
expm1-udef48.6%
add-exp-log48.6%
Applied egg-rr48.6%
sub-neg48.6%
*-lft-identity48.6%
associate-*l/48.2%
distribute-rgt-in48.2%
rgt-mult-inverse48.6%
*-lft-identity48.6%
metadata-eval48.6%
associate-+l+48.7%
Simplified48.7%
Taylor expanded in N around 0 100.0%
Final simplification100.0%
(FPCore (N) :precision binary64 (if (<= N 0.68) (- (log N)) (/ (- 1.0 (/ 0.5 N)) N)))
double code(double N) {
double tmp;
if (N <= 0.68) {
tmp = -log(N);
} else {
tmp = (1.0 - (0.5 / N)) / N;
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if (n <= 0.68d0) then
tmp = -log(n)
else
tmp = (1.0d0 - (0.5d0 / n)) / n
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if (N <= 0.68) {
tmp = -Math.log(N);
} else {
tmp = (1.0 - (0.5 / N)) / N;
}
return tmp;
}
def code(N): tmp = 0 if N <= 0.68: tmp = -math.log(N) else: tmp = (1.0 - (0.5 / N)) / N return tmp
function code(N) tmp = 0.0 if (N <= 0.68) tmp = Float64(-log(N)); else tmp = Float64(Float64(1.0 - Float64(0.5 / N)) / N); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if (N <= 0.68) tmp = -log(N); else tmp = (1.0 - (0.5 / N)) / N; end tmp_2 = tmp; end
code[N_] := If[LessEqual[N, 0.68], (-N[Log[N], $MachinePrecision]), N[(N[(1.0 - N[(0.5 / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;N \leq 0.68:\\
\;\;\;\;-\log N\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \frac{0.5}{N}}{N}\\
\end{array}
\end{array}
if N < 0.680000000000000049Initial program 100.0%
Taylor expanded in N around 0 98.9%
neg-mul-198.9%
Simplified98.9%
if 0.680000000000000049 < N Initial program 7.1%
Taylor expanded in N around inf 99.2%
associate-*r/99.2%
metadata-eval99.2%
unpow299.2%
Simplified99.2%
Taylor expanded in N around 0 99.2%
unpow299.2%
associate-*r/99.2%
metadata-eval99.2%
associate-/l/99.2%
div-sub99.2%
Simplified99.2%
Final simplification99.1%
(FPCore (N) :precision binary64 (/ 1.0 N))
double code(double N) {
return 1.0 / N;
}
real(8) function code(n)
real(8), intent (in) :: n
code = 1.0d0 / n
end function
public static double code(double N) {
return 1.0 / N;
}
def code(N): return 1.0 / N
function code(N) return Float64(1.0 / N) end
function tmp = code(N) tmp = 1.0 / N; end
code[N_] := N[(1.0 / N), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{N}
\end{array}
Initial program 48.5%
Taylor expanded in N around inf 57.1%
Final simplification57.1%
(FPCore (N) :precision binary64 N)
double code(double N) {
return N;
}
real(8) function code(n)
real(8), intent (in) :: n
code = n
end function
public static double code(double N) {
return N;
}
def code(N): return N
function code(N) return N end
function tmp = code(N) tmp = N; end
code[N_] := N
\begin{array}{l}
\\
N
\end{array}
Initial program 48.5%
Taylor expanded in N around 0 46.5%
neg-mul-146.5%
unsub-neg46.5%
Simplified46.5%
Taylor expanded in N around inf 4.4%
Final simplification4.4%
herbie shell --seed 2023274
(FPCore (N)
:name "2log (problem 3.3.6)"
:precision binary64
(- (log (+ N 1.0)) (log N)))