logs (example 3.8)

Percentage Accurate: 1.6% → 100.0%
Time: 8.9s
Alternatives: 4
Speedup: 213.0×

Specification

?
\[n > 6.8 \cdot 10^{+15}\]
\[\begin{array}{l} \\ \left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \end{array} \]
(FPCore (n)
 :precision binary64
 (- (- (* (+ n 1.0) (log (+ n 1.0))) (* n (log n))) 1.0))
double code(double n) {
	return (((n + 1.0) * log((n + 1.0))) - (n * log(n))) - 1.0;
}
real(8) function code(n)
    real(8), intent (in) :: n
    code = (((n + 1.0d0) * log((n + 1.0d0))) - (n * log(n))) - 1.0d0
end function
public static double code(double n) {
	return (((n + 1.0) * Math.log((n + 1.0))) - (n * Math.log(n))) - 1.0;
}
def code(n):
	return (((n + 1.0) * math.log((n + 1.0))) - (n * math.log(n))) - 1.0
function code(n)
	return Float64(Float64(Float64(Float64(n + 1.0) * log(Float64(n + 1.0))) - Float64(n * log(n))) - 1.0)
end
function tmp = code(n)
	tmp = (((n + 1.0) * log((n + 1.0))) - (n * log(n))) - 1.0;
