logs (example 3.8)

Percentage Accurate: 1.5% → 98.9%
Time: 14.4s
Alternatives: 5
Speedup: 1.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 5 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.5% 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: 98.9% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \mathsf{log1p}\left(n\right) + \left(n \cdot \mathsf{log1p}\left(n\right) - \mathsf{fma}\left(n, \log n, 1\right)\right) \end{array} \]
(FPCore (n)
 :precision binary64
 (+ (log1p n) (- (* n (log1p n)) (fma n (log n) 1.0))))
double code(double n) {
	return log1p(n) + ((n * log1p(n)) - fma(n, log(n), 1.0));
}
function code(n)
	return Float64(log1p(n) + Float64(Float64(n * log1p(n)) - fma(n, log(n), 1.0)))
end
code[n_] := N[(N[Log[1 + n], $MachinePrecision] + N[(N[(n * N[Log[1 + n], $MachinePrecision]), $MachinePrecision] - N[(n * N[Log[n], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{log1p}\left(n\right) + \left(n \cdot \mathsf{log1p}\left(n\right) - \mathsf{fma}\left(n, \log n, 1\right)\right)
\end{array}
Derivation
  1. Initial program 1.5%

    \[\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. associate--l-N/A

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

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

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

      \[\leadsto \color{blue}{\left(\log \left(n + 1\right) \cdot 1 + \log \left(n + 1\right) \cdot n\right)} - \left(n \cdot \log n + 1\right) \]
    5. *-rgt-identityN/A

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

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

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

      \[\leadsto \log \color{blue}{\left(1 + n\right)} + \left(\log \left(n + 1\right) \cdot n - \left(n \cdot \log n + 1\right)\right) \]
    9. accelerator-lowering-log1p.f64N/A

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

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

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

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

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

      \[\leadsto \mathsf{log1p}\left(n\right) + \left(\mathsf{log1p}\left(n\right) \cdot n - \color{blue}{\mathsf{fma}\left(n, \log n, 1\right)}\right) \]
    15. log-lowering-log.f6498.4

      \[\leadsto \mathsf{log1p}\left(n\right) + \left(\mathsf{log1p}\left(n\right) \cdot n - \mathsf{fma}\left(n, \color{blue}{\log n}, 1\right)\right) \]
  4. Applied egg-rr98.4%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(n\right) + \left(\mathsf{log1p}\left(n\right) \cdot n - \mathsf{fma}\left(n, \log n, 1\right)\right)} \]
  5. Final simplification98.4%

    \[\leadsto \mathsf{log1p}\left(n\right) + \left(n \cdot \mathsf{log1p}\left(n\right) - \mathsf{fma}\left(n, \log n, 1\right)\right) \]
  6. Add Preprocessing

Alternative 2: 30.9% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \frac{1}{\frac{1}{-1 + \frac{1}{\frac{1}{\mathsf{fma}\left(n, \log \left(\frac{n + 1}{n}\right), \mathsf{log1p}\left(n\right)\right)}}}} \end{array} \]
(FPCore (n)
 :precision binary64
 (/
  1.0
  (/ 1.0 (+ -1.0 (/ 1.0 (/ 1.0 (fma n (log (/ (+ n 1.0) n)) (log1p n))))))))
double code(double n) {
	return 1.0 / (1.0 / (-1.0 + (1.0 / (1.0 / fma(n, log(((n + 1.0) / n)), log1p(n))))));
}
function code(n)
	return Float64(1.0 / Float64(1.0 / Float64(-1.0 + Float64(1.0 / Float64(1.0 / fma(n, log(Float64(Float64(n + 1.0) / n)), log1p(n)))))))
end
code[n_] := N[(1.0 / N[(1.0 / N[(-1.0 + N[(1.0 / N[(1.0 / N[(n * N[Log[N[(N[(n + 1.0), $MachinePrecision] / n), $MachinePrecision]], $MachinePrecision] + N[Log[1 + n], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{\frac{1}{-1 + \frac{1}{\frac{1}{\mathsf{fma}\left(n, \log \left(\frac{n + 1}{n}\right), \mathsf{log1p}\left(n\right)\right)}}}}
\end{array}
Derivation
  1. Initial program 1.5%

    \[\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. associate--l-N/A

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

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

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

      \[\leadsto \color{blue}{\left(\log \left(n + 1\right) \cdot 1 + \log \left(n + 1\right) \cdot n\right)} - \left(n \cdot \log n + 1\right) \]
    5. *-rgt-identityN/A

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

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

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

      \[\leadsto \log \color{blue}{\left(1 + n\right)} + \left(\log \left(n + 1\right) \cdot n - \left(n \cdot \log n + 1\right)\right) \]
    9. accelerator-lowering-log1p.f64N/A

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

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

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

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

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

      \[\leadsto \mathsf{log1p}\left(n\right) + \left(\mathsf{log1p}\left(n\right) \cdot n - \color{blue}{\mathsf{fma}\left(n, \log n, 1\right)}\right) \]
    15. log-lowering-log.f6498.4

      \[\leadsto \mathsf{log1p}\left(n\right) + \left(\mathsf{log1p}\left(n\right) \cdot n - \mathsf{fma}\left(n, \color{blue}{\log n}, 1\right)\right) \]
  4. Applied egg-rr98.4%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(n\right) + \left(\mathsf{log1p}\left(n\right) \cdot n - \mathsf{fma}\left(n, \log n, 1\right)\right)} \]
  5. Step-by-step derivation
    1. flip-+N/A

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

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

      \[\leadsto \color{blue}{\left(\log \left(1 + n\right) \cdot \log \left(1 + n\right) - \left(\log \left(1 + n\right) \cdot n - \left(n \cdot \log n + 1\right)\right) \cdot \left(\log \left(1 + n\right) \cdot n - \left(n \cdot \log n + 1\right)\right)\right) \cdot \frac{1}{\log \left(1 + n\right) - \left(\log \left(1 + n\right) \cdot n - \left(n \cdot \log n + 1\right)\right)}} \]
  6. Applied egg-rr31.0%

    \[\leadsto \color{blue}{\left({\left(\mathsf{log1p}\left(n\right)\right)}^{2} - {\left(\mathsf{fma}\left(n, \log \left(\frac{1 + n}{n}\right), -1\right)\right)}^{2}\right) \cdot \frac{1}{\mathsf{log1p}\left(n\right) - \mathsf{fma}\left(n, \log \left(\frac{1 + n}{n}\right), -1\right)}} \]
  7. Step-by-step derivation
    1. *-commutativeN/A

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

      \[\leadsto \color{blue}{\frac{1}{\frac{\log \left(1 + n\right) - \left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right)}{{\log \left(1 + n\right)}^{2} - {\left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right)}^{2}}}} \]
    3. /-lowering-/.f64N/A

      \[\leadsto \color{blue}{\frac{1}{\frac{\log \left(1 + n\right) - \left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right)}{{\log \left(1 + n\right)}^{2} - {\left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right)}^{2}}}} \]
    4. clear-numN/A

      \[\leadsto \frac{1}{\color{blue}{\frac{1}{\frac{{\log \left(1 + n\right)}^{2} - {\left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right)}^{2}}{\log \left(1 + n\right) - \left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right)}}}} \]
    5. unpow2N/A

      \[\leadsto \frac{1}{\frac{1}{\frac{\color{blue}{\log \left(1 + n\right) \cdot \log \left(1 + n\right)} - {\left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right)}^{2}}{\log \left(1 + n\right) - \left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right)}}} \]
    6. unpow2N/A

      \[\leadsto \frac{1}{\frac{1}{\frac{\log \left(1 + n\right) \cdot \log \left(1 + n\right) - \color{blue}{\left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right) \cdot \left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right)}}{\log \left(1 + n\right) - \left(n \cdot \log \left(\frac{1 + n}{n}\right) + -1\right)}}} \]
  8. Applied egg-rr31.0%

    \[\leadsto \color{blue}{\frac{1}{\frac{1}{-1 + \mathsf{fma}\left(n, \log \left(\frac{1 + n}{n}\right), \mathsf{log1p}\left(n\right)\right)}}} \]
  9. Step-by-step derivation
    1. flip-+N/A

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

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

      \[\leadsto \frac{1}{\frac{1}{-1 + \color{blue}{\frac{1}{\frac{n \cdot \log \left(\frac{1 + n}{n}\right) - \log \left(1 + n\right)}{\left(n \cdot \log \left(\frac{1 + n}{n}\right)\right) \cdot \left(n \cdot \log \left(\frac{1 + n}{n}\right)\right) - \log \left(1 + n\right) \cdot \log \left(1 + n\right)}}}}} \]
    4. clear-numN/A

      \[\leadsto \frac{1}{\frac{1}{-1 + \frac{1}{\color{blue}{\frac{1}{\frac{\left(n \cdot \log \left(\frac{1 + n}{n}\right)\right) \cdot \left(n \cdot \log \left(\frac{1 + n}{n}\right)\right) - \log \left(1 + n\right) \cdot \log \left(1 + n\right)}{n \cdot \log \left(\frac{1 + n}{n}\right) - \log \left(1 + n\right)}}}}}} \]
    5. flip-+N/A

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

      \[\leadsto \frac{1}{\frac{1}{-1 + \frac{1}{\color{blue}{\frac{1}{n \cdot \log \left(\frac{1 + n}{n}\right) + \log \left(1 + n\right)}}}}} \]
    7. accelerator-lowering-fma.f64N/A

      \[\leadsto \frac{1}{\frac{1}{-1 + \frac{1}{\frac{1}{\color{blue}{\mathsf{fma}\left(n, \log \left(\frac{1 + n}{n}\right), \log \left(1 + n\right)\right)}}}}} \]
  10. Applied egg-rr31.0%

    \[\leadsto \frac{1}{\frac{1}{-1 + \color{blue}{\frac{1}{\frac{1}{\mathsf{fma}\left(n, \log \left(\frac{1 + n}{n}\right), \mathsf{log1p}\left(n\right)\right)}}}}} \]
  11. Final simplification31.0%

    \[\leadsto \frac{1}{\frac{1}{-1 + \frac{1}{\frac{1}{\mathsf{fma}\left(n, \log \left(\frac{n + 1}{n}\right), \mathsf{log1p}\left(n\right)\right)}}}} \]
  12. Add Preprocessing

Alternative 3: 30.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \mathsf{log1p}\left(n\right) + \mathsf{fma}\left(n, \log \left(\frac{n + 1}{n}\right), -1\right) \end{array} \]
(FPCore (n)
 :precision binary64
 (+ (log1p n) (fma n (log (/ (+ n 1.0) n)) -1.0)))
double code(double n) {
	return log1p(n) + fma(n, log(((n + 1.0) / n)), -1.0);
}
function code(n)
	return Float64(log1p(n) + fma(n, log(Float64(Float64(n + 1.0) / n)), -1.0))
end
code[n_] := N[(N[Log[1 + n], $MachinePrecision] + N[(n * N[Log[N[(N[(n + 1.0), $MachinePrecision] / n), $MachinePrecision]], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{log1p}\left(n\right) + \mathsf{fma}\left(n, \log \left(\frac{n + 1}{n}\right), -1\right)
\end{array}
Derivation
  1. Initial program 1.5%

    \[\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. associate--l-N/A

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

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

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

      \[\leadsto \color{blue}{\left(\log \left(n + 1\right) \cdot 1 + \log \left(n + 1\right) \cdot n\right)} - \left(n \cdot \log n + 1\right) \]
    5. *-rgt-identityN/A

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

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

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

      \[\leadsto \log \color{blue}{\left(1 + n\right)} + \left(\log \left(n + 1\right) \cdot n - \left(n \cdot \log n + 1\right)\right) \]
    9. accelerator-lowering-log1p.f64N/A

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

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

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

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

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

      \[\leadsto \mathsf{log1p}\left(n\right) + \left(\mathsf{log1p}\left(n\right) \cdot n - \color{blue}{\mathsf{fma}\left(n, \log n, 1\right)}\right) \]
    15. log-lowering-log.f6498.4

      \[\leadsto \mathsf{log1p}\left(n\right) + \left(\mathsf{log1p}\left(n\right) \cdot n - \mathsf{fma}\left(n, \color{blue}{\log n}, 1\right)\right) \]
  4. Applied egg-rr98.4%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(n\right) + \left(\mathsf{log1p}\left(n\right) \cdot n - \mathsf{fma}\left(n, \log n, 1\right)\right)} \]
  5. Step-by-step derivation
    1. +-commutativeN/A

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(n, \log \left(1 + n\right) - \log n, -1\right)} + \log \left(1 + n\right) \]
    9. +-commutativeN/A

      \[\leadsto \mathsf{fma}\left(n, \log \color{blue}{\left(n + 1\right)} - \log n, -1\right) + \log \left(1 + n\right) \]
    10. metadata-evalN/A

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

      \[\leadsto \mathsf{fma}\left(n, \log \color{blue}{\left(n - -1\right)} - \log n, -1\right) + \log \left(1 + n\right) \]
    12. diff-logN/A

      \[\leadsto \mathsf{fma}\left(n, \color{blue}{\log \left(\frac{n - -1}{n}\right)}, -1\right) + \log \left(1 + n\right) \]
    13. log-lowering-log.f64N/A

      \[\leadsto \mathsf{fma}\left(n, \color{blue}{\log \left(\frac{n - -1}{n}\right)}, -1\right) + \log \left(1 + n\right) \]
    14. /-lowering-/.f64N/A

      \[\leadsto \mathsf{fma}\left(n, \log \color{blue}{\left(\frac{n - -1}{n}\right)}, -1\right) + \log \left(1 + n\right) \]
    15. sub-negN/A

      \[\leadsto \mathsf{fma}\left(n, \log \left(\frac{\color{blue}{n + \left(\mathsf{neg}\left(-1\right)\right)}}{n}\right), -1\right) + \log \left(1 + n\right) \]
    16. metadata-evalN/A

      \[\leadsto \mathsf{fma}\left(n, \log \left(\frac{n + \color{blue}{1}}{n}\right), -1\right) + \log \left(1 + n\right) \]
    17. +-commutativeN/A

      \[\leadsto \mathsf{fma}\left(n, \log \left(\frac{\color{blue}{1 + n}}{n}\right), -1\right) + \log \left(1 + n\right) \]
    18. +-lowering-+.f64N/A

      \[\leadsto \mathsf{fma}\left(n, \log \left(\frac{\color{blue}{1 + n}}{n}\right), -1\right) + \log \left(1 + n\right) \]
  6. Applied egg-rr31.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(n, \log \left(\frac{1 + n}{n}\right), -1\right) + \mathsf{log1p}\left(n\right)} \]
  7. Final simplification31.0%

    \[\leadsto \mathsf{log1p}\left(n\right) + \mathsf{fma}\left(n, \log \left(\frac{n + 1}{n}\right), -1\right) \]
  8. Add Preprocessing

Alternative 4: 3.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ -1 + \mathsf{fma}\left(-\log n, n, \mathsf{log1p}\left(n\right) \cdot \left(n + 1\right)\right) \end{array} \]
(FPCore (n)
 :precision binary64
 (+ -1.0 (fma (- (log n)) n (* (log1p n) (+ n 1.0)))))
double code(double n) {
	return -1.0 + fma(-log(n), n, (log1p(n) * (n + 1.0)));
}
function code(n)
	return Float64(-1.0 + fma(Float64(-log(n)), n, Float64(log1p(n) * Float64(n + 1.0))))
end
code[n_] := N[(-1.0 + N[((-N[Log[n], $MachinePrecision]) * n + N[(N[Log[1 + n], $MachinePrecision] * N[(n + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
-1 + \mathsf{fma}\left(-\log n, n, \mathsf{log1p}\left(n\right) \cdot \left(n + 1\right)\right)
\end{array}
Derivation
  1. Initial program 1.5%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\log n\right), n, \left(n + 1\right) \cdot \log \color{blue}{\left(1 + n\right)}\right) - 1 \]
    11. accelerator-lowering-log1p.f643.2

      \[\leadsto \mathsf{fma}\left(-\log n, n, \left(n + 1\right) \cdot \color{blue}{\mathsf{log1p}\left(n\right)}\right) - 1 \]
  4. Applied egg-rr3.2%

    \[\leadsto \color{blue}{\mathsf{fma}\left(-\log n, n, \left(n + 1\right) \cdot \mathsf{log1p}\left(n\right)\right)} - 1 \]
  5. Final simplification3.2%

    \[\leadsto -1 + \mathsf{fma}\left(-\log n, n, \mathsf{log1p}\left(n\right) \cdot \left(n + 1\right)\right) \]
  6. Add Preprocessing

Alternative 5: 3.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(-\log n, n, \mathsf{fma}\left(n + 1, \mathsf{log1p}\left(n\right), -1\right)\right) \end{array} \]
(FPCore (n)
 :precision binary64
 (fma (- (log n)) n (fma (+ n 1.0) (log1p n) -1.0)))
double code(double n) {
	return fma(-log(n), n, fma((n + 1.0), log1p(n), -1.0));
}
function code(n)
	return fma(Float64(-log(n)), n, fma(Float64(n + 1.0), log1p(n), -1.0))
end
code[n_] := N[((-N[Log[n], $MachinePrecision]) * n + N[(N[(n + 1.0), $MachinePrecision] * N[Log[1 + n], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(-\log n, n, \mathsf{fma}\left(n + 1, \mathsf{log1p}\left(n\right), -1\right)\right)
\end{array}
Derivation
  1. Initial program 1.5%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\log n\right), n, \mathsf{fma}\left(n + 1, \log \color{blue}{\left(1 + n\right)}, \mathsf{neg}\left(1\right)\right)\right) \]
    13. accelerator-lowering-log1p.f64N/A

      \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\log n\right), n, \mathsf{fma}\left(n + 1, \color{blue}{\mathsf{log1p}\left(n\right)}, \mathsf{neg}\left(1\right)\right)\right) \]
    14. metadata-eval3.2

      \[\leadsto \mathsf{fma}\left(-\log n, n, \mathsf{fma}\left(n + 1, \mathsf{log1p}\left(n\right), \color{blue}{-1}\right)\right) \]
  4. Applied egg-rr3.2%

    \[\leadsto \color{blue}{\mathsf{fma}\left(-\log n, n, \mathsf{fma}\left(n + 1, \mathsf{log1p}\left(n\right), -1\right)\right)} \]
  5. Add Preprocessing

Developer Target 1: 100.0% accurate, 0.9× 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 2024207 
(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))