| Alternative 1 | |
|---|---|
| Error | 0.11% |
| Cost | 6852 |
\[\begin{array}{l}
\mathbf{if}\;N \leq 14500:\\
\;\;\;\;\log \left(\frac{N + 1}{N}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{N} + \frac{\frac{0.3333333333333333}{N} + -0.5}{N \cdot N}\\
\end{array}
\]
(FPCore (N) :precision binary64 (- (log (+ N 1.0)) (log N)))
(FPCore (N)
:precision binary64
(if (<= (- (log (+ N 1.0)) (log N)) 0.0001)
(+
(* (/ 1.0 N) (/ 0.3333333333333333 (* N N)))
(+ (/ 1.0 N) (+ (/ -0.5 (* N N)) (/ -0.25 (pow N 4.0)))))
(log (/ (+ N 1.0) N))))double code(double N) {
return log((N + 1.0)) - log(N);
}
double code(double N) {
double tmp;
if ((log((N + 1.0)) - log(N)) <= 0.0001) {
tmp = ((1.0 / N) * (0.3333333333333333 / (N * N))) + ((1.0 / N) + ((-0.5 / (N * N)) + (-0.25 / pow(N, 4.0))));
} else {
tmp = log(((N + 1.0) / N));
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
code = log((n + 1.0d0)) - log(n)
end function
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if ((log((n + 1.0d0)) - log(n)) <= 0.0001d0) then
tmp = ((1.0d0 / n) * (0.3333333333333333d0 / (n * n))) + ((1.0d0 / n) + (((-0.5d0) / (n * n)) + ((-0.25d0) / (n ** 4.0d0))))
else
tmp = log(((n + 1.0d0) / n))
end if
code = tmp
end function
public static double code(double N) {
return Math.log((N + 1.0)) - Math.log(N);
}
public static double code(double N) {
double tmp;
if ((Math.log((N + 1.0)) - Math.log(N)) <= 0.0001) {
tmp = ((1.0 / N) * (0.3333333333333333 / (N * N))) + ((1.0 / N) + ((-0.5 / (N * N)) + (-0.25 / Math.pow(N, 4.0))));
} else {
tmp = Math.log(((N + 1.0) / N));
}
return tmp;
}
def code(N): return math.log((N + 1.0)) - math.log(N)
def code(N): tmp = 0 if (math.log((N + 1.0)) - math.log(N)) <= 0.0001: tmp = ((1.0 / N) * (0.3333333333333333 / (N * N))) + ((1.0 / N) + ((-0.5 / (N * N)) + (-0.25 / math.pow(N, 4.0)))) else: tmp = math.log(((N + 1.0) / N)) return tmp
function code(N) return Float64(log(Float64(N + 1.0)) - log(N)) end
function code(N) tmp = 0.0 if (Float64(log(Float64(N + 1.0)) - log(N)) <= 0.0001) tmp = Float64(Float64(Float64(1.0 / N) * Float64(0.3333333333333333 / Float64(N * N))) + Float64(Float64(1.0 / N) + Float64(Float64(-0.5 / Float64(N * N)) + Float64(-0.25 / (N ^ 4.0))))); else tmp = log(Float64(Float64(N + 1.0) / N)); end return tmp end
function tmp = code(N) tmp = log((N + 1.0)) - log(N); end
function tmp_2 = code(N) tmp = 0.0; if ((log((N + 1.0)) - log(N)) <= 0.0001) tmp = ((1.0 / N) * (0.3333333333333333 / (N * N))) + ((1.0 / N) + ((-0.5 / (N * N)) + (-0.25 / (N ^ 4.0)))); else tmp = log(((N + 1.0) / N)); end tmp_2 = tmp; end
code[N_] := N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision]
code[N_] := If[LessEqual[N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision], 0.0001], N[(N[(N[(1.0 / N), $MachinePrecision] * N[(0.3333333333333333 / N[(N * N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(1.0 / N), $MachinePrecision] + N[(N[(-0.5 / N[(N * N), $MachinePrecision]), $MachinePrecision] + N[(-0.25 / N[Power[N, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[N[(N[(N + 1.0), $MachinePrecision] / N), $MachinePrecision]], $MachinePrecision]]
\log \left(N + 1\right) - \log N
\begin{array}{l}
\mathbf{if}\;\log \left(N + 1\right) - \log N \leq 0.0001:\\
\;\;\;\;\frac{1}{N} \cdot \frac{0.3333333333333333}{N \cdot N} + \left(\frac{1}{N} + \left(\frac{-0.5}{N \cdot N} + \frac{-0.25}{{N}^{4}}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(\frac{N + 1}{N}\right)\\
\end{array}
Results
if (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N)) < 1.00000000000000005e-4Initial program 92.99
Simplified92.99
[Start]92.99 | \[ \log \left(N + 1\right) - \log N
\] |
|---|---|
+-commutative [=>]92.99 | \[ \log \color{blue}{\left(1 + N\right)} - \log N
\] |
log1p-def [=>]92.99 | \[ \color{blue}{\mathsf{log1p}\left(N\right)} - \log N
\] |
Taylor expanded in N around inf 0.02
Simplified0.02
[Start]0.02 | \[ \left(\frac{1}{N} + 0.3333333333333333 \cdot \frac{1}{{N}^{3}}\right) - \left(0.25 \cdot \frac{1}{{N}^{4}} + 0.5 \cdot \frac{1}{{N}^{2}}\right)
\] |
|---|---|
+-commutative [=>]0.02 | \[ \color{blue}{\left(0.3333333333333333 \cdot \frac{1}{{N}^{3}} + \frac{1}{N}\right)} - \left(0.25 \cdot \frac{1}{{N}^{4}} + 0.5 \cdot \frac{1}{{N}^{2}}\right)
\] |
associate--l+ [=>]0.02 | \[ \color{blue}{0.3333333333333333 \cdot \frac{1}{{N}^{3}} + \left(\frac{1}{N} - \left(0.25 \cdot \frac{1}{{N}^{4}} + 0.5 \cdot \frac{1}{{N}^{2}}\right)\right)}
\] |
associate-*r/ [=>]0.02 | \[ \color{blue}{\frac{0.3333333333333333 \cdot 1}{{N}^{3}}} + \left(\frac{1}{N} - \left(0.25 \cdot \frac{1}{{N}^{4}} + 0.5 \cdot \frac{1}{{N}^{2}}\right)\right)
\] |
metadata-eval [=>]0.02 | \[ \frac{\color{blue}{0.3333333333333333}}{{N}^{3}} + \left(\frac{1}{N} - \left(0.25 \cdot \frac{1}{{N}^{4}} + 0.5 \cdot \frac{1}{{N}^{2}}\right)\right)
\] |
+-commutative [=>]0.02 | \[ \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1}{N} - \color{blue}{\left(0.5 \cdot \frac{1}{{N}^{2}} + 0.25 \cdot \frac{1}{{N}^{4}}\right)}\right)
\] |
associate-*r/ [=>]0.02 | \[ \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1}{N} - \left(\color{blue}{\frac{0.5 \cdot 1}{{N}^{2}}} + 0.25 \cdot \frac{1}{{N}^{4}}\right)\right)
\] |
metadata-eval [=>]0.02 | \[ \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1}{N} - \left(\frac{\color{blue}{0.5}}{{N}^{2}} + 0.25 \cdot \frac{1}{{N}^{4}}\right)\right)
\] |
unpow2 [=>]0.02 | \[ \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1}{N} - \left(\frac{0.5}{\color{blue}{N \cdot N}} + 0.25 \cdot \frac{1}{{N}^{4}}\right)\right)
\] |
associate-*r/ [=>]0.02 | \[ \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1}{N} - \left(\frac{0.5}{N \cdot N} + \color{blue}{\frac{0.25 \cdot 1}{{N}^{4}}}\right)\right)
\] |
metadata-eval [=>]0.02 | \[ \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1}{N} - \left(\frac{0.5}{N \cdot N} + \frac{\color{blue}{0.25}}{{N}^{4}}\right)\right)
\] |
Applied egg-rr0.02
if 1.00000000000000005e-4 < (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N)) Initial program 0.15
Simplified0.15
[Start]0.15 | \[ \log \left(N + 1\right) - \log N
\] |
|---|---|
+-commutative [=>]0.15 | \[ \log \color{blue}{\left(1 + N\right)} - \log N
\] |
log1p-def [=>]0.15 | \[ \color{blue}{\mathsf{log1p}\left(N\right)} - \log N
\] |
Applied egg-rr0.16
Final simplification0.09
| Alternative 1 | |
|---|---|
| Error | 0.11% |
| Cost | 6852 |
| Alternative 2 | |
|---|---|
| Error | 0.75% |
| Cost | 6724 |
| Alternative 3 | |
|---|---|
| Error | 1.22% |
| Cost | 6660 |
| Alternative 4 | |
|---|---|
| Error | 44.14% |
| Cost | 324 |
| Alternative 5 | |
|---|---|
| Error | 43.59% |
| Cost | 320 |
| Alternative 6 | |
|---|---|
| Error | 90.05% |
| Cost | 64 |
herbie shell --seed 2023089
(FPCore (N)
:name "2log (problem 3.3.6)"
:precision binary64
(- (log (+ N 1.0)) (log N)))