end
code[n_] := N[(N[(N[(N[(n + 1.0), $MachinePrecision] * N[Log[N[(n + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - N[(n * N[Log[n], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1
\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 4 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: 1.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \end{array} \]
(FPCore (n)
 :precision binary64
 (- (- (* (+ n 1.0) (log (+ n 1.0))) (* n (log n))) 1.0))
double code(double n) {
	return (((n + 1.0) * log((n + 1.0))) - (n * log(n))) - 1.0;
}
real(8) function code(n)
    real(8), intent (in) :: n
    code = (((n + 1.0d0) * log((n + 1.0d0))) - (n * log(n))) - 1.0d0
end function
public static double code(double n) {
	return (((n + 1.0) * Math.log((n + 1.0))) - (n * Math.log(n))) - 1.0;
}
def code(n):
	return (((n + 1.0) * math.log((n + 1.0))) - (n * math.log(n))) - 1.0
function code(n)
	return Float64(Float64(Float64(Float64(n + 1.0) * log(Float64(n + 1.0))) - Float64(n * log(n))) - 1.0)
end
function tmp = code(n)
	tmp = (((n + 1.0) * log((n + 1.0))) - (n * log(n))) - 1.0;
end
code[n_] := N[(N[(N[(N[(n + 1.0), $MachinePrecision] * N[Log[N[(n + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - N[(n * N[Log[n], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1
\end{array}

Alternative 1: 100.0% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \log n \end{array} \]
(FPCore (n) :precision binary64 (log n))
double code(double n) {
	return log(n);
}
real(8) function code(n)
    real(8), intent (in) :: n
    code = log(n)
end function
public static double code(double n) {
	return Math.log(n);
}
def code(n):
	return math.log(n)
function code(n)
	return log(n)
end
function tmp = code(n)
	tmp = log(n);
end
code[n_] := N[Log[n], $MachinePrecision]
\begin{array}{l}

\\
\log n
\end{array}
Derivation
  1. Initial program 1.6%

    \[\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \]
  2. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) + \color{blue}{\left(\mathsf{neg}\left(1\right)\right)} \]
    2. +-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \color{blue}{\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right)} \]
    3. sub-negN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \left(\left(n + 1\right) \cdot \log \left(n + 1\right) + \color{blue}{\left(\mathsf{neg}\left(n \cdot \log n\right)\right)}\right) \]
    4. +-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \left(\left(\mathsf{neg}\left(n \cdot \log n\right)\right) + \color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)}\right) \]
    5. associate-+r+N/A

      \[\leadsto \left(\left(\mathsf{neg}\left(1\right)\right) + \left(\mathsf{neg}\left(n \cdot \log n\right)\right)\right) + \color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)} \]
    6. unsub-negN/A

      \[\leadsto \left(\left(\mathsf{neg}\left(1\right)\right) - n \cdot \log n\right) + \color{blue}{\left(n + 1\right)} \cdot \log \left(n + 1\right) \]
    7. associate-+l-N/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) - \color{blue}{\left(n \cdot \log n - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)} \]
    8. --lowering--.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(\left(\mathsf{neg}\left(1\right)\right), \color{blue}{\left(n \cdot \log n - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)}\right) \]
    9. metadata-evalN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(\color{blue}{n \cdot \log n} - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right) \]
    10. sub-negN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \log n + \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right)}\right)\right) \]
    11. +-lowering-+.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\left(n \cdot \log n\right), \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right)}\right)\right) \]
    12. *-lowering-*.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \log n\right), \left(\mathsf{neg}\left(\color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)}\right)\right)\right)\right) \]
    13. log-lowering-log.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\mathsf{neg}\left(\left(n + 1\right) \cdot \color{blue}{\log \left(n + 1\right)}\right)\right)\right)\right) \]
    14. *-commutativeN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\mathsf{neg}\left(\log \left(n + 1\right) \cdot \left(n + 1\right)\right)\right)\right)\right) \]
    15. distribute-rgt-neg-inN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\log \left(n + 1\right) \cdot \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right)\right)\right)}\right)\right)\right) \]
    16. *-lowering-*.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \mathsf{*.f64}\left(\log \left(n + 1\right), \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right)\right)\right)}\right)\right)\right) \]
  3. Simplified1.6%

    \[\leadsto \color{blue}{-1 - \left(n \cdot \log n + \mathsf{log1p}\left(n\right) \cdot \left(-1 - n\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in n around inf

    \[\leadsto \color{blue}{n \cdot \left(-1 \cdot \frac{1 + -1 \cdot \left(1 + -1 \cdot \log \left(\frac{1}{n}\right)\right)}{n} - \left(\log \left(\frac{1}{n}\right) + -1 \cdot \log \left(\frac{1}{n}\right)\right)\right)} \]
  6. Simplified99.9%

    \[\leadsto \color{blue}{n \cdot \frac{\log n}{n}} \]
  7. Taylor expanded in n around 0

    \[\leadsto \color{blue}{\log n} \]
  8. Step-by-step derivation
    1. log-lowering-log.f64100.0%

      \[\leadsto \mathsf{log.f64}\left(n\right) \]
  9. Simplified100.0%

    \[\leadsto \color{blue}{\log n} \]
  10. Add Preprocessing

Alternative 2: 5.0% accurate, 19.4× speedup?

\[\begin{array}{l} \\ \frac{0.25}{n} \cdot \left(\frac{1}{n} \cdot \frac{n}{0.5}\right) \end{array} \]
(FPCore (n) :precision binary64 (* (/ 0.25 n) (* (/ 1.0 n) (/ n 0.5))))
double code(double n) {
	return (0.25 / n) * ((1.0 / n) * (n / 0.5));
}
real(8) function code(n)
    real(8), intent (in) :: n
    code = (0.25d0 / n) * ((1.0d0 / n) * (n / 0.5d0))
end function
public static double code(double n) {
	return (0.25 / n) * ((1.0 / n) * (n / 0.5));
}
def code(n):
	return (0.25 / n) * ((1.0 / n) * (n / 0.5))
function code(n)
	return Float64(Float64(0.25 / n) * Float64(Float64(1.0 / n) * Float64(n / 0.5)))
end
function tmp = code(n)
	tmp = (0.25 / n) * ((1.0 / n) * (n / 0.5));
end
code[n_] := N[(N[(0.25 / n), $MachinePrecision] * N[(N[(1.0 / n), $MachinePrecision] * N[(n / 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{0.25}{n} \cdot \left(\frac{1}{n} \cdot \frac{n}{0.5}\right)
\end{array}
Derivation
  1. Initial program 1.6%

    \[\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \]
  2. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) + \color{blue}{\left(\mathsf{neg}\left(1\right)\right)} \]
    2. +-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \color{blue}{\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right)} \]
    3. sub-negN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \left(\left(n + 1\right) \cdot \log \left(n + 1\right) + \color{blue}{\left(\mathsf{neg}\left(n \cdot \log n\right)\right)}\right) \]
    4. +-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \left(\left(\mathsf{neg}\left(n \cdot \log n\right)\right) + \color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)}\right) \]
    5. associate-+r+N/A

      \[\leadsto \left(\left(\mathsf{neg}\left(1\right)\right) + \left(\mathsf{neg}\left(n \cdot \log n\right)\right)\right) + \color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)} \]
    6. unsub-negN/A

      \[\leadsto \left(\left(\mathsf{neg}\left(1\right)\right) - n \cdot \log n\right) + \color{blue}{\left(n + 1\right)} \cdot \log \left(n + 1\right) \]
    7. associate-+l-N/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) - \color{blue}{\left(n \cdot \log n - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)} \]
    8. --lowering--.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(\left(\mathsf{neg}\left(1\right)\right), \color{blue}{\left(n \cdot \log n - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)}\right) \]
    9. metadata-evalN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(\color{blue}{n \cdot \log n} - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right) \]
    10. sub-negN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \log n + \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right)}\right)\right) \]
    11. +-lowering-+.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\left(n \cdot \log n\right), \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right)}\right)\right) \]
    12. *-lowering-*.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \log n\right), \left(\mathsf{neg}\left(\color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)}\right)\right)\right)\right) \]
    13. log-lowering-log.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\mathsf{neg}\left(\left(n + 1\right) \cdot \color{blue}{\log \left(n + 1\right)}\right)\right)\right)\right) \]
    14. *-commutativeN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\mathsf{neg}\left(\log \left(n + 1\right) \cdot \left(n + 1\right)\right)\right)\right)\right) \]
    15. distribute-rgt-neg-inN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\log \left(n + 1\right) \cdot \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right)\right)\right)}\right)\right)\right) \]
    16. *-lowering-*.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \mathsf{*.f64}\left(\log \left(n + 1\right), \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right)\right)\right)}\right)\right)\right) \]
  3. Simplified1.6%

    \[\leadsto \color{blue}{-1 - \left(n \cdot \log n + \mathsf{log1p}\left(n\right) \cdot \left(-1 - n\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in n around inf

    \[\leadsto \mathsf{\_.f64}\left(-1, \color{blue}{\left(n \cdot \left(\left(\log \left(\frac{1}{n}\right) + \left(-1 \cdot \log \left(\frac{1}{n}\right) + -1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right)\right) - \frac{\frac{1}{2}}{{n}^{2}}\right)\right)}\right) \]
  6. Step-by-step derivation
    1. associate-+r+N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \left(\left(\left(\log \left(\frac{1}{n}\right) + -1 \cdot \log \left(\frac{1}{n}\right)\right) + -1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right) - \frac{\color{blue}{\frac{1}{2}}}{{n}^{2}}\right)\right)\right) \]
    2. distribute-rgt1-inN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \left(\left(\left(-1 + 1\right) \cdot \log \left(\frac{1}{n}\right) + -1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right) - \frac{\frac{1}{2}}{{n}^{2}}\right)\right)\right) \]
    3. metadata-evalN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \left(\left(0 \cdot \log \left(\frac{1}{n}\right) + -1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right) - \frac{\frac{1}{2}}{{n}^{2}}\right)\right)\right) \]
    4. mul0-lftN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \left(\left(0 + -1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right) - \frac{\frac{1}{2}}{{n}^{2}}\right)\right)\right) \]
    5. +-lft-identityN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \left(-1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n} - \frac{\color{blue}{\frac{1}{2}}}{{n}^{2}}\right)\right)\right) \]
    6. *-lowering-*.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{*.f64}\left(n, \color{blue}{\left(-1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n} - \frac{\frac{1}{2}}{{n}^{2}}\right)}\right)\right) \]
    7. sub-negN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{*.f64}\left(n, \left(-1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n} + \color{blue}{\left(\mathsf{neg}\left(\frac{\frac{1}{2}}{{n}^{2}}\right)\right)}\right)\right)\right) \]
    8. +-lowering-+.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{*.f64}\left(n, \mathsf{+.f64}\left(\left(-1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right), \color{blue}{\left(\mathsf{neg}\left(\frac{\frac{1}{2}}{{n}^{2}}\right)\right)}\right)\right)\right) \]
  7. Simplified99.9%

    \[\leadsto -1 - \color{blue}{n \cdot \left(\frac{-1 - \log n}{n} + \frac{-0.5}{n \cdot n}\right)} \]
  8. Taylor expanded in n around 0

    \[\leadsto \color{blue}{\frac{\frac{1}{2}}{n}} \]
  9. Step-by-step derivation
    1. /-lowering-/.f644.9%

      \[\leadsto \mathsf{/.f64}\left(\frac{1}{2}, \color{blue}{n}\right) \]
  10. Simplified4.9%

    \[\leadsto \color{blue}{\frac{0.5}{n}} \]
  11. Step-by-step derivation
    1. frac-2negN/A

      \[\leadsto \frac{\mathsf{neg}\left(\frac{1}{2}\right)}{\color{blue}{\mathsf{neg}\left(n\right)}} \]
    2. neg-sub0N/A

      \[\leadsto \frac{\mathsf{neg}\left(\frac{1}{2}\right)}{0 - \color{blue}{n}} \]
    3. flip--N/A

      \[\leadsto \frac{\mathsf{neg}\left(\frac{1}{2}\right)}{\frac{0 \cdot 0 - n \cdot n}{\color{blue}{0 + n}}} \]
    4. +-lft-identityN/A

      \[\leadsto \frac{\mathsf{neg}\left(\frac{1}{2}\right)}{\frac{0 \cdot 0 - n \cdot n}{n}} \]
    5. associate-/r/N/A

      \[\leadsto \frac{\mathsf{neg}\left(\frac{1}{2}\right)}{0 \cdot 0 - n \cdot n} \cdot \color{blue}{n} \]
    6. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\left(\frac{\mathsf{neg}\left(\frac{1}{2}\right)}{0 \cdot 0 - n \cdot n}\right), \color{blue}{n}\right) \]
    7. /-lowering-/.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\left(\mathsf{neg}\left(\frac{1}{2}\right)\right), \left(0 \cdot 0 - n \cdot n\right)\right), n\right) \]
    8. metadata-evalN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \left(0 \cdot 0 - n \cdot n\right)\right), n\right) \]
    9. metadata-evalN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \left(0 - n \cdot n\right)\right), n\right) \]
    10. +-lft-identityN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \left(0 - n \cdot \left(0 + n\right)\right)\right), n\right) \]
    11. +-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \left(0 - n \cdot \left(n + 0\right)\right)\right), n\right) \]
    12. distribute-rgt-outN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \left(0 - \left(n \cdot n + 0 \cdot n\right)\right)\right), n\right) \]
    13. +-lft-identityN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \left(0 - \left(0 + \left(n \cdot n + 0 \cdot n\right)\right)\right)\right), n\right) \]
    14. metadata-evalN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \left(0 - \left(0 \cdot 0 + \left(n \cdot n + 0 \cdot n\right)\right)\right)\right), n\right) \]
    15. --lowering--.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{\_.f64}\left(0, \left(0 \cdot 0 + \left(n \cdot n + 0 \cdot n\right)\right)\right)\right), n\right) \]
    16. metadata-evalN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{\_.f64}\left(0, \left(0 + \left(n \cdot n + 0 \cdot n\right)\right)\right)\right), n\right) \]
    17. +-lft-identityN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{\_.f64}\left(0, \left(n \cdot n + 0 \cdot n\right)\right)\right), n\right) \]
    18. distribute-rgt-outN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{\_.f64}\left(0, \left(n \cdot \left(n + 0\right)\right)\right)\right), n\right) \]
    19. +-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{\_.f64}\left(0, \left(n \cdot \left(0 + n\right)\right)\right)\right), n\right) \]
    20. +-lft-identityN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{\_.f64}\left(0, \left(n \cdot n\right)\right)\right), n\right) \]
    21. *-lowering-*.f644.5%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{-1}{2}, \mathsf{\_.f64}\left(0, \mathsf{*.f64}\left(n, n\right)\right)\right), n\right) \]
  12. Applied egg-rr4.5%

    \[\leadsto \color{blue}{\frac{-0.5}{0 - n \cdot n} \cdot n} \]
  13. Applied egg-rr4.9%

    \[\leadsto \color{blue}{\frac{0.25}{n} \cdot \left(\frac{1}{n} \cdot \frac{n}{0.5}\right)} \]
  14. Add Preprocessing

