# ?

Average Error: 2.9 → 0.4
Time: 20.8s
Precision: binary64
Cost: 19648

# ?

$0.01 \leq x \land x \leq 1000$
$\left(\left(x + 1\right) \cdot \log \left(x + 1\right) - x \cdot \log x\right) - 1$
$\mathsf{fma}\left(x, \mathsf{log1p}\left(\frac{1}{x}\right), -1 + \mathsf{log1p}\left(x\right)\right)$
(FPCore (x)
:precision binary64
(- (- (* (+ x 1.0) (log (+ x 1.0))) (* x (log x))) 1.0))
(FPCore (x) :precision binary64 (fma x (log1p (/ 1.0 x)) (+ -1.0 (log1p x))))
double code(double x) {
return (((x + 1.0) * log((x + 1.0))) - (x * log(x))) - 1.0;
}

double code(double x) {
return fma(x, log1p((1.0 / x)), (-1.0 + log1p(x)));
}

function code(x)
return Float64(Float64(Float64(Float64(x + 1.0) * log(Float64(x + 1.0))) - Float64(x * log(x))) - 1.0)
end

function code(x)
return fma(x, log1p(Float64(1.0 / x)), Float64(-1.0 + log1p(x)))
end

code[x_] := N[(N[(N[(N[(x + 1.0), $MachinePrecision] * N[Log[N[(x + 1.0),$MachinePrecision]], $MachinePrecision]),$MachinePrecision] - N[(x * N[Log[x], $MachinePrecision]),$MachinePrecision]), $MachinePrecision] - 1.0),$MachinePrecision]

code[x_] := N[(x * N[Log[1 + N[(1.0 / x), $MachinePrecision]],$MachinePrecision] + N[(-1.0 + N[Log[1 + x], $MachinePrecision]),$MachinePrecision]), \$MachinePrecision]

\left(\left(x + 1\right) \cdot \log \left(x + 1\right) - x \cdot \log x\right) - 1

\mathsf{fma}\left(x, \mathsf{log1p}\left(\frac{1}{x}\right), -1 + \mathsf{log1p}\left(x\right)\right)


# Derivation?

1. Initial program 2.9

$\left(\left(x + 1\right) \cdot \log \left(x + 1\right) - x \cdot \log x\right) - 1$
2. Simplified2.4

$\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{log1p}\left(x\right) - \log x, -1 + \mathsf{log1p}\left(x\right)\right)}$
Proof
[Start]2.9 $\left(\left(x + 1\right) \cdot \log \left(x + 1\right) - x \cdot \log x\right) - 1$ $\color{blue}{\left(x + 1\right) \cdot \log \left(x + 1\right) - \left(x \cdot \log x + 1\right)}$ $\color{blue}{\log \left(x + 1\right) \cdot \left(x + 1\right)} - \left(x \cdot \log x + 1\right)$ $\log \left(x + 1\right) \cdot \color{blue}{\left(1 + x\right)} - \left(x \cdot \log x + 1\right)$ $\color{blue}{\left(\log \left(x + 1\right) \cdot 1 + \log \left(x + 1\right) \cdot x\right)} - \left(x \cdot \log x + 1\right)$ $\color{blue}{\log \left(x + 1\right) \cdot 1 + \left(\log \left(x + 1\right) \cdot x - \left(x \cdot \log x + 1\right)\right)}$ $\color{blue}{\left(\log \left(x + 1\right) \cdot x - \left(x \cdot \log x + 1\right)\right) + \log \left(x + 1\right) \cdot 1}$ $\color{blue}{\log \left(x + 1\right) \cdot x - \left(\left(x \cdot \log x + 1\right) - \log \left(x + 1\right) \cdot 1\right)}$ $\log \left(x + 1\right) \cdot x - \color{blue}{\left(x \cdot \log x + \left(1 - \log \left(x + 1\right) \cdot 1\right)\right)}$ $\color{blue}{\left(\log \left(x + 1\right) \cdot x - x \cdot \log x\right) - \left(1 - \log \left(x + 1\right) \cdot 1\right)}$ $\left(\color{blue}{\left(1 \cdot \log \left(x + 1\right)\right)} \cdot x - x \cdot \log x\right) - \left(1 - \log \left(x + 1\right) \cdot 1\right)$ $\left(\color{blue}{\left(\log \left(x + 1\right) \cdot 1\right)} \cdot x - x \cdot \log x\right) - \left(1 - \log \left(x + 1\right) \cdot 1\right)$ $\left(\left(\log \left(x + 1\right) \cdot 1\right) \cdot x - \color{blue}{\log x \cdot x}\right) - \left(1 - \log \left(x + 1\right) \cdot 1\right)$ $\color{blue}{x \cdot \left(\log \left(x + 1\right) \cdot 1 - \log x\right)} - \left(1 - \log \left(x + 1\right) \cdot 1\right)$ $x \cdot \color{blue}{\left(\log \left(x + 1\right) \cdot 1 + \left(-\log x\right)\right)} - \left(1 - \log \left(x + 1\right) \cdot 1\right)$
3. Applied egg-rr1.6

