2log (problem 3.3.6)

?

Percentage Accurate: 53.1% → 100.0%
Time: 4.9s
Precision: binary64
Cost: 6592

?

\[\log \left(N + 1\right) - \log N \]
\[\mathsf{log1p}\left(\frac{1}{N}\right) \]
(FPCore (N) :precision binary64 (- (log (+ N 1.0)) (log N)))
(FPCore (N) :precision binary64 (log1p (/ 1.0 N)))
double code(double N) {
	return log((N + 1.0)) - log(N);
}
double code(double N) {
	return log1p((1.0 / N));
}
public static double code(double N) {
	return Math.log((N + 1.0)) - Math.log(N);
}
public static double code(double N) {
	return Math.log1p((1.0 / N));
}
def code(N):
	return math.log((N + 1.0)) - math.log(N)
def code(N):
	return math.log1p((1.0 / N))
function code(N)
	return Float64(log(Float64(N + 1.0)) - log(N))
end
function code(N)
	return log1p(Float64(1.0 / N))
end
code[N_] := N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision]
code[N_] := N[Log[1 + N[(1.0 / N), $MachinePrecision]], $MachinePrecision]
\log \left(N + 1\right) - \log N
\mathsf{log1p}\left(\frac{1}{N}\right)

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 5 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

Derivation?

  1. Initial program 54.1%

    \[\log \left(N + 1\right) - \log N \]
  2. Simplified54.1%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
    Step-by-step derivation

    [Start]54.1

    \[ \log \left(N + 1\right) - \log N \]

    +-commutative [=>]54.1

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

    log1p-def [=>]54.1

    \[ \color{blue}{\mathsf{log1p}\left(N\right)} - \log N \]
  3. Applied egg-rr54.2%

    \[\leadsto \color{blue}{\log \left(\frac{N + 1}{N}\right)} \]
    Step-by-step derivation

    [Start]54.1

    \[ \mathsf{log1p}\left(N\right) - \log N \]

    log1p-udef [=>]54.1

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

    diff-log [=>]54.2

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

    +-commutative [=>]54.2

    \[ \log \left(\frac{\color{blue}{N + 1}}{N}\right) \]
  4. Applied egg-rr54.1%

    \[\leadsto \color{blue}{0 + \left(\mathsf{log1p}\left(N\right) - \log N\right)} \]
    Step-by-step derivation

    [Start]54.2

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

    *-un-lft-identity [=>]54.2

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

    log-prod [=>]54.2

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

    metadata-eval [=>]54.2

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

    log-div [=>]54.1

    \[ 0 + \color{blue}{\left(\log \left(N + 1\right) - \log N\right)} \]

    +-commutative [=>]54.1

    \[ 0 + \left(\log \color{blue}{\left(1 + N\right)} - \log N\right) \]

    log1p-udef [<=]54.1

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

    \[\leadsto \color{blue}{\mathsf{log1p}\left(\frac{1}{N}\right)} \]
    Step-by-step derivation

    [Start]54.1

    \[ 0 + \left(\mathsf{log1p}\left(N\right) - \log N\right) \]

    +-lft-identity [=>]54.1

    \[ \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]

    log1p-def [<=]54.1

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

    +-commutative [<=]54.1

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

    log-div [<=]54.2

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

    *-lft-identity [<=]54.2

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

    associate-*l/ [<=]54.0

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

    distribute-lft-in [=>]54.0

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

    lft-mult-inverse [=>]54.2

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

    *-rgt-identity [=>]54.2

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

    log1p-def [=>]100.0

    \[ \color{blue}{\mathsf{log1p}\left(\frac{1}{N}\right)} \]
  6. Final simplification100.0%

    \[\leadsto \mathsf{log1p}\left(\frac{1}{N}\right) \]

Alternatives

Alternative 1
Accuracy98.7%
Cost6660
\[\begin{array}{l} \mathbf{if}\;N \leq 0.27:\\ \;\;\;\;-\log N\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{N + 0.5}\\ \end{array} \]
Alternative 2
Accuracy57.1%
Cost324
\[\begin{array}{l} \mathbf{if}\;N \leq 0.5:\\ \;\;\;\;2\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{N}\\ \end{array} \]
Alternative 3
Accuracy57.6%
Cost320
\[\frac{1}{N + 0.5} \]
Alternative 4
Accuracy9.8%
Cost64
\[2 \]

Reproduce?

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