Alternative 3: 5.0% accurate, 71.0× speedup?

\[\begin{array}{l} \\ \frac{0.5}{n} \end{array} \]
(FPCore (n) :precision binary64 (/ 0.5 n))
double code(double n) {
	return 0.5 / n;
}
real(8) function code(n)
    real(8), intent (in) :: n
    code = 0.5d0 / n
end function
public static double code(double n) {
	return 0.5 / n;
}
def code(n):
	return 0.5 / n
function code(n)
	return Float64(0.5 / n)
end
function tmp = code(n)
	tmp = 0.5 / n;
end
code[n_] := N[(0.5 / n), $MachinePrecision]
\begin{array}{l}

\\
\frac{0.5}{n}
\end{array}
Derivation
  1. Initial program 1.6%

    \[\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \]
  2. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) + \color{blue}{\left(\mathsf{neg}\left(1\right)\right)} \]
    2. +-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \color{blue}{\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right)} \]
    3. sub-negN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \left(\left(n + 1\right) \cdot \log \left(n + 1\right) + \color{blue}{\left(\mathsf{neg}\left(n \cdot \log n\right)\right)}\right) \]
    4. +-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \left(\left(\mathsf{neg}\left(n \cdot \log n\right)\right) + \color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)}\right) \]
    5. associate-+r+N/A

      \[\leadsto \left(\left(\mathsf{neg}\left(1\right)\right) + \left(\mathsf{neg}\left(n \cdot \log n\right)\right)\right) + \color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)} \]
    6. unsub-negN/A

      \[\leadsto \left(\left(\mathsf{neg}\left(1\right)\right) - n \cdot \log n\right) + \color{blue}{\left(n + 1\right)} \cdot \log \left(n + 1\right) \]
    7. associate-+l-N/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) - \color{blue}{\left(n \cdot \log n - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)} \]
    8. --lowering--.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(\left(\mathsf{neg}\left(1\right)\right), \color{blue}{\left(n \cdot \log n - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)}\right) \]
    9. metadata-evalN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(\color{blue}{n \cdot \log n} - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right) \]
    10. sub-negN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \log n + \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right)}\right)\right) \]
    11. +-lowering-+.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\left(n \cdot \log n\right), \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right)}\right)\right) \]
    12. *-lowering-*.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \log n\right), \left(\mathsf{neg}\left(\color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)}\right)\right)\right)\right) \]
    13. log-lowering-log.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\mathsf{neg}\left(\left(n + 1\right) \cdot \color{blue}{\log \left(n + 1\right)}\right)\right)\right)\right) \]
    14. *-commutativeN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\mathsf{neg}\left(\log \left(n + 1\right) \cdot \left(n + 1\right)\right)\right)\right)\right) \]
    15. distribute-rgt-neg-inN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\log \left(n + 1\right) \cdot \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right)\right)\right)}\right)\right)\right) \]
    16. *-lowering-*.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \mathsf{*.f64}\left(\log \left(n + 1\right), \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right)\right)\right)}\right)\right)\right) \]
  3. Simplified1.6%

    \[\leadsto \color{blue}{-1 - \left(n \cdot \log n + \mathsf{log1p}\left(n\right) \cdot \left(-1 - n\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in n around inf

    \[\leadsto \mathsf{\_.f64}\left(-1, \color{blue}{\left(n \cdot \left(\left(\log \left(\frac{1}{n}\right) + \left(-1 \cdot \log \left(\frac{1}{n}\right) + -1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right)\right) - \frac{\frac{1}{2}}{{n}^{2}}\right)\right)}\right) \]
  6. Step-by-step derivation
    1. associate-+r+N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \left(\left(\left(\log \left(\frac{1}{n}\right) + -1 \cdot \log \left(\frac{1}{n}\right)\right) + -1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right) - \frac{\color{blue}{\frac{1}{2}}}{{n}^{2}}\right)\right)\right) \]
    2. distribute-rgt1-inN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \left(\left(\left(-1 + 1\right) \cdot \log \left(\frac{1}{n}\right) + -1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right) - \frac{\frac{1}{2}}{{n}^{2}}\right)\right)\right) \]
    3. metadata-evalN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \left(\left(0 \cdot \log \left(\frac{1}{n}\right) + -1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right) - \frac{\frac{1}{2}}{{n}^{2}}\right)\right)\right) \]
    4. mul0-lftN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \left(\left(0 + -1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right) - \frac{\frac{1}{2}}{{n}^{2}}\right)\right)\right) \]
    5. +-lft-identityN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \left(-1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n} - \frac{\color{blue}{\frac{1}{2}}}{{n}^{2}}\right)\right)\right) \]
    6. *-lowering-*.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{*.f64}\left(n, \color{blue}{\left(-1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n} - \frac{\frac{1}{2}}{{n}^{2}}\right)}\right)\right) \]
    7. sub-negN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{*.f64}\left(n, \left(-1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n} + \color{blue}{\left(\mathsf{neg}\left(\frac{\frac{1}{2}}{{n}^{2}}\right)\right)}\right)\right)\right) \]
    8. +-lowering-+.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{*.f64}\left(n, \mathsf{+.f64}\left(\left(-1 \cdot \frac{1 + -1 \cdot \log \left(\frac{1}{n}\right)}{n}\right), \color{blue}{\left(\mathsf{neg}\left(\frac{\frac{1}{2}}{{n}^{2}}\right)\right)}\right)\right)\right) \]
  7. Simplified99.9%

    \[\leadsto -1 - \color{blue}{n \cdot \left(\frac{-1 - \log n}{n} + \frac{-0.5}{n \cdot n}\right)} \]
  8. Taylor expanded in n around 0

    \[\leadsto \color{blue}{\frac{\frac{1}{2}}{n}} \]
  9. Step-by-step derivation
    1. /-lowering-/.f644.9%

      \[\leadsto \mathsf{/.f64}\left(\frac{1}{2}, \color{blue}{n}\right) \]
  10. Simplified4.9%

    \[\leadsto \color{blue}{\frac{0.5}{n}} \]
  11. Add Preprocessing