$\leadsto \mathsf{fma}\left(x, \color{blue}{\log \left(\frac{x + 1}{x}\right)}, -1 + \mathsf{log1p}\left(x\right)\right)$
4. Applied egg-rr1.6

$\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{log1p}\left(\frac{x + 1}{x} - 1\right)}, -1 + \mathsf{log1p}\left(x\right)\right)$
5. Simplified0.4

$\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{log1p}\left(\frac{1}{x} + 0\right)}, -1 + \mathsf{log1p}\left(x\right)\right)$
Proof
[Start]1.6 $\mathsf{fma}\left(x, \mathsf{log1p}\left(\frac{x + 1}{x} - 1\right), -1 + \mathsf{log1p}\left(x\right)\right)$ $\mathsf{fma}\left(x, \mathsf{log1p}\left(\frac{\color{blue}{1 \cdot \left(x + 1\right)}}{x} - 1\right), -1 + \mathsf{log1p}\left(x\right)\right)$ $\mathsf{fma}\left(x, \mathsf{log1p}\left(\color{blue}{\frac{1}{x} \cdot \left(x + 1\right)} - 1\right), -1 + \mathsf{log1p}\left(x\right)\right)$ $\mathsf{fma}\left(x, \mathsf{log1p}\left(\color{blue}{\left(x \cdot \frac{1}{x} + 1 \cdot \frac{1}{x}\right)} - 1\right), -1 + \mathsf{log1p}\left(x\right)\right)$ $\mathsf{fma}\left(x, \mathsf{log1p}\left(\left(\color{blue}{1} + 1 \cdot \frac{1}{x}\right) - 1\right), -1 + \mathsf{log1p}\left(x\right)\right)$ $\mathsf{fma}\left(x, \mathsf{log1p}\left(\left(1 + \color{blue}{\frac{1}{x}}\right) - 1\right), -1 + \mathsf{log1p}\left(x\right)\right)$ $\mathsf{fma}\left(x, \mathsf{log1p}\left(\color{blue}{\left(\frac{1}{x} + 1\right)} - 1\right), -1 + \mathsf{log1p}\left(x\right)\right)$ $\mathsf{fma}\left(x, \mathsf{log1p}\left(\color{blue}{\frac{1}{x} + \left(1 - 1\right)}\right), -1 + \mathsf{log1p}\left(x\right)\right)$ $\mathsf{fma}\left(x, \mathsf{log1p}\left(\frac{1}{x} + \color{blue}{0}\right), -1 + \mathsf{log1p}\left(x\right)\right)$
6. Final simplification0.4

$\leadsto \mathsf{fma}\left(x, \mathsf{log1p}\left(\frac{1}{x}\right), -1 + \mathsf{log1p}\left(x\right)\right)$

# Alternatives

