?

Average Error: 29.4 → 0.1
Time: 1.4min
Precision: binary64
Cost: 46088

?

\[\log \left(N + 1\right) - \log N \]
\[\begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 2 \cdot 10^{-5}:\\ \;\;\;\;-0.3333333333333333 \cdot {\left(\frac{-1}{N}\right)}^{3} + \left(-0.5 \cdot {\left(\frac{-1}{N}\right)}^{2} + \left(\frac{1}{N} + -0.25 \cdot {\left(\frac{-1}{N}\right)}^{4}\right)\right)\\ \mathbf{elif}\;-\log N \ne 0:\\ \;\;\;\;1 - \left(1 + \mathsf{fma}\left(\mathsf{log1p}\left(N\right), \frac{-1}{\log N}, 1\right) \cdot \log N\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(N\right) - \log N\\ \end{array} \]
(FPCore (N) :precision binary64 (- (log (+ N 1.0)) (log N)))
(FPCore (N)
 :precision binary64
 (if (<= (- (log (+ N 1.0)) (log N)) 2e-5)
   (+
    (* -0.3333333333333333 (pow (/ -1.0 N) 3.0))
    (+
     (* -0.5 (pow (/ -1.0 N) 2.0))
     (+ (/ 1.0 N) (* -0.25 (pow (/ -1.0 N) 4.0)))))
   (if (!= (- (log N)) 0.0)
     (- 1.0 (+ 1.0 (* (fma (log1p N) (/ -1.0 (log N)) 1.0) (log N))))
     (- (log1p N) (log N)))))
double code(double N) {
	return log((N + 1.0)) - log(N);
}
double code(double N) {
	double tmp;
	if ((log((N + 1.0)) - log(N)) <= 2e-5) {
		tmp = (-0.3333333333333333 * pow((-1.0 / N), 3.0)) + ((-0.5 * pow((-1.0 / N), 2.0)) + ((1.0 / N) + (-0.25 * pow((-1.0 / N), 4.0))));
	} else if (-log(N) != 0.0) {
		tmp = 1.0 - (1.0 + (fma(log1p(N), (-1.0 / log(N)), 1.0) * log(N)));
	} else {
		tmp = log1p(N) - log(N);
	}
	return tmp;
}
function code(N)
	return Float64(log(Float64(N + 1.0)) - log(N))
end
function code(N)
	tmp = 0.0
	if (Float64(log(Float64(N + 1.0)) - log(N)) <= 2e-5)
		tmp = Float64(Float64(-0.3333333333333333 * (Float64(-1.0 / N) ^ 3.0)) + Float64(Float64(-0.5 * (Float64(-1.0 / N) ^ 2.0)) + Float64(Float64(1.0 / N) + Float64(-0.25 * (Float64(-1.0 / N) ^ 4.0)))));
	elseif (Float64(-log(N)) != 0.0)
		tmp = Float64(1.0 - Float64(1.0 + Float64(fma(log1p(N), Float64(-1.0 / log(N)), 1.0) * log(N))));
	else
		tmp = Float64(log1p(N) - log(N));
	end
	return tmp
end
code[N_] := N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision]
code[N_] := If[LessEqual[N[(N[Log[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision], 2e-5], N[(N[(-0.3333333333333333 * N[Power[N[(-1.0 / N), $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(-0.5 * N[Power[N[(-1.0 / N), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision] + N[(N[(1.0 / N), $MachinePrecision] + N[(-0.25 * N[Power[N[(-1.0 / N), $MachinePrecision], 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[Unequal[(-N[Log[N], $MachinePrecision]), 0.0], N[(1.0 - N[(1.0 + N[(N[(N[Log[1 + N], $MachinePrecision] * N[(-1.0 / N[Log[N], $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision] * N[Log[N], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Log[1 + N], $MachinePrecision] - N[Log[N], $MachinePrecision]), $MachinePrecision]]]
\log \left(N + 1\right) - \log N
\begin{array}{l}
\mathbf{if}\;\log \left(N + 1\right) - \log N \leq 2 \cdot 10^{-5}:\\
\;\;\;\;-0.3333333333333333 \cdot {\left(\frac{-1}{N}\right)}^{3} + \left(-0.5 \cdot {\left(\frac{-1}{N}\right)}^{2} + \left(\frac{1}{N} + -0.25 \cdot {\left(\frac{-1}{N}\right)}^{4}\right)\right)\\

\mathbf{elif}\;-\log N \ne 0:\\
\;\;\;\;1 - \left(1 + \mathsf{fma}\left(\mathsf{log1p}\left(N\right), \frac{-1}{\log N}, 1\right) \cdot \log N\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(N\right) - \log N\\


\end{array}

Error?

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N)) < 2.00000000000000016e-5

    1. Initial program 59.5

      \[\log \left(N + 1\right) - \log N \]
    2. Simplified59.5

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
      Proof
    3. Taylor expanded in N around -inf 0.0

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

    if 2.00000000000000016e-5 < (-.f64 (log.f64 (+.f64 N 1)) (log.f64 N))

    1. Initial program 0.1

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

      \[\leadsto \color{blue}{\mathsf{log1p}\left(N\right) - \log N} \]
      Proof
    3. Applied egg-rr0.1

      \[\leadsto \color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;-\log N \ne 0:\\ \;\;\;\;\left(-\log N\right) \cdot \left(1 + \left(-\frac{\mathsf{log1p}\left(N\right)}{\log N}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(N\right) - \log N\\ } \end{array}} \]
    4. Simplified0.1

      \[\leadsto \color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;-\log N \ne 0:\\ \;\;\;\;\left(-\log N\right) \cdot \left(1 - \frac{\mathsf{log1p}\left(N\right)}{\log N}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(N\right) - \log N\\ } \end{array}} \]
      Proof
    5. Applied egg-rr0.1

      \[\leadsto \begin{array}{l} \mathbf{if}\;-\log N \ne 0:\\ \;\;\;\;\color{blue}{\left(-1 + \frac{\mathsf{log1p}\left(N\right)}{\log N}\right) \cdot \log N}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(N\right) - \log N\\ \end{array} \]
    6. Applied egg-rr0.1

      \[\leadsto \begin{array}{l} \mathbf{if}\;-\log N \ne 0:\\ \;\;\;\;\color{blue}{1 - \left(1 + \left(1 - \frac{\mathsf{log1p}\left(N\right)}{\log N}\right) \cdot \log N\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(N\right) - \log N\\ \end{array} \]
    7. Applied egg-rr0.1

      \[\leadsto \begin{array}{l} \mathbf{if}\;-\log N \ne 0:\\ \;\;\;\;1 - \left(1 + \color{blue}{\mathsf{fma}\left(\mathsf{log1p}\left(N\right), \frac{-1}{\log N}, 1\right)} \cdot \log N\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(N\right) - \log N\\ \end{array} \]
  3. Recombined 2 regimes into one program.

Alternatives

Alternative 1
Error0.1
Cost45832
\[\begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 2 \cdot 10^{-5}:\\ \;\;\;\;-0.3333333333333333 \cdot {\left(\frac{-1}{N}\right)}^{3} + \left(-0.5 \cdot {\left(\frac{-1}{N}\right)}^{2} + \left(\frac{1}{N} + -0.25 \cdot {\left(\frac{-1}{N}\right)}^{4}\right)\right)\\ \mathbf{elif}\;-\log N \ne 0:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{\log N}, \mathsf{log1p}\left(N\right), -1\right) \cdot \log N\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(N\right) - \log N\\ \end{array} \]
Alternative 2
Error0.1
Cost33924
\[\begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 2 \cdot 10^{-5}:\\ \;\;\;\;-0.3333333333333333 \cdot {\left(\frac{-1}{N}\right)}^{3} + \left(-0.5 \cdot {\left(\frac{-1}{N}\right)}^{2} + \left(\frac{1}{N} + -0.25 \cdot {\left(\frac{-1}{N}\right)}^{4}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(N\right) - \log N\\ \end{array} \]
Alternative 3
Error0.1
Cost26180
\[\begin{array}{l} \mathbf{if}\;\log \left(N + 1\right) - \log N \leq 2 \cdot 10^{-5}:\\ \;\;\;\;\frac{\frac{\frac{0.3333333333333333 - 0.5 \cdot N}{N} + N}{N}}{N}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(N\right) - \log N\\ \end{array} \]
Alternative 4
Error0.4
Cost7108
\[\begin{array}{l} \mathbf{if}\;N \leq 0.95:\\ \;\;\;\;\left(N - \log N\right) + -0.5 \cdot \left(N \cdot N\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\frac{0.3333333333333333 - 0.5 \cdot N}{N} + N}{N}}{N}\\ \end{array} \]
Alternative 5
Error0.4
Cost6724
\[\begin{array}{l} \mathbf{if}\;N \leq 0.85:\\ \;\;\;\;N - \log N\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\frac{0.3333333333333333 - 0.5 \cdot N}{N} + N}{N}}{N}\\ \end{array} \]
Alternative 6
Error30.9
Cost192
\[\frac{1}{N} \]
Alternative 7
Error61.1
Cost64
\[N \]

Error

Reproduce?

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