Average Error: 39.0 → 0.2
Time: 11.1s
Precision: 64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;x + 1 \le 1.000051798647896950100744106748607009649:\\ \;\;\;\;\mathsf{fma}\left(x, 1, 0.3333333333333332593184650249895639717579 \cdot \left(\left(x \cdot x\right) \cdot x\right) - 0.5 \cdot \left(x \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(x + 1\right)\\ \end{array}\]
\log \left(1 + x\right)
\begin{array}{l}
\mathbf{if}\;x + 1 \le 1.000051798647896950100744106748607009649:\\
\;\;\;\;\mathsf{fma}\left(x, 1, 0.3333333333333332593184650249895639717579 \cdot \left(\left(x \cdot x\right) \cdot x\right) - 0.5 \cdot \left(x \cdot x\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\log \left(x + 1\right)\\

\end{array}
double f(double x) {
        double r3449205 = 1.0;
        double r3449206 = x;
        double r3449207 = r3449205 + r3449206;
        double r3449208 = log(r3449207);
        return r3449208;
}

double f(double x) {
        double r3449209 = x;
        double r3449210 = 1.0;
        double r3449211 = r3449209 + r3449210;
        double r3449212 = 1.000051798647897;
        bool r3449213 = r3449211 <= r3449212;
        double r3449214 = 0.33333333333333326;
        double r3449215 = r3449209 * r3449209;
        double r3449216 = r3449215 * r3449209;
        double r3449217 = r3449214 * r3449216;
        double r3449218 = 0.5;
        double r3449219 = r3449218 * r3449215;
        double r3449220 = r3449217 - r3449219;
        double r3449221 = fma(r3449209, r3449210, r3449220);
        double r3449222 = log(r3449211);
        double r3449223 = r3449213 ? r3449221 : r3449222;
        return r3449223;
}

Error

Bits error versus x

Target

Original39.0
Target0.3
Herbie0.2
\[\begin{array}{l} \mathbf{if}\;1 + x = 1:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot \log \left(1 + x\right)}{\left(1 + x\right) - 1}\\ \end{array}\]

Derivation

  1. Split input into 2 regimes
  2. if (+ 1.0 x) < 1.000051798647897

    1. Initial program 58.9

      \[\log \left(1 + x\right)\]
    2. Using strategy rm
    3. Applied flip3-+59.0

      \[\leadsto \log \color{blue}{\left(\frac{{1}^{3} + {x}^{3}}{1 \cdot 1 + \left(x \cdot x - 1 \cdot x\right)}\right)}\]
    4. Applied log-div58.9

      \[\leadsto \color{blue}{\log \left({1}^{3} + {x}^{3}\right) - \log \left(1 \cdot 1 + \left(x \cdot x - 1 \cdot x\right)\right)}\]
    5. Simplified58.9

      \[\leadsto \color{blue}{\log \left(\mathsf{fma}\left(x \cdot x, x, \left(1 \cdot 1\right) \cdot 1\right)\right)} - \log \left(1 \cdot 1 + \left(x \cdot x - 1 \cdot x\right)\right)\]
    6. Taylor expanded around 0 0.2

      \[\leadsto \color{blue}{\left(1 \cdot {x}^{3} + \left(0.3333333333333333148296162562473909929395 \cdot \frac{{x}^{3}}{{1}^{3}} + \left(0.5 \cdot \frac{{x}^{2}}{{1}^{2}} + 1 \cdot x\right)\right)\right) - \left(1 \cdot \frac{{x}^{3}}{{1}^{2}} + 1 \cdot {x}^{2}\right)}\]
    7. Simplified0.2

      \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, x \cdot 1, \mathsf{fma}\left(\frac{x}{1} \cdot \left(\frac{x}{1} \cdot \frac{x}{1}\right), 0.3333333333333333148296162562473909929395, \mathsf{fma}\left(0.5, \frac{x}{1} \cdot \frac{x}{1}, x \cdot 1\right)\right) - \mathsf{fma}\left(\frac{x \cdot x}{1}, \frac{x}{1}, x \cdot x\right) \cdot 1\right)}\]
    8. Taylor expanded around 0 0.2

      \[\leadsto \color{blue}{\left(1 \cdot x + 0.3333333333333332593184650249895639717579 \cdot {x}^{3}\right) - 0.5 \cdot {x}^{2}}\]
    9. Simplified0.2

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, 1, \left(x \cdot \left(x \cdot x\right)\right) \cdot 0.3333333333333332593184650249895639717579 - \left(x \cdot x\right) \cdot 0.5\right)}\]

    if 1.000051798647897 < (+ 1.0 x)

    1. Initial program 0.1

      \[\log \left(1 + x\right)\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;x + 1 \le 1.000051798647896950100744106748607009649:\\ \;\;\;\;\mathsf{fma}\left(x, 1, 0.3333333333333332593184650249895639717579 \cdot \left(\left(x \cdot x\right) \cdot x\right) - 0.5 \cdot \left(x \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(x + 1\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2019174 +o rules:numerics
(FPCore (x)
  :name "ln(1 + x)"

  :herbie-target
  (if (== (+ 1.0 x) 1.0) x (/ (* x (log (+ 1.0 x))) (- (+ 1.0 x) 1.0)))

  (log (+ 1.0 x)))