Alternative 1
Error2.4
Cost19776
$\left(-1 + \mathsf{log1p}\left(x\right)\right) + x \cdot \left(\mathsf{log1p}\left(x\right) - \log x\right)$
Alternative 2
Error1.6
Cost19776
$\mathsf{fma}\left(x, \log \left(1 + \frac{1}{x}\right), -1 + \mathsf{log1p}\left(x\right)\right)$
Alternative 3
Error37.8
Cost13508
$\begin{array}{l} \mathbf{if}\;x \leq 0.78:\\ \;\;\;\;\mathsf{fma}\left(x, \log \left(\frac{x + 1}{x}\right), x + -1\right)\\ \mathbf{else}:\\ \;\;\;\;\log x + \left(\frac{0.5}{x} + \frac{-0.16666666666666666}{x \cdot x}\right)\\ \end{array}$
Alternative 4
Error2.7
Cost13504
$\mathsf{log1p}\left(x\right) \cdot \left(x + 1\right) + \left(-1 - x \cdot \log x\right)$
Alternative 5
Error2.7
Cost13504
$\left(-1 + \mathsf{log1p}\left(x\right) \cdot \left(x + 1\right)\right) - x \cdot \log x$
Alternative 6
Error37.9
Cost7236
$\begin{array}{l} \mathbf{if}\;x \leq 0.62:\\ \;\;\;\;-1 + \left(x - x \cdot \log x\right)\\ \mathbf{else}:\\ \;\;\;\;\log x + \left(\frac{0.5}{x} + \frac{-0.16666666666666666}{x \cdot x}\right)\\ \end{array}$
Alternative 7
Error38.0
Cost7236
$\begin{array}{l} \mathbf{if}\;x \leq 0.72:\\ \;\;\;\;-1 + \left(x \cdot \left(x + 1\right) - x \cdot \log x\right)\\ \mathbf{else}:\\ \;\;\;\;\log x + \left(\frac{0.5}{x} + \frac{-0.16666666666666666}{x \cdot x}\right)\\ \end{array}$
Alternative 8
Error41.3
Cost7108
$\begin{array}{l} \mathbf{if}\;x \leq 0.54:\\ \;\;\;\;-1 + \left(x - x \cdot \log x\right)\\ \mathbf{else}:\\ \;\;\;\;-1 + \left(1 + \left(\log x + \frac{0.5}{x}\right)\right)\\ \end{array}$
Alternative 9
Error43.4
Cost6980
$\begin{array}{l} \mathbf{if}\;x \leq 0.54:\\ \;\;\;\;-1 + \mathsf{log1p}\left(x\right) \cdot \left(x + 1\right)\\ \mathbf{else}:\\ \;\;\;\;\log x + \frac{0.5}{x}\\ \end{array}$
Alternative 10
Error41.3
Cost6980
$\begin{array}{l} \mathbf{if}\;x \leq 0.54:\\ \;\;\;\;-1 + x \cdot \left(1 - \log x\right)\\ \mathbf{else}:\\ \;\;\;\;\log x + \frac{0.5}{x}\\ \end{array}$
Alternative 11
Error41.3
Cost6980
$\begin{array}{l} \mathbf{if}\;x \leq 0.54:\\ \;\;\;\;-1 + \left(x - x \cdot \log x\right)\\ \mathbf{else}:\\ \;\;\;\;\log x + \frac{0.5}{x}\\ \end{array}$
Alternative 12
Error43.5
Cost6852
$\begin{array}{l} \mathbf{if}\;x \leq 0.54:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\log x + \frac{0.5}{x}\\ \end{array}$
Alternative 13
Error48.8
Cost6656
$-\log \left(\frac{1}{x}\right)$
Alternative 14
Error48.8
Cost6464
$\log x$
Alternative 15
Error58.9
Cost64
$-1$

# Reproduce?

herbie shell --seed 1
(FPCore (x)
:name "(x+1)*log(x+1) - x*log(x) - 1"
:precision binary64
:pre (and (<= 0.01 x) (<= x 1000.0))
(- (- (* (+ x 1.0) (log (+ x 1.0))) (* x (log x))) 1.0))