
(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 10 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.001)
(/
1.0
(-
(fma
N
(/
(fma N (fma N -0.5 0.08333333333333333) -0.041666666666666664)
(* N (* N N)))
(- N))))
(exp (- (log (/ -1.0 (log (/ N (+ N 1.0)))))))))
double code(double N) {
double tmp;
if ((log((N + 1.0)) - log(N)) <= 0.001) {
tmp = 1.0 / -fma(N, (fma(N, fma(N, -0.5, 0.08333333333333333), -0.041666666666666664) / (N * (N * N))), -N);
} else {
tmp = exp(-log((-1.0 / log((N / (N + 1.0))))));
}
return tmp;
}
function code(N) tmp = 0.0 if (Float64(log(Float64(N + 1.0)) - log(N)) <= 0.001) tmp = Float64(1.0 / Float64(-fma(N, Float64(fma(N, fma(N, -0.5, 0.08333333333333333), -0.041666666666666664) / Float64(N * Float64(N * N))), Float64(-N)))); else tmp = exp(Float64(-log(Float64(-1.0 / log(Float64(N / Float64(N + 1.0))))))); end return tmp end
code[N_] := If[LessEqual[N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision], 0.001], N[(1.0 / (-N[(N * N[(N[(N * N[(N * -0.5 + 0.08333333333333333), $MachinePrecision] + -0.041666666666666664), $MachinePrecision] / N[(N * N[(N * N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + (-N)), $MachinePrecision])), $MachinePrecision], N[Exp[(-N[Log[N[(-1.0 / N[Log[N[(N / N[(N + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision])], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\log \left(N + 1\right) - \log N \leq 0.001:\\
\;\;\;\;\frac{1}{-\mathsf{fma}\left(N, \frac{\mathsf{fma}\left(N, \mathsf{fma}\left(N, -0.5, 0.08333333333333333\right), -0.041666666666666664\right)}{N \cdot \left(N \cdot N\right)}, -N\right)}\\
\mathbf{else}:\\
\;\;\;\;e^{-\log \left(\frac{-1}{\log \left(\frac{N}{N + 1}\right)}\right)}\\
\end{array}
\end{array}
if (-.f64 (log.f64 (+.f64 N #s(literal 1 binary64))) (log.f64 N)) < 1e-3Initial program 18.2%
lift--.f64N/A
lift-log.f64N/A
lift-log.f64N/A
diff-logN/A
clear-numN/A
neg-logN/A
diff-logN/A
lift-log.f64N/A
lift-log.f64N/A
lower-neg.f64N/A
lift-log.f64N/A
lift-log.f64N/A
diff-logN/A
lower-log.f64N/A
lower-/.f6421.3
Applied rewrites21.3%
Applied rewrites21.3%
Taylor expanded in N around -inf
mul-1-negN/A
lower-neg.f64N/A
sub-negN/A
metadata-evalN/A
distribute-lft-inN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites99.9%
Taylor expanded in N around 0
Applied rewrites99.9%
if 1e-3 < (-.f64 (log.f64 (+.f64 N #s(literal 1 binary64))) (log.f64 N)) Initial program 88.7%
lift--.f64N/A
lift-log.f64N/A
lift-log.f64N/A
diff-logN/A
clear-numN/A
neg-logN/A
diff-logN/A
lift-log.f64N/A
lift-log.f64N/A
lower-neg.f64N/A
lift-log.f64N/A
lift-log.f64N/A
diff-logN/A
lower-log.f64N/A
lower-/.f6493.4
Applied rewrites93.4%
Applied rewrites93.6%
Final simplification99.5%
(FPCore (N)
:precision binary64
(if (<= N 1100.0)
(/ 1.0 (/ -1.0 (log (/ N (+ N 1.0)))))
(/
1.0
(-
(fma
N
(/
(fma N (fma N -0.5 0.08333333333333333) -0.041666666666666664)
(* N (* N N)))
(- N))))))
double code(double N) {
double tmp;
if (N <= 1100.0) {
tmp = 1.0 / (-1.0 / log((N / (N + 1.0))));
} else {
tmp = 1.0 / -fma(N, (fma(N, fma(N, -0.5, 0.08333333333333333), -0.041666666666666664) / (N * (N * N))), -N);
}
return tmp;
}
function code(N) tmp = 0.0 if (N <= 1100.0) tmp = Float64(1.0 / Float64(-1.0 / log(Float64(N / Float64(N + 1.0))))); else tmp = Float64(1.0 / Float64(-fma(N, Float64(fma(N, fma(N, -0.5, 0.08333333333333333), -0.041666666666666664) / Float64(N * Float64(N * N))), Float64(-N)))); end return tmp end
code[N_] := If[LessEqual[N, 1100.0], N[(1.0 / N[(-1.0 / N[Log[N[(N / N[(N + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / (-N[(N * N[(N[(N * N[(N * -0.5 + 0.08333333333333333), $MachinePrecision] + -0.041666666666666664), $MachinePrecision] / N[(N * N[(N * N), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + (-N)), $MachinePrecision])), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;N \leq 1100:\\
\;\;\;\;\frac{1}{\frac{-1}{\log \left(\frac{N}{N + 1}\right)}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{-\mathsf{fma}\left(N, \frac{\mathsf{fma}\left(N, \mathsf{fma}\left(N, -0.5, 0.08333333333333333\right), -0.041666666666666664\right)}{N \cdot \left(N \cdot N\right)}, -N\right)}\\
\end{array}
\end{array}
if N < 1100Initial program 92.2%
lift--.f64N/A
lift-log.f64N/A
lift-log.f64N/A
diff-logN/A
clear-numN/A
neg-logN/A
diff-logN/A
lift-log.f64N/A
lift-log.f64N/A
lower-neg.f64N/A
lift-log.f64N/A
lift-log.f64N/A
diff-logN/A
lower-log.f64N/A
lower-/.f6495.3
Applied rewrites95.3%
Applied rewrites95.3%
if 1100 < N Initial program 18.8%
lift--.f64N/A
lift-log.f64N/A
lift-log.f64N/A
diff-logN/A
clear-numN/A
neg-logN/A
diff-logN/A
lift-log.f64N/A
lift-log.f64N/A
lower-neg.f64N/A
lift-log.f64N/A
lift-log.f64N/A
diff-logN/A
lower-log.f64N/A
lower-/.f6422.2
Applied rewrites22.2%
Applied rewrites22.2%
Taylor expanded in N around -inf
mul-1-negN/A
lower-neg.f64N/A
sub-negN/A
metadata-evalN/A
distribute-lft-inN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites99.9%
Taylor expanded in N around 0
Applied rewrites99.9%
(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}
(FPCore (N) :precision binary64 (log (+ 1.0 (/ 1.0 N))))
double code(double N) {
return log((1.0 + (1.0 / N)));
}
real(8) function code(n)
real(8), intent (in) :: n
code = log((1.0d0 + (1.0d0 / n)))
end function
public static double code(double N) {
return Math.log((1.0 + (1.0 / N)));
}
def code(N): return math.log((1.0 + (1.0 / N)))
function code(N) return log(Float64(1.0 + Float64(1.0 / N))) end
function tmp = code(N) tmp = log((1.0 + (1.0 / N))); end
code[N_] := N[Log[N[(1.0 + N[(1.0 / N), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\log \left(1 + \frac{1}{N}\right)
\end{array}
(FPCore (N) :precision binary64 (+ (+ (+ (/ 1.0 N) (/ -1.0 (* 2.0 (pow N 2.0)))) (/ 1.0 (* 3.0 (pow N 3.0)))) (/ -1.0 (* 4.0 (pow N 4.0)))))
double code(double N) {
return (((1.0 / N) + (-1.0 / (2.0 * pow(N, 2.0)))) + (1.0 / (3.0 * pow(N, 3.0)))) + (-1.0 / (4.0 * pow(N, 4.0)));
}
real(8) function code(n)
real(8), intent (in) :: n
code = (((1.0d0 / n) + ((-1.0d0) / (2.0d0 * (n ** 2.0d0)))) + (1.0d0 / (3.0d0 * (n ** 3.0d0)))) + ((-1.0d0) / (4.0d0 * (n ** 4.0d0)))
end function
public static double code(double N) {
return (((1.0 / N) + (-1.0 / (2.0 * Math.pow(N, 2.0)))) + (1.0 / (3.0 * Math.pow(N, 3.0)))) + (-1.0 / (4.0 * Math.pow(N, 4.0)));
}
def code(N): return (((1.0 / N) + (-1.0 / (2.0 * math.pow(N, 2.0)))) + (1.0 / (3.0 * math.pow(N, 3.0)))) + (-1.0 / (4.0 * math.pow(N, 4.0)))
function code(N) return Float64(Float64(Float64(Float64(1.0 / N) + Float64(-1.0 / Float64(2.0 * (N ^ 2.0)))) + Float64(1.0 / Float64(3.0 * (N ^ 3.0)))) + Float64(-1.0 / Float64(4.0 * (N ^ 4.0)))) end
function tmp = code(N) tmp = (((1.0 / N) + (-1.0 / (2.0 * (N ^ 2.0)))) + (1.0 / (3.0 * (N ^ 3.0)))) + (-1.0 / (4.0 * (N ^ 4.0))); end
code[N_] := N[(N[(N[(N[(1.0 / N), $MachinePrecision] + N[(-1.0 / N[(2.0 * N[Power[N, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(3.0 * N[Power[N, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / N[(4.0 * N[Power[N, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(\frac{1}{N} + \frac{-1}{2 \cdot {N}^{2}}\right) + \frac{1}{3 \cdot {N}^{3}}\right) + \frac{-1}{4 \cdot {N}^{4}}
\end{array}
herbie shell --seed 2024230
(FPCore (N)
:name "2log (problem 3.3.6)"
:precision binary64
:pre (and (> N 1.0) (< N 1e+40))
:alt
(! :herbie-platform default (log1p (/ 1 N)))
:alt
(! :herbie-platform default (log (+ 1 (/ 1 N))))
:alt
(! :herbie-platform default (+ (/ 1 N) (/ -1 (* 2 (pow N 2))) (/ 1 (* 3 (pow N 3))) (/ -1 (* 4 (pow N 4)))))
(- (log (+ N 1.0)) (log N)))