logs (example 3.8)

?

Percentage Accurate: 1.6% → 100.0%
Time: 8.5s
Precision: binary64
Cost: 6464

?

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

Local Percentage Accuracy?

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.

Herbie found 2 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original1.6%
Target100.0%
Herbie100.0%
\[\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) \]

Derivation?

  1. Initial program 1.5%

    \[\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \]
  2. Simplified3.0%

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

    [Start]1.5

    \[ \left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) - 1 \]

    sub-neg [=>]1.5

    \[ \color{blue}{\left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right) + \left(-1\right)} \]

    +-commutative [=>]1.5

    \[ \color{blue}{\left(-1\right) + \left(\left(n + 1\right) \cdot \log \left(n + 1\right) - n \cdot \log n\right)} \]

    associate-+r- [=>]3.0

    \[ \color{blue}{\left(\left(-1\right) + \left(n + 1\right) \cdot \log \left(n + 1\right)\right) - n \cdot \log n} \]

    +-commutative [<=]3.0

    \[ \color{blue}{\left(\left(n + 1\right) \cdot \log \left(n + 1\right) + \left(-1\right)\right)} - n \cdot \log n \]

    fma-def [=>]3.0

    \[ \color{blue}{\mathsf{fma}\left(n + 1, \log \left(n + 1\right), -1\right)} - n \cdot \log n \]

    +-commutative [=>]3.0

    \[ \mathsf{fma}\left(n + 1, \log \color{blue}{\left(1 + n\right)}, -1\right) - n \cdot \log n \]

    log1p-def [=>]3.0

    \[ \mathsf{fma}\left(n + 1, \color{blue}{\mathsf{log1p}\left(n\right)}, -1\right) - n \cdot \log n \]

    metadata-eval [=>]3.0

    \[ \mathsf{fma}\left(n + 1, \mathsf{log1p}\left(n\right), \color{blue}{-1}\right) - n \cdot \log n \]
  3. Taylor expanded in n around inf 100.0%

    \[\leadsto \color{blue}{-1 \cdot \log \left(\frac{1}{n}\right)} \]
  4. Simplified100.0%

    \[\leadsto \color{blue}{\log n} \]
    Step-by-step derivation

    [Start]100.0

    \[ -1 \cdot \log \left(\frac{1}{n}\right) \]

    mul-1-neg [=>]100.0

    \[ \color{blue}{-\log \left(\frac{1}{n}\right)} \]

    log-rec [=>]100.0

    \[ -\color{blue}{\left(-\log n\right)} \]

    remove-double-neg [=>]100.0

    \[ \color{blue}{\log n} \]
  5. Final simplification100.0%

    \[\leadsto \log n \]

Alternatives

Alternative 1
Accuracy3.1%
Cost64
\[0 \]

Reproduce?

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

  :herbie-target
  (- (log (+ n 1.0)) (- (/ 1.0 (* 2.0 n)) (- (/ 1.0 (* 3.0 (* n n))) (/ 4.0 (pow n 3.0)))))

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