Alternative 4: 3.1% accurate, 213.0× speedup?

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

\\
0
\end{array}
Derivation
  1. Initial program 1.6%

    \[\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \]
  2. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) + \color{blue}{\left(\mathsf{neg}\left(1\right)\right)} \]
    2. +-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \color{blue}{\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right)} \]
    3. sub-negN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \left(\left(n + 1\right) \cdot \log \left(n + 1\right) + \color{blue}{\left(\mathsf{neg}\left(n \cdot \log n\right)\right)}\right) \]
    4. +-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) + \left(\left(\mathsf{neg}\left(n \cdot \log n\right)\right) + \color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)}\right) \]
    5. associate-+r+N/A

      \[\leadsto \left(\left(\mathsf{neg}\left(1\right)\right) + \left(\mathsf{neg}\left(n \cdot \log n\right)\right)\right) + \color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)} \]
    6. unsub-negN/A

      \[\leadsto \left(\left(\mathsf{neg}\left(1\right)\right) - n \cdot \log n\right) + \color{blue}{\left(n + 1\right)} \cdot \log \left(n + 1\right) \]
    7. associate-+l-N/A

      \[\leadsto \left(\mathsf{neg}\left(1\right)\right) - \color{blue}{\left(n \cdot \log n - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)} \]
    8. --lowering--.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(\left(\mathsf{neg}\left(1\right)\right), \color{blue}{\left(n \cdot \log n - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)}\right) \]
    9. metadata-evalN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(\color{blue}{n \cdot \log n} - \left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right) \]
    10. sub-negN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \left(n \cdot \log n + \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right)}\right)\right) \]
    11. +-lowering-+.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\left(n \cdot \log n\right), \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right) \cdot \log \left(n + 1\right)\right)\right)}\right)\right) \]
    12. *-lowering-*.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \log n\right), \left(\mathsf{neg}\left(\color{blue}{\left(n + 1\right) \cdot \log \left(n + 1\right)}\right)\right)\right)\right) \]
    13. log-lowering-log.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\mathsf{neg}\left(\left(n + 1\right) \cdot \color{blue}{\log \left(n + 1\right)}\right)\right)\right)\right) \]
    14. *-commutativeN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\mathsf{neg}\left(\log \left(n + 1\right) \cdot \left(n + 1\right)\right)\right)\right)\right) \]
    15. distribute-rgt-neg-inN/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \left(\log \left(n + 1\right) \cdot \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right)\right)\right)}\right)\right)\right) \]
    16. *-lowering-*.f64N/A

      \[\leadsto \mathsf{\_.f64}\left(-1, \mathsf{+.f64}\left(\mathsf{*.f64}\left(n, \mathsf{log.f64}\left(n\right)\right), \mathsf{*.f64}\left(\log \left(n + 1\right), \color{blue}{\left(\mathsf{neg}\left(\left(n + 1\right)\right)\right)}\right)\right)\right) \]
  3. Simplified1.6%

    \[\leadsto \color{blue}{-1 - \left(n \cdot \log n + \mathsf{log1p}\left(n\right) \cdot \left(-1 - n\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in n around inf

    \[\leadsto \color{blue}{-1 \cdot \left(n \cdot \left(\log \left(\frac{1}{n}\right) + -1 \cdot \log \left(\frac{1}{n}\right)\right)\right)} \]
  6. Step-by-step derivation
    1. associate-*r*N/A

      \[\leadsto \left(-1 \cdot n\right) \cdot \color{blue}{\left(\log \left(\frac{1}{n}\right) + -1 \cdot \log \left(\frac{1}{n}\right)\right)} \]
    2. distribute-rgt1-inN/A

      \[\leadsto \left(-1 \cdot n\right) \cdot \left(\left(-1 + 1\right) \cdot \color{blue}{\log \left(\frac{1}{n}\right)}\right) \]
    3. metadata-evalN/A

      \[\leadsto \left(-1 \cdot n\right) \cdot \left(0 \cdot \log \color{blue}{\left(\frac{1}{n}\right)}\right) \]
    4. mul0-lftN/A

      \[\leadsto \left(-1 \cdot n\right) \cdot 0 \]
    5. mul0-rgt3.1%

      \[\leadsto 0 \]
  7. Simplified3.1%

    \[\leadsto \color{blue}{0} \]
  8. Add Preprocessing

Developer Target 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(n + 1\right) - \left(\frac{1}{2 \cdot n} - \left(\frac{1}{3 \cdot \left(n \cdot n\right)} - \frac{4}{{n}^{3}}\right)\right) \end{array} \]
(FPCore (n)
 :precision binary64
 (-
  (log (+ n 1.0))
  (- (/ 1.0 (* 2.0 n)) (- (/ 1.0 (* 3.0 (* n n))) (/ 4.0 (pow n 3.0))))))
double code(double n) {
	return log((n + 1.0)) - ((1.0 / (2.0 * n)) - ((1.0 / (3.0 * (n * n))) - (4.0 / pow(n, 3.0))));
}
real(8) function code(n)
    real(8), intent (in) :: n
    code = log((n + 1.0d0)) - ((1.0d0 / (2.0d0 * n)) - ((1.0d0 / (3.0d0 * (n * n))) - (4.0d0 / (n ** 3.0d0))))
end function
public static double code(double n) {
	return Math.log((n + 1.0)) - ((1.0 / (2.0 * n)) - ((1.0 / (3.0 * (n * n))) - (4.0 / Math.pow(n, 3.0))));
}
def code(n):
	return math.log((n + 1.0)) - ((1.0 / (2.0 * n)) - ((1.0 / (3.0 * (n * n))) - (4.0 / math.pow(n, 3.0))))
function code(n)
	return Float64(log(Float64(n + 1.0)) - Float64(Float64(1.0 / Float64(2.0 * n)) - Float64(Float64(1.0 / Float64(3.0 * Float64(n * n))) - Float64(4.0 / (n ^ 3.0)))))
end
function tmp = code(n)
	tmp = log((n + 1.0)) - ((1.0 / (2.0 * n)) - ((1.0 / (3.0 * (n * n))) - (4.0 / (n ^ 3.0))));
end
code[n_] := N[(N[Log[N[(n + 1.0), $MachinePrecision]], $MachinePrecision] - N[(N[(1.0 / N[(2.0 * n), $MachinePrecision]), $MachinePrecision] - N[(N[(1.0 / N[(3.0 * N[(n * n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(4.0 / N[Power[n, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\log \left(n + 1\right) - \left(\frac{1}{2 \cdot n} - \left(\frac{1}{3 \cdot \left(n \cdot n\right)} - \frac{4}{{n}^{3}}\right)\right)
\end{array}

Reproduce

?
herbie shell --seed 2024161 
(FPCore (n)
  :name "logs (example 3.8)"
  :precision binary64
  :pre (> n 6.8e+15)

  :alt
  (! :herbie-platform default (- (log (+ n 1)) (- (/ 1 (* 2 n)) (- (/ 1 (* 3 (* n n))) (/ 4 (pow n 3))))))

  (- (- (* (+ n 1.0) (log (+ n 1.0))) (* n (log n))) 1.0))