
(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 8 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)
(+
(/ 0.3333333333333333 (pow N 3.0))
(- (+ (/ 1.0 N) (/ -0.5 (pow N 2.0))) (/ 0.25 (pow N 4.0))))
(- (log (/ N (+ N 1.0))))))
double code(double N) {
double tmp;
if ((log((N + 1.0)) - log(N)) <= 0.0002) {
tmp = (0.3333333333333333 / pow(N, 3.0)) + (((1.0 / N) + (-0.5 / pow(N, 2.0))) - (0.25 / pow(N, 4.0)));
} 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 = (0.3333333333333333d0 / (n ** 3.0d0)) + (((1.0d0 / n) + ((-0.5d0) / (n ** 2.0d0))) - (0.25d0 / (n ** 4.0d0)))
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 = (0.3333333333333333 / Math.pow(N, 3.0)) + (((1.0 / N) + (-0.5 / Math.pow(N, 2.0))) - (0.25 / Math.pow(N, 4.0)));
} 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 = (0.3333333333333333 / math.pow(N, 3.0)) + (((1.0 / N) + (-0.5 / math.pow(N, 2.0))) - (0.25 / math.pow(N, 4.0))) 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(0.3333333333333333 / (N ^ 3.0)) + Float64(Float64(Float64(1.0 / N) + Float64(-0.5 / (N ^ 2.0))) - Float64(0.25 / (N ^ 4.0)))); 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 = (0.3333333333333333 / (N ^ 3.0)) + (((1.0 / N) + (-0.5 / (N ^ 2.0))) - (0.25 / (N ^ 4.0))); 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[(0.3333333333333333 / N[Power[N, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(N[(1.0 / N), $MachinePrecision] + N[(-0.5 / N[Power[N, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(0.25 / N[Power[N, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $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{0.3333333333333333}{{N}^{3}} + \left(\left(\frac{1}{N} + \frac{-0.5}{{N}^{2}}\right) - \frac{0.25}{{N}^{4}}\right)\\
\mathbf{else}:\\
\;\;\;\;-\log \left(\frac{N}{N + 1}\right)\\
\end{array}
\end{array}
(FPCore (N) :precision binary64 (if (<= (- (log (+ N 1.0)) (log N)) 1e-5) (/ 1.0 (+ N (- 0.5 (/ 0.08333333333333333 N)))) (* (log (sqrt (/ N (+ N 1.0)))) (- 2.0))))
double code(double N) {
double tmp;
if ((log((N + 1.0)) - log(N)) <= 1e-5) {
tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N)));
} else {
tmp = log(sqrt((N / (N + 1.0)))) * -2.0;
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if ((log((n + 1.0d0)) - log(n)) <= 1d-5) then
tmp = 1.0d0 / (n + (0.5d0 - (0.08333333333333333d0 / n)))
else
tmp = log(sqrt((n / (n + 1.0d0)))) * -2.0d0
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if ((Math.log((N + 1.0)) - Math.log(N)) <= 1e-5) {
tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N)));
} else {
tmp = Math.log(Math.sqrt((N / (N + 1.0)))) * -2.0;
}
return tmp;
}
def code(N): tmp = 0 if (math.log((N + 1.0)) - math.log(N)) <= 1e-5: tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N))) else: tmp = math.log(math.sqrt((N / (N + 1.0)))) * -2.0 return tmp
function code(N) tmp = 0.0 if (Float64(log(Float64(N + 1.0)) - log(N)) <= 1e-5) tmp = Float64(1.0 / Float64(N + Float64(0.5 - Float64(0.08333333333333333 / N)))); else tmp = Float64(log(sqrt(Float64(N / Float64(N + 1.0)))) * Float64(-2.0)); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if ((log((N + 1.0)) - log(N)) <= 1e-5) tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N))); else tmp = log(sqrt((N / (N + 1.0)))) * -2.0; end tmp_2 = tmp; end
code[N_] := If[LessEqual[N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision], 1e-5], N[(1.0 / N[(N + N[(0.5 - N[(0.08333333333333333 / N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Log[N[Sqrt[N[(N / N[(N + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * (-2.0)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\log \left(N + 1\right) - \log N \leq 10^{-5}:\\
\;\;\;\;\frac{1}{N + \left(0.5 - \frac{0.08333333333333333}{N}\right)}\\
\mathbf{else}:\\
\;\;\;\;\log \left(\sqrt{\frac{N}{N + 1}}\right) \cdot \left(-2\right)\\
\end{array}
\end{array}
(FPCore (N) :precision binary64 (if (<= (- (log (+ N 1.0)) (log N)) 1e-5) (/ 1.0 (+ N (- 0.5 (/ 0.08333333333333333 N)))) (- (log (/ N (+ N 1.0))))))
double code(double N) {
double tmp;
if ((log((N + 1.0)) - log(N)) <= 1e-5) {
tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / 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)) <= 1d-5) then
tmp = 1.0d0 / (n + (0.5d0 - (0.08333333333333333d0 / 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)) <= 1e-5) {
tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / 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)) <= 1e-5: tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / 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)) <= 1e-5) tmp = Float64(1.0 / Float64(N + Float64(0.5 - Float64(0.08333333333333333 / 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)) <= 1e-5) tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / 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], 1e-5], N[(1.0 / N[(N + N[(0.5 - N[(0.08333333333333333 / N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $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 10^{-5}:\\
\;\;\;\;\frac{1}{N + \left(0.5 - \frac{0.08333333333333333}{N}\right)}\\
\mathbf{else}:\\
\;\;\;\;-\log \left(\frac{N}{N + 1}\right)\\
\end{array}
\end{array}
(FPCore (N) :precision binary64 (if (<= N 6200.0) (log (/ (+ N 1.0) N)) (/ 1.0 (+ N (- 0.5 (/ 0.08333333333333333 N))))))
double code(double N) {
double tmp;
if (N <= 6200.0) {
tmp = log(((N + 1.0) / N));
} else {
tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N)));
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if (n <= 6200.0d0) then
tmp = log(((n + 1.0d0) / n))
else
tmp = 1.0d0 / (n + (0.5d0 - (0.08333333333333333d0 / n)))
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if (N <= 6200.0) {
tmp = Math.log(((N + 1.0) / N));
} else {
tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N)));
}
return tmp;
}
def code(N): tmp = 0 if N <= 6200.0: tmp = math.log(((N + 1.0) / N)) else: tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N))) return tmp
function code(N) tmp = 0.0 if (N <= 6200.0) tmp = log(Float64(Float64(N + 1.0) / N)); else tmp = Float64(1.0 / Float64(N + Float64(0.5 - Float64(0.08333333333333333 / N)))); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if (N <= 6200.0) tmp = log(((N + 1.0) / N)); else tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N))); end tmp_2 = tmp; end
code[N_] := If[LessEqual[N, 6200.0], N[Log[N[(N[(N + 1.0), $MachinePrecision] / N), $MachinePrecision]], $MachinePrecision], N[(1.0 / N[(N + N[(0.5 - N[(0.08333333333333333 / N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;N \leq 6200:\\
\;\;\;\;\log \left(\frac{N + 1}{N}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{N + \left(0.5 - \frac{0.08333333333333333}{N}\right)}\\
\end{array}
\end{array}
(FPCore (N) :precision binary64 (if (<= N 0.5) (- N (log N)) (/ 1.0 (+ N (- 0.5 (/ 0.08333333333333333 N))))))
double code(double N) {
double tmp;
if (N <= 0.5) {
tmp = N - log(N);
} else {
tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N)));
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if (n <= 0.5d0) then
tmp = n - log(n)
else
tmp = 1.0d0 / (n + (0.5d0 - (0.08333333333333333d0 / n)))
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if (N <= 0.5) {
tmp = N - Math.log(N);
} else {
tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N)));
}
return tmp;
}
def code(N): tmp = 0 if N <= 0.5: tmp = N - math.log(N) else: tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N))) return tmp
function code(N) tmp = 0.0 if (N <= 0.5) tmp = Float64(N - log(N)); else tmp = Float64(1.0 / Float64(N + Float64(0.5 - Float64(0.08333333333333333 / N)))); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if (N <= 0.5) tmp = N - log(N); else tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N))); end tmp_2 = tmp; end
code[N_] := If[LessEqual[N, 0.5], N[(N - N[Log[N], $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N + N[(0.5 - N[(0.08333333333333333 / N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;N \leq 0.5:\\
\;\;\;\;N - \log N\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{N + \left(0.5 - \frac{0.08333333333333333}{N}\right)}\\
\end{array}
\end{array}
(FPCore (N) :precision binary64 (if (<= N 0.35) (- (log N)) (/ 1.0 (+ N (- 0.5 (/ 0.08333333333333333 N))))))
double code(double N) {
double tmp;
if (N <= 0.35) {
tmp = -log(N);
} else {
tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N)));
}
return tmp;
}
real(8) function code(n)
real(8), intent (in) :: n
real(8) :: tmp
if (n <= 0.35d0) then
tmp = -log(n)
else
tmp = 1.0d0 / (n + (0.5d0 - (0.08333333333333333d0 / n)))
end if
code = tmp
end function
public static double code(double N) {
double tmp;
if (N <= 0.35) {
tmp = -Math.log(N);
} else {
tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N)));
}
return tmp;
}
def code(N): tmp = 0 if N <= 0.35: tmp = -math.log(N) else: tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N))) return tmp
function code(N) tmp = 0.0 if (N <= 0.35) tmp = Float64(-log(N)); else tmp = Float64(1.0 / Float64(N + Float64(0.5 - Float64(0.08333333333333333 / N)))); end return tmp end
function tmp_2 = code(N) tmp = 0.0; if (N <= 0.35) tmp = -log(N); else tmp = 1.0 / (N + (0.5 - (0.08333333333333333 / N))); end tmp_2 = tmp; end
code[N_] := If[LessEqual[N, 0.35], (-N[Log[N], $MachinePrecision]), N[(1.0 / N[(N + N[(0.5 - N[(0.08333333333333333 / N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;N \leq 0.35:\\
\;\;\;\;-\log N\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{N + \left(0.5 - \frac{0.08333333333333333}{N}\right)}\\
\end{array}
\end{array}
(FPCore (N) :precision binary64 (/ 1.0 (+ N 0.5)))
double code(double N) {
return 1.0 / (N + 0.5);
}
real(8) function code(n)
real(8), intent (in) :: n
code = 1.0d0 / (n + 0.5d0)
end function
public static double code(double N) {
return 1.0 / (N + 0.5);
}
def code(N): return 1.0 / (N + 0.5)
function code(N) return Float64(1.0 / Float64(N + 0.5)) end
function tmp = code(N) tmp = 1.0 / (N + 0.5); end
code[N_] := N[(1.0 / N[(N + 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{N + 0.5}
\end{array}
(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}
herbie shell --seed 2023343
(FPCore (N)
:name "2log (problem 3.3.6)"
:precision binary64
(- (log (+ N 1.0)) (log N)))