
(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 11 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)) 0.0002) (/ (+ 1.0 (/ (+ -0.5 (/ (+ 0.3333333333333333 (/ -0.25 N)) N)) N)) N) (- (log (/ N (+ N 1.0))))))
double code(double N) {
double tmp;
if ((log((N + 1.0)) - log(N)) <= 0.0002) {
tmp = (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N;
} else {
tmp = -log((N / (N + 1.0)));
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if ((log((n + 1.0d0)) - log(n)) <= 0.0002d0) then
tmp = (1.0d0 + (((-0.5d0) + ((0.3333333333333333d0 + ((-0.25d0) / n)) / n)) / n)) / n
else
tmp = -log((n / (n + 1.0d0)))
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if ((Math.log((N + 1.0)) - Math.log(N)) <= 0.0002) {
tmp = (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N;
} else {
tmp = -Math.log((N / (N + 1.0)));
}
return tmp;
}
def code(N): tmp = 0 if (math.log((N + 1.0)) - math.log(N)) <= 0.0002: tmp = (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N else: tmp = -math.log((N / (N + 1.0))) return tmp
function code(N) tmp = 0.0 if (Float64(log(Float64(N + 1.0)) - log(N)) <= 0.0002) tmp = Float64(Float64(1.0 + Float64(Float64(-0.5 + Float64(Float64(0.3333333333333333 + Float64(-0.25 / N)) / N)) / N)) / N); else tmp = Float64(-log(Float64(N / Float64(N + 1.0)))); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if ((log((N + 1.0)) - log(N)) <= 0.0002) tmp = (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N; else tmp = -log((N / (N + 1.0))); end tmp_2 = tmp; end
code[N_] := If[LessEqual[N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision], 0.0002], N[(N[(1.0 + N[(N[(-0.5 + N[(N[(0.3333333333333333 + N[(-0.25 / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision], (-N[Log[N[(N / N[(N + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\log \left(N + 1\right) - \log N \leq 0.0002:\\
\;\;\;\;\frac{1 + \frac{-0.5 + \frac{0.3333333333333333 + \frac{-0.25}{N}}{N}}{N}}{N}\\
\mathbf{else}:\\
\;\;\;\;-\log \left(\frac{N}{N + 1}\right)\\
\end{array}
\end{array}
if (-.f64 (log.f64 (+.f64 N #s(literal 1 binary64))) (log.f64 N)) < 2.0000000000000001e-4Initial program 15.1%
+-commutative15.1%
log1p-define15.1%
Simplified15.1%
Taylor expanded in N around -inf 100.0%
associate-*r/100.0%
Simplified100.0%
if 2.0000000000000001e-4 < (-.f64 (log.f64 (+.f64 N #s(literal 1 binary64))) (log.f64 N)) Initial program 90.5%
+-commutative90.5%
log1p-define90.5%
Simplified90.5%
add-sqr-sqrt90.6%
pow290.6%
Applied egg-rr90.6%
unpow290.6%
add-sqr-sqrt90.5%
log1p-undefine90.5%
+-commutative90.5%
diff-log93.3%
clear-num93.2%
log-div94.4%
metadata-eval94.4%
Applied egg-rr94.4%
neg-sub094.4%
Simplified94.4%
(FPCore (N) :precision binary64 (if (<= N 1700.0) (log (+ 1.0 (/ 1.0 N))) (/ (+ 1.0 (/ (+ -0.5 (/ (+ 0.3333333333333333 (/ -0.25 N)) N)) N)) N)))
double code(double N) {
double tmp;
if (N <= 1700.0) {
tmp = log((1.0 + (1.0 / N)));
} else {
tmp = (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N;
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if (n <= 1700.0d0) then
tmp = log((1.0d0 + (1.0d0 / n)))
else
tmp = (1.0d0 + (((-0.5d0) + ((0.3333333333333333d0 + ((-0.25d0) / n)) / n)) / n)) / n
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if (N <= 1700.0) {
tmp = Math.log((1.0 + (1.0 / N)));
} else {
tmp = (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N;
}
return tmp;
}
def code(N): tmp = 0 if N <= 1700.0: tmp = math.log((1.0 + (1.0 / N))) else: tmp = (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N return tmp
function code(N) tmp = 0.0 if (N <= 1700.0) tmp = log(Float64(1.0 + Float64(1.0 / N))); else tmp = Float64(Float64(1.0 + Float64(Float64(-0.5 + Float64(Float64(0.3333333333333333 + Float64(-0.25 / N)) / N)) / N)) / N); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if (N <= 1700.0) tmp = log((1.0 + (1.0 / N))); else tmp = (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N; end tmp_2 = tmp; end
code[N_] := If[LessEqual[N, 1700.0], N[Log[N[(1.0 + N[(1.0 / N), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(1.0 + N[(N[(-0.5 + N[(N[(0.3333333333333333 + N[(-0.25 / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;N \leq 1700:\\
\;\;\;\;\log \left(1 + \frac{1}{N}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1 + \frac{-0.5 + \frac{0.3333333333333333 + \frac{-0.25}{N}}{N}}{N}}{N}\\
\end{array}
\end{array}
if N < 1700Initial program 90.5%
+-commutative90.5%
log1p-define90.5%
Simplified90.5%
add-sqr-sqrt90.6%
pow290.6%
Applied egg-rr90.6%
unpow290.6%
add-sqr-sqrt90.5%
log1p-undefine90.5%
+-commutative90.5%
diff-log93.3%
Applied egg-rr93.3%
*-lft-identity93.3%
associate-*l/93.3%
distribute-rgt-in93.2%
rgt-mult-inverse93.4%
*-lft-identity93.4%
Simplified93.4%
if 1700 < N Initial program 15.1%
+-commutative15.1%
log1p-define15.1%
Simplified15.1%
Taylor expanded in N around -inf 100.0%
associate-*r/100.0%
Simplified100.0%
(FPCore (N) :precision binary64 (/ 1.0 (/ N (+ 1.0 (/ (+ -0.5 (/ (+ 0.3333333333333333 (/ -0.25 N)) N)) N)))))
double code(double N) {
return 1.0 / (N / (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)));
}
real(8) function code(n)
real(8), intent (in) :: n
code = 1.0d0 / (n / (1.0d0 + (((-0.5d0) + ((0.3333333333333333d0 + ((-0.25d0) / n)) / n)) / n)))
end function
public static double code(double N) {
return 1.0 / (N / (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)));
}
def code(N): return 1.0 / (N / (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)))
function code(N) return Float64(1.0 / Float64(N / Float64(1.0 + Float64(Float64(-0.5 + Float64(Float64(0.3333333333333333 + Float64(-0.25 / N)) / N)) / N)))) end
function tmp = code(N) tmp = 1.0 / (N / (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N))); end
code[N_] := N[(1.0 / N[(N / N[(1.0 + N[(N[(-0.5 + N[(N[(0.3333333333333333 + N[(-0.25 / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{N}{1 + \frac{-0.5 + \frac{0.3333333333333333 + \frac{-0.25}{N}}{N}}{N}}}
\end{array}
Initial program 23.7%
+-commutative23.7%
log1p-define23.7%
Simplified23.7%
Taylor expanded in N around -inf 95.3%
associate-*r/95.3%
Simplified95.3%
clear-num95.3%
inv-pow95.3%
+-commutative95.3%
Applied egg-rr95.3%
unpow-195.3%
+-commutative95.3%
Simplified95.3%
(FPCore (N) :precision binary64 (/ (+ 1.0 (/ (+ -0.5 (/ (+ 0.3333333333333333 (/ -0.25 N)) N)) N)) N))
double code(double N) {
return (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N;
}
real(8) function code(n)
real(8), intent (in) :: n
code = (1.0d0 + (((-0.5d0) + ((0.3333333333333333d0 + ((-0.25d0) / n)) / n)) / n)) / n
end function
public static double code(double N) {
return (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N;
}
def code(N): return (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N
function code(N) return Float64(Float64(1.0 + Float64(Float64(-0.5 + Float64(Float64(0.3333333333333333 + Float64(-0.25 / N)) / N)) / N)) / N) end
function tmp = code(N) tmp = (1.0 + ((-0.5 + ((0.3333333333333333 + (-0.25 / N)) / N)) / N)) / N; end
code[N_] := N[(N[(1.0 + N[(N[(-0.5 + N[(N[(0.3333333333333333 + N[(-0.25 / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]
\begin{array}{l}
\\
\frac{1 + \frac{-0.5 + \frac{0.3333333333333333 + \frac{-0.25}{N}}{N}}{N}}{N}
\end{array}
Initial program 23.7%
+-commutative23.7%
log1p-define23.7%
Simplified23.7%
Taylor expanded in N around -inf 95.3%
associate-*r/95.3%
Simplified95.3%
(FPCore (N) :precision binary64 (/ 1.0 (/ N (+ 1.0 (/ (+ -0.5 (/ 0.3333333333333333 N)) N)))))
double code(double N) {
return 1.0 / (N / (1.0 + ((-0.5 + (0.3333333333333333 / N)) / N)));
}
real(8) function code(n)
real(8), intent (in) :: n
code = 1.0d0 / (n / (1.0d0 + (((-0.5d0) + (0.3333333333333333d0 / n)) / n)))
end function
public static double code(double N) {
return 1.0 / (N / (1.0 + ((-0.5 + (0.3333333333333333 / N)) / N)));
}
def code(N): return 1.0 / (N / (1.0 + ((-0.5 + (0.3333333333333333 / N)) / N)))
function code(N) return Float64(1.0 / Float64(N / Float64(1.0 + Float64(Float64(-0.5 + Float64(0.3333333333333333 / N)) / N)))) end
function tmp = code(N) tmp = 1.0 / (N / (1.0 + ((-0.5 + (0.3333333333333333 / N)) / N))); end
code[N_] := N[(1.0 / N[(N / N[(1.0 + N[(N[(-0.5 + N[(0.3333333333333333 / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{N}{1 + \frac{-0.5 + \frac{0.3333333333333333}{N}}{N}}}
\end{array}
Initial program 23.7%
+-commutative23.7%
log1p-define23.7%
Simplified23.7%
Taylor expanded in N around -inf 95.3%
associate-*r/95.3%
Simplified95.3%
clear-num95.3%
inv-pow95.3%
+-commutative95.3%
Applied egg-rr95.3%
unpow-195.3%
+-commutative95.3%
Simplified95.3%
Taylor expanded in N around inf 93.9%
sub-neg93.9%
associate-*r/93.9%
metadata-eval93.9%
metadata-eval93.9%
Simplified93.9%
Final simplification93.9%
(FPCore (N) :precision binary64 (/ (+ 1.0 (/ (+ -0.5 (/ 0.3333333333333333 N)) N)) N))
double code(double N) {
return (1.0 + ((-0.5 + (0.3333333333333333 / N)) / N)) / N;
}
real(8) function code(n)
real(8), intent (in) :: n
code = (1.0d0 + (((-0.5d0) + (0.3333333333333333d0 / n)) / n)) / n
end function
public static double code(double N) {
return (1.0 + ((-0.5 + (0.3333333333333333 / N)) / N)) / N;
}
def code(N): return (1.0 + ((-0.5 + (0.3333333333333333 / N)) / N)) / N
function code(N) return Float64(Float64(1.0 + Float64(Float64(-0.5 + Float64(0.3333333333333333 / N)) / N)) / N) end
function tmp = code(N) tmp = (1.0 + ((-0.5 + (0.3333333333333333 / N)) / N)) / N; end
code[N_] := N[(N[(1.0 + N[(N[(-0.5 + N[(0.3333333333333333 / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]
\begin{array}{l}
\\
\frac{1 + \frac{-0.5 + \frac{0.3333333333333333}{N}}{N}}{N}
\end{array}
Initial program 23.7%
+-commutative23.7%
log1p-define23.7%
Simplified23.7%
Taylor expanded in N around inf 93.9%
sub-neg93.9%
associate-+l+93.9%
sub-neg93.9%
unpow293.9%
associate-/r*93.9%
metadata-eval93.9%
associate-*r/93.9%
associate-*r/93.9%
metadata-eval93.9%
div-sub93.9%
sub-neg93.9%
metadata-eval93.9%
+-commutative93.9%
associate-*r/93.9%
metadata-eval93.9%
Simplified93.9%
(FPCore (N) :precision binary64 (/ 1.0 (/ N (+ 1.0 (/ -0.5 N)))))
double code(double N) {
return 1.0 / (N / (1.0 + (-0.5 / N)));
}
real(8) function code(n)
real(8), intent (in) :: n
code = 1.0d0 / (n / (1.0d0 + ((-0.5d0) / n)))
end function
public static double code(double N) {
return 1.0 / (N / (1.0 + (-0.5 / N)));
}
def code(N): return 1.0 / (N / (1.0 + (-0.5 / N)))
function code(N) return Float64(1.0 / Float64(N / Float64(1.0 + Float64(-0.5 / N)))) end
function tmp = code(N) tmp = 1.0 / (N / (1.0 + (-0.5 / N))); end
code[N_] := N[(1.0 / N[(N / N[(1.0 + N[(-0.5 / N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{N}{1 + \frac{-0.5}{N}}}
\end{array}
Initial program 23.7%
+-commutative23.7%
log1p-define23.7%
Simplified23.7%
Taylor expanded in N around inf 91.4%
associate-*r/91.4%
metadata-eval91.4%
Simplified91.4%
clear-num91.4%
inv-pow91.4%
sub-neg91.4%
distribute-neg-frac91.4%
metadata-eval91.4%
Applied egg-rr91.4%
unpow-191.4%
Simplified91.4%
(FPCore (N) :precision binary64 (- (/ 1.0 N) (/ (/ 0.5 N) N)))
double code(double N) {
return (1.0 / N) - ((0.5 / N) / N);
}
real(8) function code(n)
real(8), intent (in) :: n
code = (1.0d0 / n) - ((0.5d0 / n) / n)
end function
public static double code(double N) {
return (1.0 / N) - ((0.5 / N) / N);
}
def code(N): return (1.0 / N) - ((0.5 / N) / N)
function code(N) return Float64(Float64(1.0 / N) - Float64(Float64(0.5 / N) / N)) end
function tmp = code(N) tmp = (1.0 / N) - ((0.5 / N) / N); end
code[N_] := N[(N[(1.0 / N), $MachinePrecision] - N[(N[(0.5 / N), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{N} - \frac{\frac{0.5}{N}}{N}
\end{array}
Initial program 23.7%
+-commutative23.7%
log1p-define23.7%
Simplified23.7%
Taylor expanded in N around inf 91.4%
associate-*r/91.4%
metadata-eval91.4%
Simplified91.4%
div-sub91.4%
Applied egg-rr91.4%
(FPCore (N) :precision binary64 (/ (- 1.0 (/ 0.5 N)) N))
double code(double N) {
return (1.0 - (0.5 / N)) / N;
}
real(8) function code(n)
real(8), intent (in) :: n
code = (1.0d0 - (0.5d0 / n)) / n
end function
public static double code(double N) {
return (1.0 - (0.5 / N)) / N;
}
def code(N): return (1.0 - (0.5 / N)) / N
function code(N) return Float64(Float64(1.0 - Float64(0.5 / N)) / N) end
function tmp = code(N) tmp = (1.0 - (0.5 / N)) / N; end
code[N_] := N[(N[(1.0 - N[(0.5 / N), $MachinePrecision]), $MachinePrecision] / N), $MachinePrecision]
\begin{array}{l}
\\
\frac{1 - \frac{0.5}{N}}{N}
\end{array}
Initial program 23.7%
+-commutative23.7%
log1p-define23.7%
Simplified23.7%
Taylor expanded in N around inf 91.4%
associate-*r/91.4%
metadata-eval91.4%
Simplified91.4%
(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 23.7%
+-commutative23.7%
log1p-define23.7%
Simplified23.7%
Taylor expanded in N around inf 84.6%
(FPCore (N) :precision binary64 0.0)
double code(double N) {
return 0.0;
}
real(8) function code(n)
real(8), intent (in) :: n
code = 0.0d0
end function
public static double code(double N) {
return 0.0;
}
def code(N): return 0.0
function code(N) return 0.0 end
function tmp = code(N) tmp = 0.0; end
code[N_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 23.7%
+-commutative23.7%
log1p-define23.7%
Simplified23.7%
sub-neg23.7%
+-commutative23.7%
add-sqr-sqrt24.0%
distribute-rgt-neg-in24.0%
fma-define25.1%
Applied egg-rr25.1%
Taylor expanded in N around inf 3.3%
distribute-rgt1-in3.3%
metadata-eval3.3%
mul0-lft3.3%
Simplified3.3%
(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}
herbie shell --seed 2024107
(FPCore (N)
:name "2log (problem 3.3.6)"
:precision binary64
:pre (and (> N 1.0) (< N 1e+40))
:alt
(log1p (/ 1.0 N))
(- (log (+ N 1.0)) (log N)))