2log (problem 3.3.6)

Percentage Accurate: 53.6% → 99.9%
Time: 9.1s
Alternatives: 9
Speedup: 2.0×

Specification

?
\[\begin{array}{l} \\ \log \left(N + 1\right) - \log N \end{array} \]
(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:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 9 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 53.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(N + 1\right) - \log N \end{array} \]
(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}

Alternative 1: 99.9% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 4 \cdot 10^{-5}:\\ \;\;\;\;\left(\frac{1}{N} + \frac{0.3333333333333333}{{N}^{3}}\right) - \left(\frac{0.5}{{N}^{2}} + \frac{0.25}{{N}^{4}}\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\frac{N + 1}{N}\right)\\ \end{array} \end{array} \]
(FPCore (N)
 :precision binary64
 (if (<= (- (log (+ N 1.0)) (log N)) 4e-5)
   (-
    (+ (/ 1.0 N) (/ 0.3333333333333333 (pow N 3.0)))
    (+ (/ 0.5 (pow N 2.0)) (/ 0.25 (pow N 4.0))))
   (log (/ (+ N 1.0) N))))
double code(double N) {
	double tmp;
	if ((log((N + 1.0)) - log(N)) <= 4e-5) {
		tmp = ((1.0 / N) + (0.3333333333333333 / pow(N, 3.0))) - ((0.5 / pow(N, 2.0)) + (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
    real(8) :: tmp
    if ((log((n + 1.0d0)) - log(n)) <= 4d-5) then
        tmp = ((1.0d0 / n) + (0.3333333333333333d0 / (n ** 3.0d0))) - ((0.5d0 / (n ** 2.0d0)) + (0.25d0 / (n ** 4.0d0)))
    else
        tmp = log(((n + 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)) <= 4e-5) {
		tmp = ((1.0 / N) + (0.3333333333333333 / Math.pow(N, 3.0))) - ((0.5 / Math.pow(N, 2.0)) + (0.25 / Math.pow(N, 4.0)));
	} else {
		tmp = Math.log(((N + 1.0) / N));
	}
	return tmp;
}
def code(N):
	tmp = 0
	if (math.log((N + 1.0)) - math.log(N)) <= 4e-5:
		tmp = ((1.0 / N) + (0.3333333333333333 / math.pow(N, 3.0))) - ((0.5 / math.pow(N, 2.0)) + (0.25 / math.pow(N, 4.0)))
	else:
		tmp = math.log(((N + 1.0) / N))
	return tmp
function code(N)
	tmp = 0.0
	if (Float64(log(Float64(N + 1.0)) - log(N)) <= 4e-5)
		tmp = Float64(Float64(Float64(1.0 / N) + Float64(0.3333333333333333 / (N ^ 3.0))) - Float64(Float64(0.5 / (N ^ 2.0)) + Float64(0.25 / (N ^ 4.0))));
	else
		tmp = log(Float64(Float64(N + 1.0) / N));
	end
	return tmp
end
function tmp_2 = code(N)
	tmp = 0.0;
	if ((log((N + 1.0)) - log(N)) <= 4e-5)
		tmp = ((1.0 / N) + (0.3333333333333333 / (N ^ 3.0))) - ((0.5 / (N ^ 2.0)) + (0.25 / (N ^ 4.0)));
	else
		tmp = log(((N + 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], 4e-5], N[(N[(N[(1.0 / N), $MachinePrecision] + N[(0.3333333333333333 / N[Power[N, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(N[(0.5 / N[Power[N, 2.0], $MachinePrecision]), $MachinePrecision] + N[(0.25 / N[Power[N, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[N[(N[(N + 1.0), $MachinePrecision] / N), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\log \left(N + 1\right) - \log N \leq 4 \cdot 10^{-5}:\\
\;\;\;\;\left(\frac{1}{N} + \frac{0.3333333333333333}{{N}^{3}}\right) - \left(\frac{0.5}{{N}^{2}} + \frac{0.25}{{N}^{4}}\right)\\

\mathbf{else}:\\
\;\;\;\;\log \left(\frac{N + 1}{N}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N)) < 4.00000000000000033e-5

    1. Initial program 7.4%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative7.4%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def7.4%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified7.4%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Taylor expanded in N around inf 100.0%

      \[\leadsto \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)} \]
    5. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \color{blue}{\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) \]
      2. associate-*r/100.0%

        \[\leadsto \left(\frac{1}{N} + \color{blue}{\frac{0.3333333333333333 \cdot 1}{{N}^{3}}}\right) - \left(0.25 \cdot \frac{1}{{N}^{4}} + 0.5 \cdot \frac{1}{{N}^{2}}\right) \]
      3. metadata-eval100.0%

        \[\leadsto \left(\frac{1}{N} + \frac{\color{blue}{0.3333333333333333}}{{N}^{3}}\right) - \left(0.25 \cdot \frac{1}{{N}^{4}} + 0.5 \cdot \frac{1}{{N}^{2}}\right) \]
      4. +-commutative100.0%

        \[\leadsto \left(\frac{1}{N} + \frac{0.3333333333333333}{{N}^{3}}\right) - \color{blue}{\left(0.5 \cdot \frac{1}{{N}^{2}} + 0.25 \cdot \frac{1}{{N}^{4}}\right)} \]
      5. associate-*r/100.0%

        \[\leadsto \left(\frac{1}{N} + \frac{0.3333333333333333}{{N}^{3}}\right) - \left(\color{blue}{\frac{0.5 \cdot 1}{{N}^{2}}} + 0.25 \cdot \frac{1}{{N}^{4}}\right) \]
      6. metadata-eval100.0%

        \[\leadsto \left(\frac{1}{N} + \frac{0.3333333333333333}{{N}^{3}}\right) - \left(\frac{\color{blue}{0.5}}{{N}^{2}} + 0.25 \cdot \frac{1}{{N}^{4}}\right) \]
      7. associate-*r/100.0%

        \[\leadsto \left(\frac{1}{N} + \frac{0.3333333333333333}{{N}^{3}}\right) - \left(\frac{0.5}{{N}^{2}} + \color{blue}{\frac{0.25 \cdot 1}{{N}^{4}}}\right) \]
      8. metadata-eval100.0%

        \[\leadsto \left(\frac{1}{N} + \frac{0.3333333333333333}{{N}^{3}}\right) - \left(\frac{0.5}{{N}^{2}} + \frac{\color{blue}{0.25}}{{N}^{4}}\right) \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\left(\frac{1}{N} + \frac{0.3333333333333333}{{N}^{3}}\right) - \left(\frac{0.5}{{N}^{2}} + \frac{0.25}{{N}^{4}}\right)} \]

    if 4.00000000000000033e-5 < (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N))

    1. Initial program 100.0%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp100.0%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u5.8%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef5.8%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log5.8%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef5.8%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log5.8%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative5.8%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log5.8%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef5.8%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u100.0%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log100.0%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 4 \cdot 10^{-5}:\\ \;\;\;\;\left(\frac{1}{N} + \frac{0.3333333333333333}{{N}^{3}}\right) - \left(\frac{0.5}{{N}^{2}} + \frac{0.25}{{N}^{4}}\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\frac{N + 1}{N}\right)\\ \end{array} \]

Alternative 2: 99.9% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 4 \cdot 10^{-5}:\\ \;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1}{N} \cdot \frac{N + -0.5}{N} + \frac{-0.25}{{N}^{4}}\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\frac{N + 1}{N}\right)\\ \end{array} \end{array} \]
(FPCore (N)
 :precision binary64
 (if (<= (- (log (+ N 1.0)) (log N)) 4e-5)
   (+
    (/ 0.3333333333333333 (pow N 3.0))
    (+ (* (/ 1.0 N) (/ (+ N -0.5) N)) (/ -0.25 (pow N 4.0))))
   (log (/ (+ N 1.0) N))))
double code(double N) {
	double tmp;
	if ((log((N + 1.0)) - log(N)) <= 4e-5) {
		tmp = (0.3333333333333333 / pow(N, 3.0)) + (((1.0 / N) * ((N + -0.5) / 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
    real(8) :: tmp
    if ((log((n + 1.0d0)) - log(n)) <= 4d-5) then
        tmp = (0.3333333333333333d0 / (n ** 3.0d0)) + (((1.0d0 / n) * ((n + (-0.5d0)) / 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) {
	double tmp;
	if ((Math.log((N + 1.0)) - Math.log(N)) <= 4e-5) {
		tmp = (0.3333333333333333 / Math.pow(N, 3.0)) + (((1.0 / N) * ((N + -0.5) / N)) + (-0.25 / Math.pow(N, 4.0)));
	} else {
		tmp = Math.log(((N + 1.0) / N));
	}
	return tmp;
}
def code(N):
	tmp = 0
	if (math.log((N + 1.0)) - math.log(N)) <= 4e-5:
		tmp = (0.3333333333333333 / math.pow(N, 3.0)) + (((1.0 / N) * ((N + -0.5) / N)) + (-0.25 / math.pow(N, 4.0)))
	else:
		tmp = math.log(((N + 1.0) / N))
	return tmp
function code(N)
	tmp = 0.0
	if (Float64(log(Float64(N + 1.0)) - log(N)) <= 4e-5)
		tmp = Float64(Float64(0.3333333333333333 / (N ^ 3.0)) + Float64(Float64(Float64(1.0 / N) * Float64(Float64(N + -0.5) / N)) + Float64(-0.25 / (N ^ 4.0))));
	else
		tmp = log(Float64(Float64(N + 1.0) / N));
	end
	return tmp
end
function tmp_2 = code(N)
	tmp = 0.0;
	if ((log((N + 1.0)) - log(N)) <= 4e-5)
		tmp = (0.3333333333333333 / (N ^ 3.0)) + (((1.0 / N) * ((N + -0.5) / N)) + (-0.25 / (N ^ 4.0)));
	else
		tmp = log(((N + 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], 4e-5], N[(N[(0.3333333333333333 / N[Power[N, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(N[(1.0 / N), $MachinePrecision] * N[(N[(N + -0.5), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision] + N[(-0.25 / N[Power[N, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[N[(N[(N + 1.0), $MachinePrecision] / N), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\log \left(N + 1\right) - \log N \leq 4 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1}{N} \cdot \frac{N + -0.5}{N} + \frac{-0.25}{{N}^{4}}\right)\\

\mathbf{else}:\\
\;\;\;\;\log \left(\frac{N + 1}{N}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N)) < 4.00000000000000033e-5

    1. Initial program 7.4%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative7.4%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def7.4%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified7.4%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp7.4%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u7.4%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef7.4%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log7.2%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef7.2%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log5.9%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative5.9%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log5.9%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef5.9%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u5.9%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log7.8%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr7.8%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
    6. Taylor expanded in N around inf 100.0%

      \[\leadsto \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)} \]
    7. Simplified50.2%

      \[\leadsto \color{blue}{\frac{0.3333333333333333}{{N}^{3}} + \left(\frac{N + -0.5}{{N}^{2}} + \frac{-0.25}{{N}^{4}}\right)} \]
    8. Step-by-step derivation
      1. *-un-lft-identity49.6%

        \[\leadsto \frac{\color{blue}{1 \cdot \left(N + -0.5\right)}}{{N}^{2}} \]
      2. unpow249.6%

        \[\leadsto \frac{1 \cdot \left(N + -0.5\right)}{\color{blue}{N \cdot N}} \]
      3. times-frac99.4%

        \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    9. Applied egg-rr100.0%

      \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \left(\color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} + \frac{-0.25}{{N}^{4}}\right) \]

    if 4.00000000000000033e-5 < (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N))

    1. Initial program 100.0%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp100.0%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u5.8%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef5.8%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log5.8%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef5.8%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log5.8%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative5.8%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log5.8%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef5.8%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u100.0%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log100.0%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 4 \cdot 10^{-5}:\\ \;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1}{N} \cdot \frac{N + -0.5}{N} + \frac{-0.25}{{N}^{4}}\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\frac{N + 1}{N}\right)\\ \end{array} \]

Alternative 3: 99.9% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 4 \cdot 10^{-5}:\\ \;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\ \mathbf{else}:\\ \;\;\;\;\log \left(\frac{N + 1}{N}\right)\\ \end{array} \end{array} \]
(FPCore (N)
 :precision binary64
 (if (<= (- (log (+ N 1.0)) (log N)) 4e-5)
   (+ (/ 0.3333333333333333 (pow N 3.0)) (/ (/ 1.0 N) (/ N (+ N -0.5))))
   (log (/ (+ N 1.0) N))))
double code(double N) {
	double tmp;
	if ((log((N + 1.0)) - log(N)) <= 4e-5) {
		tmp = (0.3333333333333333 / pow(N, 3.0)) + ((1.0 / N) / (N / (N + -0.5)));
	} else {
		tmp = log(((N + 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)) <= 4d-5) then
        tmp = (0.3333333333333333d0 / (n ** 3.0d0)) + ((1.0d0 / n) / (n / (n + (-0.5d0))))
    else
        tmp = log(((n + 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)) <= 4e-5) {
		tmp = (0.3333333333333333 / Math.pow(N, 3.0)) + ((1.0 / N) / (N / (N + -0.5)));
	} else {
		tmp = Math.log(((N + 1.0) / N));
	}
	return tmp;
}
def code(N):
	tmp = 0
	if (math.log((N + 1.0)) - math.log(N)) <= 4e-5:
		tmp = (0.3333333333333333 / math.pow(N, 3.0)) + ((1.0 / N) / (N / (N + -0.5)))
	else:
		tmp = math.log(((N + 1.0) / N))
	return tmp
function code(N)
	tmp = 0.0
	if (Float64(log(Float64(N + 1.0)) - log(N)) <= 4e-5)
		tmp = Float64(Float64(0.3333333333333333 / (N ^ 3.0)) + Float64(Float64(1.0 / N) / Float64(N / Float64(N + -0.5))));
	else
		tmp = log(Float64(Float64(N + 1.0) / N));
	end
	return tmp
end
function tmp_2 = code(N)
	tmp = 0.0;
	if ((log((N + 1.0)) - log(N)) <= 4e-5)
		tmp = (0.3333333333333333 / (N ^ 3.0)) + ((1.0 / N) / (N / (N + -0.5)));
	else
		tmp = log(((N + 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], 4e-5], N[(N[(0.3333333333333333 / N[Power[N, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(1.0 / N), $MachinePrecision] / N[(N / N[(N + -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[N[(N[(N + 1.0), $MachinePrecision] / N), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\log \left(N + 1\right) - \log N \leq 4 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\

\mathbf{else}:\\
\;\;\;\;\log \left(\frac{N + 1}{N}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N)) < 4.00000000000000033e-5

    1. Initial program 7.4%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative7.4%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def7.4%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified7.4%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp7.4%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u7.4%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef7.4%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log7.2%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef7.2%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log5.9%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative5.9%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log5.9%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef5.9%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u5.9%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log7.8%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr7.8%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
    6. Taylor expanded in N around inf 99.9%

      \[\leadsto \color{blue}{\left(0.3333333333333333 \cdot \frac{1}{{N}^{3}} + \frac{1}{N}\right) - 0.5 \cdot \frac{1}{{N}^{2}}} \]
    7. Simplified50.1%

      \[\leadsto \color{blue}{\frac{0.3333333333333333}{{N}^{3}} + \frac{N + -0.5}{{N}^{2}}} \]
    8. Step-by-step derivation
      1. *-un-lft-identity49.6%

        \[\leadsto \frac{\color{blue}{1 \cdot \left(N + -0.5\right)}}{{N}^{2}} \]
      2. unpow249.6%

        \[\leadsto \frac{1 \cdot \left(N + -0.5\right)}{\color{blue}{N \cdot N}} \]
      3. times-frac99.4%

        \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    9. Applied egg-rr99.9%

      \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    10. Step-by-step derivation
      1. clear-num99.4%

        \[\leadsto \frac{1}{N} \cdot \color{blue}{\frac{1}{\frac{N}{N + -0.5}}} \]
      2. un-div-inv99.4%

        \[\leadsto \color{blue}{\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}} \]
    11. Applied egg-rr100.0%

      \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \color{blue}{\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}} \]

    if 4.00000000000000033e-5 < (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N))

    1. Initial program 100.0%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp100.0%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u5.8%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef5.8%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log5.8%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef5.8%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log5.8%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative5.8%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log5.8%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef5.8%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u100.0%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log100.0%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 4 \cdot 10^{-5}:\\ \;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\ \mathbf{else}:\\ \;\;\;\;\log \left(\frac{N + 1}{N}\right)\\ \end{array} \]

Alternative 4: 99.9% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;N \leq 13000:\\ \;\;\;\;\log \left(\frac{N + 1}{N}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \frac{\frac{N + -0.5}{N}}{N}\\ \end{array} \end{array} \]
(FPCore (N)
 :precision binary64
 (if (<= N 13000.0)
   (log (/ (+ N 1.0) N))
   (+ (/ 0.3333333333333333 (pow N 3.0)) (/ (/ (+ N -0.5) N) N))))
double code(double N) {
	double tmp;
	if (N <= 13000.0) {
		tmp = log(((N + 1.0) / N));
	} else {
		tmp = (0.3333333333333333 / pow(N, 3.0)) + (((N + -0.5) / N) / N);
	}
	return tmp;
}
real(8) function code(n)
    real(8), intent (in) :: n
    real(8) :: tmp
    if (n <= 13000.0d0) then
        tmp = log(((n + 1.0d0) / n))
    else
        tmp = (0.3333333333333333d0 / (n ** 3.0d0)) + (((n + (-0.5d0)) / n) / n)
    end if
    code = tmp
end function
public static double code(double N) {
	double tmp;
	if (N <= 13000.0) {
		tmp = Math.log(((N + 1.0) / N));
	} else {
		tmp = (0.3333333333333333 / Math.pow(N, 3.0)) + (((N + -0.5) / N) / N);
	}
	return tmp;
}
def code(N):
	tmp = 0
	if N <= 13000.0:
		tmp = math.log(((N + 1.0) / N))
	else:
		tmp = (0.3333333333333333 / math.pow(N, 3.0)) + (((N + -0.5) / N) / N)
	return tmp
function code(N)
	tmp = 0.0
	if (N <= 13000.0)
		tmp = log(Float64(Float64(N + 1.0) / N));
	else
		tmp = Float64(Float64(0.3333333333333333 / (N ^ 3.0)) + Float64(Float64(Float64(N + -0.5) / N) / N));
	end
	return tmp
end
function tmp_2 = code(N)
	tmp = 0.0;
	if (N <= 13000.0)
		tmp = log(((N + 1.0) / N));
	else
		tmp = (0.3333333333333333 / (N ^ 3.0)) + (((N + -0.5) / N) / N);
	end
	tmp_2 = tmp;
end
code[N_] := If[LessEqual[N, 13000.0], N[Log[N[(N[(N + 1.0), $MachinePrecision] / N), $MachinePrecision]], $MachinePrecision], N[(N[(0.3333333333333333 / N[Power[N, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(N[(N + -0.5), $MachinePrecision] / N), $MachinePrecision] / N), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;N \leq 13000:\\
\;\;\;\;\log \left(\frac{N + 1}{N}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \frac{\frac{N + -0.5}{N}}{N}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if N < 13000

    1. Initial program 100.0%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp100.0%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u5.8%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef5.8%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log5.8%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef5.8%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log5.8%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative5.8%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log5.8%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef5.8%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u100.0%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log100.0%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]

    if 13000 < N

    1. Initial program 7.4%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative7.4%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def7.4%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified7.4%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp7.4%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u7.4%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef7.4%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log7.2%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef7.2%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log5.9%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative5.9%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log5.9%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef5.9%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u5.9%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log7.8%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr7.8%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
    6. Taylor expanded in N around inf 99.9%

      \[\leadsto \color{blue}{\left(0.3333333333333333 \cdot \frac{1}{{N}^{3}} + \frac{1}{N}\right) - 0.5 \cdot \frac{1}{{N}^{2}}} \]
    7. Simplified50.1%

      \[\leadsto \color{blue}{\frac{0.3333333333333333}{{N}^{3}} + \frac{N + -0.5}{{N}^{2}}} \]
    8. Step-by-step derivation
      1. *-un-lft-identity49.6%

        \[\leadsto \frac{\color{blue}{1 \cdot \left(N + -0.5\right)}}{{N}^{2}} \]
      2. unpow249.6%

        \[\leadsto \frac{1 \cdot \left(N + -0.5\right)}{\color{blue}{N \cdot N}} \]
      3. times-frac99.4%

        \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    9. Applied egg-rr99.9%

      \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    10. Step-by-step derivation
      1. associate-*l/99.9%

        \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \color{blue}{\frac{1 \cdot \frac{N + -0.5}{N}}{N}} \]
      2. *-un-lft-identity99.9%

        \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \frac{\color{blue}{\frac{N + -0.5}{N}}}{N} \]
    11. Applied egg-rr99.9%

      \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \color{blue}{\frac{\frac{N + -0.5}{N}}{N}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;N \leq 13000:\\ \;\;\;\;\log \left(\frac{N + 1}{N}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \frac{\frac{N + -0.5}{N}}{N}\\ \end{array} \]

Alternative 5: 99.8% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;N \leq 180000:\\ \;\;\;\;\log \left(\frac{N + 1}{N}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\ \end{array} \end{array} \]
(FPCore (N)
 :precision binary64
 (if (<= N 180000.0) (log (/ (+ N 1.0) N)) (/ (/ 1.0 N) (/ N (+ N -0.5)))))
double code(double N) {
	double tmp;
	if (N <= 180000.0) {
		tmp = log(((N + 1.0) / N));
	} else {
		tmp = (1.0 / N) / (N / (N + -0.5));
	}
	return tmp;
}
real(8) function code(n)
    real(8), intent (in) :: n
    real(8) :: tmp
    if (n <= 180000.0d0) then
        tmp = log(((n + 1.0d0) / n))
    else
        tmp = (1.0d0 / n) / (n / (n + (-0.5d0)))
    end if
    code = tmp
end function
public static double code(double N) {
	double tmp;
	if (N <= 180000.0) {
		tmp = Math.log(((N + 1.0) / N));
	} else {
		tmp = (1.0 / N) / (N / (N + -0.5));
	}
	return tmp;
}
def code(N):
	tmp = 0
	if N <= 180000.0:
		tmp = math.log(((N + 1.0) / N))
	else:
		tmp = (1.0 / N) / (N / (N + -0.5))
	return tmp
function code(N)
	tmp = 0.0
	if (N <= 180000.0)
		tmp = log(Float64(Float64(N + 1.0) / N));
	else
		tmp = Float64(Float64(1.0 / N) / Float64(N / Float64(N + -0.5)));
	end
	return tmp
end
function tmp_2 = code(N)
	tmp = 0.0;
	if (N <= 180000.0)
		tmp = log(((N + 1.0) / N));
	else
		tmp = (1.0 / N) / (N / (N + -0.5));
	end
	tmp_2 = tmp;
end
code[N_] := If[LessEqual[N, 180000.0], N[Log[N[(N[(N + 1.0), $MachinePrecision] / N), $MachinePrecision]], $MachinePrecision], N[(N[(1.0 / N), $MachinePrecision] / N[(N / N[(N + -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;N \leq 180000:\\
\;\;\;\;\log \left(\frac{N + 1}{N}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if N < 1.8e5

    1. Initial program 99.8%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative99.8%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def99.8%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified99.8%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp99.8%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u6.3%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef6.3%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log6.3%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef6.3%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log6.3%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative6.3%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log6.3%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef6.3%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u99.8%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log99.9%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr99.9%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]

    if 1.8e5 < N

    1. Initial program 6.9%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative6.9%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def6.9%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified6.9%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp6.9%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u6.9%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef6.9%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log6.6%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef6.6%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log5.4%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative5.4%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log5.4%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef5.4%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u5.4%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log7.2%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr7.2%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
    6. Taylor expanded in N around inf 99.7%

      \[\leadsto \color{blue}{\frac{1}{N} - 0.5 \cdot \frac{1}{{N}^{2}}} \]
    7. Step-by-step derivation
      1. *-rgt-identity99.7%

        \[\leadsto \color{blue}{\frac{1}{N} \cdot 1} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      2. *-rgt-identity99.7%

        \[\leadsto \color{blue}{\frac{1}{N}} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      3. *-inverses46.0%

        \[\leadsto \frac{\color{blue}{\frac{-{N}^{2}}{-{N}^{2}}}}{N} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      4. associate-/r*36.2%

        \[\leadsto \color{blue}{\frac{-{N}^{2}}{\left(-{N}^{2}\right) \cdot N}} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      5. *-commutative36.2%

        \[\leadsto \frac{-{N}^{2}}{\color{blue}{N \cdot \left(-{N}^{2}\right)}} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      6. *-lft-identity36.2%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \color{blue}{1 \cdot \left(0.5 \cdot \frac{1}{{N}^{2}}\right)} \]
      7. *-inverses36.2%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \color{blue}{\frac{-N}{-N}} \cdot \left(0.5 \cdot \frac{1}{{N}^{2}}\right) \]
      8. associate-*r/36.2%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{-N}{-N} \cdot \color{blue}{\frac{0.5 \cdot 1}{{N}^{2}}} \]
      9. metadata-eval36.2%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{-N}{-N} \cdot \frac{\color{blue}{0.5}}{{N}^{2}} \]
      10. times-frac36.3%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \color{blue}{\frac{\left(-N\right) \cdot 0.5}{\left(-N\right) \cdot {N}^{2}}} \]
      11. distribute-lft-neg-out36.3%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\left(-N\right) \cdot 0.5}{\color{blue}{-N \cdot {N}^{2}}} \]
      12. distribute-rgt-neg-out36.3%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\left(-N\right) \cdot 0.5}{\color{blue}{N \cdot \left(-{N}^{2}\right)}} \]
      13. metadata-eval36.3%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\left(-N\right) \cdot \color{blue}{\left(--0.5\right)}}{N \cdot \left(-{N}^{2}\right)} \]
      14. distribute-rgt-neg-in36.3%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\color{blue}{-\left(-N\right) \cdot -0.5}}{N \cdot \left(-{N}^{2}\right)} \]
      15. distribute-lft-neg-out36.3%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{-\color{blue}{\left(-N \cdot -0.5\right)}}{N \cdot \left(-{N}^{2}\right)} \]
      16. remove-double-neg36.3%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\color{blue}{N \cdot -0.5}}{N \cdot \left(-{N}^{2}\right)} \]
    8. Simplified49.4%

      \[\leadsto \color{blue}{\frac{N + -0.5}{{N}^{2}}} \]
    9. Step-by-step derivation
      1. *-un-lft-identity49.4%

        \[\leadsto \frac{\color{blue}{1 \cdot \left(N + -0.5\right)}}{{N}^{2}} \]
      2. unpow249.4%

        \[\leadsto \frac{1 \cdot \left(N + -0.5\right)}{\color{blue}{N \cdot N}} \]
      3. times-frac99.7%

        \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    10. Applied egg-rr99.7%

      \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    11. Step-by-step derivation
      1. clear-num99.7%

        \[\leadsto \frac{1}{N} \cdot \color{blue}{\frac{1}{\frac{N}{N + -0.5}}} \]
      2. un-div-inv99.7%

        \[\leadsto \color{blue}{\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}} \]
    12. Applied egg-rr99.7%

      \[\leadsto \color{blue}{\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;N \leq 180000:\\ \;\;\;\;\log \left(\frac{N + 1}{N}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\ \end{array} \]

Alternative 6: 99.2% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;N \leq 0.9:\\ \;\;\;\;N - \log N\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\ \end{array} \end{array} \]
(FPCore (N)
 :precision binary64
 (if (<= N 0.9) (- N (log N)) (/ (/ 1.0 N) (/ N (+ N -0.5)))))
double code(double N) {
	double tmp;
	if (N <= 0.9) {
		tmp = N - log(N);
	} else {
		tmp = (1.0 / N) / (N / (N + -0.5));
	}
	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 / n) / (n / (n + (-0.5d0)))
    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 / N) / (N / (N + -0.5));
	}
	return tmp;
}
def code(N):
	tmp = 0
	if N <= 0.9:
		tmp = N - math.log(N)
	else:
		tmp = (1.0 / N) / (N / (N + -0.5))
	return tmp
function code(N)
	tmp = 0.0
	if (N <= 0.9)
		tmp = Float64(N - log(N));
	else
		tmp = Float64(Float64(1.0 / N) / Float64(N / Float64(N + -0.5)));
	end
	return tmp
end
function tmp_2 = code(N)
	tmp = 0.0;
	if (N <= 0.9)
		tmp = N - log(N);
	else
		tmp = (1.0 / N) / (N / (N + -0.5));
	end
	tmp_2 = tmp;
end
code[N_] := If[LessEqual[N, 0.9], N[(N - N[Log[N], $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / N), $MachinePrecision] / N[(N / N[(N + -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;N \leq 0.9:\\
\;\;\;\;N - \log N\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if N < 0.900000000000000022

    1. Initial program 100.0%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Taylor expanded in N around 0 99.3%

      \[\leadsto \color{blue}{N + -1 \cdot \log N} \]
    5. Step-by-step derivation
      1. neg-mul-199.3%

        \[\leadsto N + \color{blue}{\left(-\log N\right)} \]
      2. unsub-neg99.3%

        \[\leadsto \color{blue}{N - \log N} \]
    6. Simplified99.3%

      \[\leadsto \color{blue}{N - \log N} \]

    if 0.900000000000000022 < N

    1. Initial program 7.4%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative7.4%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def7.4%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified7.4%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp7.4%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u7.4%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef7.4%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log7.2%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef7.2%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log5.9%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative5.9%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log5.9%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef5.9%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u5.9%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log7.8%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr7.8%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
    6. Taylor expanded in N around inf 99.4%

      \[\leadsto \color{blue}{\frac{1}{N} - 0.5 \cdot \frac{1}{{N}^{2}}} \]
    7. Step-by-step derivation
      1. *-rgt-identity99.4%

        \[\leadsto \color{blue}{\frac{1}{N} \cdot 1} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      2. *-rgt-identity99.4%

        \[\leadsto \color{blue}{\frac{1}{N}} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      3. *-inverses46.1%

        \[\leadsto \frac{\color{blue}{\frac{-{N}^{2}}{-{N}^{2}}}}{N} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      4. associate-/r*36.5%

        \[\leadsto \color{blue}{\frac{-{N}^{2}}{\left(-{N}^{2}\right) \cdot N}} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      5. *-commutative36.5%

        \[\leadsto \frac{-{N}^{2}}{\color{blue}{N \cdot \left(-{N}^{2}\right)}} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      6. *-lft-identity36.5%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \color{blue}{1 \cdot \left(0.5 \cdot \frac{1}{{N}^{2}}\right)} \]
      7. *-inverses36.5%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \color{blue}{\frac{-N}{-N}} \cdot \left(0.5 \cdot \frac{1}{{N}^{2}}\right) \]
      8. associate-*r/36.5%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{-N}{-N} \cdot \color{blue}{\frac{0.5 \cdot 1}{{N}^{2}}} \]
      9. metadata-eval36.5%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{-N}{-N} \cdot \frac{\color{blue}{0.5}}{{N}^{2}} \]
      10. times-frac36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \color{blue}{\frac{\left(-N\right) \cdot 0.5}{\left(-N\right) \cdot {N}^{2}}} \]
      11. distribute-lft-neg-out36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\left(-N\right) \cdot 0.5}{\color{blue}{-N \cdot {N}^{2}}} \]
      12. distribute-rgt-neg-out36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\left(-N\right) \cdot 0.5}{\color{blue}{N \cdot \left(-{N}^{2}\right)}} \]
      13. metadata-eval36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\left(-N\right) \cdot \color{blue}{\left(--0.5\right)}}{N \cdot \left(-{N}^{2}\right)} \]
      14. distribute-rgt-neg-in36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\color{blue}{-\left(-N\right) \cdot -0.5}}{N \cdot \left(-{N}^{2}\right)} \]
      15. distribute-lft-neg-out36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{-\color{blue}{\left(-N \cdot -0.5\right)}}{N \cdot \left(-{N}^{2}\right)} \]
      16. remove-double-neg36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\color{blue}{N \cdot -0.5}}{N \cdot \left(-{N}^{2}\right)} \]
    8. Simplified49.6%

      \[\leadsto \color{blue}{\frac{N + -0.5}{{N}^{2}}} \]
    9. Step-by-step derivation
      1. *-un-lft-identity49.6%

        \[\leadsto \frac{\color{blue}{1 \cdot \left(N + -0.5\right)}}{{N}^{2}} \]
      2. unpow249.6%

        \[\leadsto \frac{1 \cdot \left(N + -0.5\right)}{\color{blue}{N \cdot N}} \]
      3. times-frac99.4%

        \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    10. Applied egg-rr99.4%

      \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    11. Step-by-step derivation
      1. clear-num99.4%

        \[\leadsto \frac{1}{N} \cdot \color{blue}{\frac{1}{\frac{N}{N + -0.5}}} \]
      2. un-div-inv99.4%

        \[\leadsto \color{blue}{\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}} \]
    12. Applied egg-rr99.4%

      \[\leadsto \color{blue}{\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;N \leq 0.9:\\ \;\;\;\;N - \log N\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\ \end{array} \]

Alternative 7: 98.8% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;N \leq 0.7:\\ \;\;\;\;-\log N\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\ \end{array} \end{array} \]
(FPCore (N)
 :precision binary64
 (if (<= N 0.7) (- (log N)) (/ (/ 1.0 N) (/ N (+ N -0.5)))))
double code(double N) {
	double tmp;
	if (N <= 0.7) {
		tmp = -log(N);
	} else {
		tmp = (1.0 / N) / (N / (N + -0.5));
	}
	return tmp;
}
real(8) function code(n)
    real(8), intent (in) :: n
    real(8) :: tmp
    if (n <= 0.7d0) then
        tmp = -log(n)
    else
        tmp = (1.0d0 / n) / (n / (n + (-0.5d0)))
    end if
    code = tmp
end function
public static double code(double N) {
	double tmp;
	if (N <= 0.7) {
		tmp = -Math.log(N);
	} else {
		tmp = (1.0 / N) / (N / (N + -0.5));
	}
	return tmp;
}
def code(N):
	tmp = 0
	if N <= 0.7:
		tmp = -math.log(N)
	else:
		tmp = (1.0 / N) / (N / (N + -0.5))
	return tmp
function code(N)
	tmp = 0.0
	if (N <= 0.7)
		tmp = Float64(-log(N));
	else
		tmp = Float64(Float64(1.0 / N) / Float64(N / Float64(N + -0.5)));
	end
	return tmp
end
function tmp_2 = code(N)
	tmp = 0.0;
	if (N <= 0.7)
		tmp = -log(N);
	else
		tmp = (1.0 / N) / (N / (N + -0.5));
	end
	tmp_2 = tmp;
end
code[N_] := If[LessEqual[N, 0.7], (-N[Log[N], $MachinePrecision]), N[(N[(1.0 / N), $MachinePrecision] / N[(N / N[(N + -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;N \leq 0.7:\\
\;\;\;\;-\log N\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if N < 0.69999999999999996

    1. Initial program 100.0%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Taylor expanded in N around 0 98.6%

      \[\leadsto \color{blue}{-1 \cdot \log N} \]
    5. Step-by-step derivation
      1. neg-mul-198.6%

        \[\leadsto \color{blue}{-\log N} \]
    6. Simplified98.6%

      \[\leadsto \color{blue}{-\log N} \]

    if 0.69999999999999996 < N

    1. Initial program 7.4%

      \[\log \left(N + 1\right) - \log N \]
    2. Step-by-step derivation
      1. +-commutative7.4%

        \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
      2. log1p-def7.4%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
    3. Simplified7.4%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    4. Step-by-step derivation
      1. add-log-exp7.4%

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u7.4%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)} \]
      3. log1p-udef7.4%

        \[\leadsto \log \left(e^{\mathsf{log1p}\left(N\right)}\right) - \color{blue}{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)} \]
      4. diff-log7.2%

        \[\leadsto \color{blue}{\log \left(\frac{e^{\mathsf{log1p}\left(N\right)}}{1 + \mathsf{expm1}\left(\log N\right)}\right)} \]
      5. log1p-udef7.2%

        \[\leadsto \log \left(\frac{e^{\color{blue}{\log \left(1 + N\right)}}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      6. rem-exp-log5.9%

        \[\leadsto \log \left(\frac{\color{blue}{1 + N}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      7. +-commutative5.9%

        \[\leadsto \log \left(\frac{\color{blue}{N + 1}}{1 + \mathsf{expm1}\left(\log N\right)}\right) \]
      8. add-exp-log5.9%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef5.9%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      10. log1p-expm1-u5.9%

        \[\leadsto \log \left(\frac{N + 1}{e^{\color{blue}{\log N}}}\right) \]
      11. add-exp-log7.8%

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{N}}\right) \]
    5. Applied egg-rr7.8%

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
    6. Taylor expanded in N around inf 99.4%

      \[\leadsto \color{blue}{\frac{1}{N} - 0.5 \cdot \frac{1}{{N}^{2}}} \]
    7. Step-by-step derivation
      1. *-rgt-identity99.4%

        \[\leadsto \color{blue}{\frac{1}{N} \cdot 1} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      2. *-rgt-identity99.4%

        \[\leadsto \color{blue}{\frac{1}{N}} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      3. *-inverses46.1%

        \[\leadsto \frac{\color{blue}{\frac{-{N}^{2}}{-{N}^{2}}}}{N} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      4. associate-/r*36.5%

        \[\leadsto \color{blue}{\frac{-{N}^{2}}{\left(-{N}^{2}\right) \cdot N}} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      5. *-commutative36.5%

        \[\leadsto \frac{-{N}^{2}}{\color{blue}{N \cdot \left(-{N}^{2}\right)}} - 0.5 \cdot \frac{1}{{N}^{2}} \]
      6. *-lft-identity36.5%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \color{blue}{1 \cdot \left(0.5 \cdot \frac{1}{{N}^{2}}\right)} \]
      7. *-inverses36.5%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \color{blue}{\frac{-N}{-N}} \cdot \left(0.5 \cdot \frac{1}{{N}^{2}}\right) \]
      8. associate-*r/36.5%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{-N}{-N} \cdot \color{blue}{\frac{0.5 \cdot 1}{{N}^{2}}} \]
      9. metadata-eval36.5%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{-N}{-N} \cdot \frac{\color{blue}{0.5}}{{N}^{2}} \]
      10. times-frac36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \color{blue}{\frac{\left(-N\right) \cdot 0.5}{\left(-N\right) \cdot {N}^{2}}} \]
      11. distribute-lft-neg-out36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\left(-N\right) \cdot 0.5}{\color{blue}{-N \cdot {N}^{2}}} \]
      12. distribute-rgt-neg-out36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\left(-N\right) \cdot 0.5}{\color{blue}{N \cdot \left(-{N}^{2}\right)}} \]
      13. metadata-eval36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\left(-N\right) \cdot \color{blue}{\left(--0.5\right)}}{N \cdot \left(-{N}^{2}\right)} \]
      14. distribute-rgt-neg-in36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\color{blue}{-\left(-N\right) \cdot -0.5}}{N \cdot \left(-{N}^{2}\right)} \]
      15. distribute-lft-neg-out36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{-\color{blue}{\left(-N \cdot -0.5\right)}}{N \cdot \left(-{N}^{2}\right)} \]
      16. remove-double-neg36.6%

        \[\leadsto \frac{-{N}^{2}}{N \cdot \left(-{N}^{2}\right)} - \frac{\color{blue}{N \cdot -0.5}}{N \cdot \left(-{N}^{2}\right)} \]
    8. Simplified49.6%

      \[\leadsto \color{blue}{\frac{N + -0.5}{{N}^{2}}} \]
    9. Step-by-step derivation
      1. *-un-lft-identity49.6%

        \[\leadsto \frac{\color{blue}{1 \cdot \left(N + -0.5\right)}}{{N}^{2}} \]
      2. unpow249.6%

        \[\leadsto \frac{1 \cdot \left(N + -0.5\right)}{\color{blue}{N \cdot N}} \]
      3. times-frac99.4%

        \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    10. Applied egg-rr99.4%

      \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    11. Step-by-step derivation
      1. clear-num99.4%

        \[\leadsto \frac{1}{N} \cdot \color{blue}{\frac{1}{\frac{N}{N + -0.5}}} \]
      2. un-div-inv99.4%

        \[\leadsto \color{blue}{\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}} \]
    12. Applied egg-rr99.4%

      \[\leadsto \color{blue}{\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;N \leq 0.7:\\ \;\;\;\;-\log N\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{N}}{\frac{N}{N + -0.5}}\\ \end{array} \]

Alternative 8: 52.1% accurate, 68.3× speedup?

\[\begin{array}{l} \\ \frac{1}{N} \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}
Derivation
  1. Initial program 55.9%

    \[\log \left(N + 1\right) - \log N \]
  2. Step-by-step derivation
    1. +-commutative55.9%

      \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
    2. log1p-def55.9%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
  3. Simplified55.9%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
  4. Taylor expanded in N around inf 49.6%

    \[\leadsto \color{blue}{\frac{1}{N}} \]
  5. Final simplification49.6%

    \[\leadsto \frac{1}{N} \]

Alternative 9: 4.5% accurate, 205.0× speedup?

\[\begin{array}{l} \\ N \end{array} \]
(FPCore (N) :precision binary64 N)
double code(double N) {
	return N;
}
real(8) function code(n)
    real(8), intent (in) :: n
    code = n
end function
public static double code(double N) {
	return N;
}
def code(N):
	return N
function code(N)
	return N
end
function tmp = code(N)
	tmp = N;
end
code[N_] := N
\begin{array}{l}

\\
N
\end{array}
Derivation
  1. Initial program 55.9%

    \[\log \left(N + 1\right) - \log N \]
  2. Step-by-step derivation
    1. +-commutative55.9%

      \[\leadsto \log \color{blue}{\left(1 + N\right)} - \log N \]
    2. log1p-def55.9%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
  3. Simplified55.9%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
  4. Taylor expanded in N around 0 53.8%

    \[\leadsto \color{blue}{N + -1 \cdot \log N} \]
  5. Step-by-step derivation
    1. neg-mul-153.8%

      \[\leadsto N + \color{blue}{\left(-\log N\right)} \]
    2. unsub-neg53.8%

      \[\leadsto \color{blue}{N - \log N} \]
  6. Simplified53.8%

    \[\leadsto \color{blue}{N - \log N} \]
  7. Taylor expanded in N around inf 4.5%

    \[\leadsto \color{blue}{N} \]
  8. Final simplification4.5%

    \[\leadsto N \]

Reproduce

?
herbie shell --seed 2023339 
(FPCore (N)
  :name "2log (problem 3.3.6)"
  :precision binary64
  (- (log (+ N 1.0)) (log N)))