2log (problem 3.3.6)

Percentage Accurate: 54.3% → 100.0%
Time: 6.4s
Alternatives: 7
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 7 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: 54.3% 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: 100.0% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 0.0005:\\ \;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \left(\frac{\frac{N + -0.5}{N}}{N} + \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)) 0.0005)
   (+
    (/ 0.3333333333333333 (pow N 3.0))
    (+ (/ (/ (+ N -0.5) N) N) (/ -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.0005) {
		tmp = (0.3333333333333333 / pow(N, 3.0)) + ((((N + -0.5) / N) / N) + (-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.0005d0) then
        tmp = (0.3333333333333333d0 / (n ** 3.0d0)) + ((((n + (-0.5d0)) / n) / n) + ((-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.0005) {
		tmp = (0.3333333333333333 / Math.pow(N, 3.0)) + ((((N + -0.5) / N) / N) + (-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.0005:
		tmp = (0.3333333333333333 / math.pow(N, 3.0)) + ((((N + -0.5) / N) / N) + (-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.0005)
		tmp = Float64(Float64(0.3333333333333333 / (N ^ 3.0)) + Float64(Float64(Float64(Float64(N + -0.5) / N) / N) + 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.0005)
		tmp = (0.3333333333333333 / (N ^ 3.0)) + ((((N + -0.5) / N) / N) + (-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.0005], N[(N[(0.3333333333333333 / N[Power[N, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(N[(N[(N + -0.5), $MachinePrecision] / N), $MachinePrecision] / N), $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.0005:\\
\;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \left(\frac{\frac{N + -0.5}{N}}{N} + \frac{-0.25}{{N}^{4}}\right)\\

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


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

    1. Initial program 6.3%

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

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

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

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

        \[\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-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-log4.6%

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

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

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

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

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

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

      \[\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) - \left(0.25 \cdot \frac{1}{{N}^{4}} + 0.5 \cdot \frac{1}{{N}^{2}}\right)} \]
    7. Simplified52.3%

      \[\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-identity52.3%

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

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

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

      \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \left(\color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} + \frac{-0.25}{{N}^{4}}\right) \]
    10. Step-by-step derivation
      1. associate-*l/100.0%

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

        \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{\color{blue}{\frac{N + -0.5}{N}}}{N} + \frac{-0.25}{{N}^{4}}\right) \]
    11. Applied egg-rr100.0%

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

    if 5.0000000000000001e-4 < (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N))

    1. Initial program 99.9%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \log \left(\frac{N + 1}{\color{blue}{e^{\log \left(1 + \mathsf{expm1}\left(\log N\right)\right)}}}\right) \]
      9. log1p-udef8.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.9%

        \[\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)} \]
    6. Step-by-step derivation
      1. clear-num99.9%

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

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

      \[\leadsto \color{blue}{-\log \left(\frac{N}{N + 1}\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 0.0005:\\ \;\;\;\;\frac{0.3333333333333333}{{N}^{3}} + \left(\frac{\frac{N + -0.5}{N}}{N} + \frac{-0.25}{{N}^{4}}\right)\\ \mathbf{else}:\\ \;\;\;\;-\log \left(\frac{N}{N + 1}\right)\\ \end{array} \]

Alternative 2: 99.2% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 0:\\ \;\;\;\;\frac{\frac{N + -0.5}{N}}{N}\\ \mathbf{else}:\\ \;\;\;\;-\log \left(\frac{N}{N + 1}\right)\\ \end{array} \end{array} \]
(FPCore (N)
 :precision binary64
 (if (<= (- (log (+ N 1.0)) (log N)) 0.0)
   (/ (/ (+ N -0.5) N) N)
   (- (log (/ N (+ N 1.0))))))
double code(double N) {
	double tmp;
	if ((log((N + 1.0)) - log(N)) <= 0.0) {
		tmp = ((N + -0.5) / 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.0d0) then
        tmp = ((n + (-0.5d0)) / 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.0) {
		tmp = ((N + -0.5) / 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.0:
		tmp = ((N + -0.5) / 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.0)
		tmp = Float64(Float64(Float64(N + -0.5) / 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.0)
		tmp = ((N + -0.5) / 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.0], N[(N[(N[(N + -0.5), $MachinePrecision] / N), $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:\\
\;\;\;\;\frac{\frac{N + -0.5}{N}}{N}\\

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


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

    1. Initial program 5.7%

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

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

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

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

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

        \[\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.7%

        \[\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.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\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/35.6%

        \[\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-eval35.6%

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

        \[\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-out35.7%

        \[\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-out35.7%

        \[\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-eval35.7%

        \[\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-in35.7%

        \[\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-out35.7%

        \[\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-neg35.7%

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

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

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

        \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1 \cdot \left(N + -0.5\right)}{\color{blue}{N \cdot N}} + \frac{-0.25}{{N}^{4}}\right) \]
      3. times-frac100.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) \]
    10. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    11. Step-by-step derivation
      1. associate-*l/100.0%

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

        \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{\color{blue}{\frac{N + -0.5}{N}}}{N} + \frac{-0.25}{{N}^{4}}\right) \]
    12. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\frac{\frac{N + -0.5}{N}}{N}} \]

    if 0.0 < (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N))

    1. Initial program 99.7%

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

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

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

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

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u8.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-udef8.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-log8.8%

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

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

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

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

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

        \[\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.8%

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

      \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
    6. Step-by-step derivation
      1. clear-num99.8%

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

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

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

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

Alternative 3: 99.8% accurate, 1.9× speedup?

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

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

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


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

    1. Initial program 99.7%

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

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

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

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

        \[\leadsto \color{blue}{\log \left(e^{\mathsf{log1p}\left(N\right)}\right)} - \log N \]
      2. log1p-expm1-u8.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-udef8.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-log8.8%

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

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

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

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

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

        \[\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.8%

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

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

    if 205000 < N

    1. Initial program 5.7%

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

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

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

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

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

        \[\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.7%

        \[\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.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\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/35.6%

        \[\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-eval35.6%

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

        \[\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-out35.7%

        \[\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-out35.7%

        \[\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-eval35.7%

        \[\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-in35.7%

        \[\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-out35.7%

        \[\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-neg35.7%

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

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

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

        \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{1 \cdot \left(N + -0.5\right)}{\color{blue}{N \cdot N}} + \frac{-0.25}{{N}^{4}}\right) \]
      3. times-frac100.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) \]
    10. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    11. Step-by-step derivation
      1. associate-*l/100.0%

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

        \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{\color{blue}{\frac{N + -0.5}{N}}}{N} + \frac{-0.25}{{N}^{4}}\right) \]
    12. Applied egg-rr100.0%

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

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

Alternative 4: 99.1% accurate, 2.0× speedup?

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

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

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


\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 98.7%

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

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

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

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

    if 0.900000000000000022 < 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.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\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/35.9%

        \[\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-eval35.9%

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

        \[\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-out35.9%

        \[\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-out35.9%

        \[\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-eval35.9%

        \[\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-in35.9%

        \[\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-out35.9%

        \[\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-neg35.9%

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

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

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

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

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

      \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    11. Step-by-step derivation
      1. associate-*l/99.6%

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

        \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{\color{blue}{\frac{N + -0.5}{N}}}{N} + \frac{-0.25}{{N}^{4}}\right) \]
    12. Applied egg-rr99.2%

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

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

Alternative 5: 98.7% accurate, 2.0× speedup?

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

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

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


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

    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 97.4%

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

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

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

    if 0.680000000000000049 < 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.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\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/35.9%

        \[\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-eval35.9%

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

        \[\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-out35.9%

        \[\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-out35.9%

        \[\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-eval35.9%

        \[\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-in35.9%

        \[\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-out35.9%

        \[\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-neg35.9%

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

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

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

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

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

      \[\leadsto \color{blue}{\frac{1}{N} \cdot \frac{N + -0.5}{N}} \]
    11. Step-by-step derivation
      1. associate-*l/99.6%

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

        \[\leadsto \frac{0.3333333333333333}{{N}^{3}} + \left(\frac{\color{blue}{\frac{N + -0.5}{N}}}{N} + \frac{-0.25}{{N}^{4}}\right) \]
    12. Applied egg-rr99.2%

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

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

Alternative 6: 51.4% 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 54.9%

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

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

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

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

    \[\leadsto \color{blue}{\frac{1}{N}} \]
  5. Final simplification50.9%

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

Alternative 7: 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 54.9%

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

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

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

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

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

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

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

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

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

    \[\leadsto N \]

Reproduce

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