
(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 6 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 (if (<= (- (log (+ N 1.0)) (log N)) 5e-7) (+ (/ 1.0 N) (- (/ 0.3333333333333333 (pow N 3.0)) (/ 0.5 (* N N)))) (fma (pow (log1p N) 0.6666666666666666) (cbrt (log1p N)) (- (log N)))))
double code(double N) {
double tmp;
if ((log((N + 1.0)) - log(N)) <= 5e-7) {
tmp = (1.0 / N) + ((0.3333333333333333 / pow(N, 3.0)) - (0.5 / (N * N)));
} else {
tmp = fma(pow(log1p(N), 0.6666666666666666), cbrt(log1p(N)), -log(N));
}
return tmp;
}
function code(N) tmp = 0.0 if (Float64(log(Float64(N + 1.0)) - log(N)) <= 5e-7) tmp = Float64(Float64(1.0 / N) + Float64(Float64(0.3333333333333333 / (N ^ 3.0)) - Float64(0.5 / Float64(N * N)))); else tmp = fma((log1p(N) ^ 0.6666666666666666), cbrt(log1p(N)), Float64(-log(N))); end return tmp end
code[N_] := If[LessEqual[N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision], 5e-7], N[(N[(1.0 / N), $MachinePrecision] + N[(N[(0.3333333333333333 / N[Power[N, 3.0], $MachinePrecision]), $MachinePrecision] - N[(0.5 / N[(N * N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[N[Log[1 + N], $MachinePrecision], 0.6666666666666666], $MachinePrecision] * N[Power[N[Log[1 + N], $MachinePrecision], 1/3], $MachinePrecision] + (-N[Log[N], $MachinePrecision])), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\log \left(N + 1\right) - \log N \leq 5 \cdot 10^{-7}:\\
\;\;\;\;\frac{1}{N} + \left(\frac{0.3333333333333333}{{N}^{3}} - \frac{0.5}{N \cdot N}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left({\left(\mathsf{log1p}\left(N\right)\right)}^{0.6666666666666666}, \sqrt[3]{\mathsf{log1p}\left(N\right)}, -\log N\right)\\
\end{array}
\end{array}
if (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N)) < 4.99999999999999977e-7Initial program 7.0%
Taylor expanded in N around inf 100.0%
associate--l+100.0%
associate-*r/100.0%
metadata-eval100.0%
associate-*r/100.0%
metadata-eval100.0%
unpow2100.0%
Simplified100.0%
if 4.99999999999999977e-7 < (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N)) Initial program 99.9%
add-cube-cbrt99.9%
fma-neg99.9%
cbrt-unprod99.9%
pow299.9%
+-commutative99.9%
log1p-udef99.9%
+-commutative99.9%
log1p-udef99.9%
Applied egg-rr99.9%
pow1/399.9%
pow-pow99.9%
metadata-eval99.9%
Applied egg-rr99.9%
Final simplification100.0%
(FPCore (N) :precision binary64 (if (<= (- (log (+ N 1.0)) (log N)) 5e-7) (+ (/ 1.0 N) (- (/ 0.3333333333333333 (pow N 3.0)) (/ 0.5 (* N N)))) (log (+ 1.0 (/ 1.0 N)))))
double code(double N) {
double tmp;
if ((log((N + 1.0)) - log(N)) <= 5e-7) {
tmp = (1.0 / N) + ((0.3333333333333333 / pow(N, 3.0)) - (0.5 / (N * N)));
} else {
tmp = log((1.0 + (1.0 / N)));
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if ((log((n + 1.0d0)) - log(n)) <= 5d-7) then
tmp = (1.0d0 / n) + ((0.3333333333333333d0 / (n ** 3.0d0)) - (0.5d0 / (n * n)))
else
tmp = log((1.0d0 + (1.0d0 / n)))
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if ((Math.log((N + 1.0)) - Math.log(N)) <= 5e-7) {
tmp = (1.0 / N) + ((0.3333333333333333 / Math.pow(N, 3.0)) - (0.5 / (N * N)));
} else {
tmp = Math.log((1.0 + (1.0 / N)));
}
return tmp;
}
def code(N): tmp = 0 if (math.log((N + 1.0)) - math.log(N)) <= 5e-7: tmp = (1.0 / N) + ((0.3333333333333333 / math.pow(N, 3.0)) - (0.5 / (N * N))) else: tmp = math.log((1.0 + (1.0 / N))) return tmp
function code(N) tmp = 0.0 if (Float64(log(Float64(N + 1.0)) - log(N)) <= 5e-7) tmp = Float64(Float64(1.0 / N) + Float64(Float64(0.3333333333333333 / (N ^ 3.0)) - Float64(0.5 / Float64(N * N)))); else tmp = log(Float64(1.0 + Float64(1.0 / N))); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if ((log((N + 1.0)) - log(N)) <= 5e-7) tmp = (1.0 / N) + ((0.3333333333333333 / (N ^ 3.0)) - (0.5 / (N * N))); else tmp = log((1.0 + (1.0 / N))); end tmp_2 = tmp; end
code[N_] := If[LessEqual[N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision], 5e-7], N[(N[(1.0 / N), $MachinePrecision] + N[(N[(0.3333333333333333 / N[Power[N, 3.0], $MachinePrecision]), $MachinePrecision] - N[(0.5 / N[(N * N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[N[(1.0 + N[(1.0 / N), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\log \left(N + 1\right) - \log N \leq 5 \cdot 10^{-7}:\\
\;\;\;\;\frac{1}{N} + \left(\frac{0.3333333333333333}{{N}^{3}} - \frac{0.5}{N \cdot N}\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(1 + \frac{1}{N}\right)\\
\end{array}
\end{array}
if (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N)) < 4.99999999999999977e-7Initial program 7.0%
Taylor expanded in N around inf 100.0%
associate--l+100.0%
associate-*r/100.0%
metadata-eval100.0%
associate-*r/100.0%
metadata-eval100.0%
unpow2100.0%
Simplified100.0%
if 4.99999999999999977e-7 < (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N)) Initial program 99.9%
diff-log99.9%
Applied egg-rr99.9%
Taylor expanded in N around 0 99.9%
Final simplification100.0%
(FPCore (N) :precision binary64 (if (<= N 195000.0) (log (+ 1.0 (/ 1.0 N))) (- (/ 1.0 N) (/ 0.5 (* N N)))))
double code(double N) {
double tmp;
if (N <= 195000.0) {
tmp = log((1.0 + (1.0 / N)));
} else {
tmp = (1.0 / N) - (0.5 / (N * N));
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if (n <= 195000.0d0) then
tmp = log((1.0d0 + (1.0d0 / n)))
else
tmp = (1.0d0 / n) - (0.5d0 / (n * n))
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if (N <= 195000.0) {
tmp = Math.log((1.0 + (1.0 / N)));
} else {
tmp = (1.0 / N) - (0.5 / (N * N));
}
return tmp;
}
def code(N): tmp = 0 if N <= 195000.0: tmp = math.log((1.0 + (1.0 / N))) else: tmp = (1.0 / N) - (0.5 / (N * N)) return tmp
function code(N) tmp = 0.0 if (N <= 195000.0) tmp = log(Float64(1.0 + Float64(1.0 / N))); else tmp = Float64(Float64(1.0 / N) - Float64(0.5 / Float64(N * N))); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if (N <= 195000.0) tmp = log((1.0 + (1.0 / N))); else tmp = (1.0 / N) - (0.5 / (N * N)); end tmp_2 = tmp; end
code[N_] := If[LessEqual[N, 195000.0], N[Log[N[(1.0 + N[(1.0 / N), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(1.0 / N), $MachinePrecision] - N[(0.5 / N[(N * N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;N \leq 195000:\\
\;\;\;\;\log \left(1 + \frac{1}{N}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{N} - \frac{0.5}{N \cdot N}\\
\end{array}
\end{array}
if N < 195000Initial program 99.9%
diff-log99.9%
Applied egg-rr99.9%
Taylor expanded in N around 0 99.9%
if 195000 < N Initial program 7.0%
Taylor expanded in N around inf 99.9%
associate-*r/99.9%
metadata-eval99.9%
unpow299.9%
Simplified99.9%
Final simplification99.9%
(FPCore (N) :precision binary64 (if (<= N 0.9) (- N (log N)) (/ (- 1.0 (/ 1.0 (* N 2.0))) N)))
double code(double N) {
double tmp;
if (N <= 0.9) {
tmp = N - log(N);
} else {
tmp = (1.0 - (1.0 / (N * 2.0))) / N;
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if (n <= 0.9d0) then
tmp = n - log(n)
else
tmp = (1.0d0 - (1.0d0 / (n * 2.0d0))) / n
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if (N <= 0.9) {
tmp = N - Math.log(N);
} else {
tmp = (1.0 - (1.0 / (N * 2.0))) / N;
}
return tmp;
}
def code(N): tmp = 0 if N <= 0.9: tmp = N - math.log(N) else: tmp = (1.0 - (1.0 / (N * 2.0))) / N return tmp
function code(N) tmp = 0.0 if (N <= 0.9) tmp = Float64(N - log(N)); else tmp = Float64(Float64(1.0 - Float64(1.0 / Float64(N * 2.0))) / N); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if (N <= 0.9) tmp = N - log(N); else tmp = (1.0 - (1.0 / (N * 2.0))) / N; end tmp_2 = tmp; end
code[N_] := If[LessEqual[N, 0.9], N[(N - N[Log[N], $MachinePrecision]), $MachinePrecision], N[(N[(1.0 - N[(1.0 / N[(N * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;N \leq 0.9:\\
\;\;\;\;N - \log N\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \frac{1}{N \cdot 2}}{N}\\
\end{array}
\end{array}
if N < 0.900000000000000022Initial program 100.0%
Taylor expanded in N around 0 99.5%
neg-mul-199.5%
unsub-neg99.5%
Simplified99.5%
if 0.900000000000000022 < N Initial program 8.3%
Taylor expanded in N around inf 98.9%
associate-*r/98.9%
metadata-eval98.9%
unpow298.9%
Simplified98.9%
clear-num98.9%
frac-sub33.6%
*-un-lft-identity33.6%
div-inv33.6%
metadata-eval33.6%
*-rgt-identity33.6%
div-inv33.6%
metadata-eval33.6%
Applied egg-rr33.6%
div-inv33.6%
associate-*l*33.6%
associate-*r*33.6%
cube-mult33.5%
Applied egg-rr33.5%
associate-*r/33.5%
*-rgt-identity33.5%
*-commutative33.5%
associate-/r*33.5%
unpow333.6%
associate-/r*49.3%
associate-/r*49.3%
*-commutative49.3%
associate-*r*49.3%
associate-/r*50.1%
div-sub50.1%
*-inverses50.1%
div-sub50.1%
associate-/r*49.3%
*-inverses98.9%
Simplified98.9%
Final simplification99.2%
(FPCore (N) :precision binary64 (if (<= N 0.66) (- (log N)) (/ (- 1.0 (/ 1.0 (* N 2.0))) N)))
double code(double N) {
double tmp;
if (N <= 0.66) {
tmp = -log(N);
} else {
tmp = (1.0 - (1.0 / (N * 2.0))) / N;
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if (n <= 0.66d0) then
tmp = -log(n)
else
tmp = (1.0d0 - (1.0d0 / (n * 2.0d0))) / n
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if (N <= 0.66) {
tmp = -Math.log(N);
} else {
tmp = (1.0 - (1.0 / (N * 2.0))) / N;
}
return tmp;
}
def code(N): tmp = 0 if N <= 0.66: tmp = -math.log(N) else: tmp = (1.0 - (1.0 / (N * 2.0))) / N return tmp
function code(N) tmp = 0.0 if (N <= 0.66) tmp = Float64(-log(N)); else tmp = Float64(Float64(1.0 - Float64(1.0 / Float64(N * 2.0))) / N); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if (N <= 0.66) tmp = -log(N); else tmp = (1.0 - (1.0 / (N * 2.0))) / N; end tmp_2 = tmp; end
code[N_] := If[LessEqual[N, 0.66], (-N[Log[N], $MachinePrecision]), N[(N[(1.0 - N[(1.0 / N[(N * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;N \leq 0.66:\\
\;\;\;\;-\log N\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \frac{1}{N \cdot 2}}{N}\\
\end{array}
\end{array}
if N < 0.660000000000000031Initial program 100.0%
Taylor expanded in N around 0 98.9%
neg-mul-198.9%
Simplified98.9%
if 0.660000000000000031 < N Initial program 8.3%
Taylor expanded in N around inf 98.9%
associate-*r/98.9%
metadata-eval98.9%
unpow298.9%
Simplified98.9%
clear-num98.9%
frac-sub33.6%
*-un-lft-identity33.6%
div-inv33.6%
metadata-eval33.6%
*-rgt-identity33.6%
div-inv33.6%
metadata-eval33.6%
Applied egg-rr33.6%
div-inv33.6%
associate-*l*33.6%
associate-*r*33.6%
cube-mult33.5%
Applied egg-rr33.5%
associate-*r/33.5%
*-rgt-identity33.5%
*-commutative33.5%
associate-/r*33.5%
unpow333.6%
associate-/r*49.3%
associate-/r*49.3%
*-commutative49.3%
associate-*r*49.3%
associate-/r*50.1%
div-sub50.1%
*-inverses50.1%
div-sub50.1%
associate-/r*49.3%
*-inverses98.9%
Simplified98.9%
Final simplification98.9%
(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 53.8%
Taylor expanded in N around inf 52.0%
Final simplification52.0%
herbie shell --seed 2023200
(FPCore (N)
:name "2log (problem 3.3.6)"
:precision binary64
(- (log (+ N 1.0)) (log N)))