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

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

\end{array}
double f(double x) {
        double r5729743 = 1.0;
        double r5729744 = x;
        double r5729745 = r5729743 + r5729744;
        double r5729746 = log(r5729745);
        return r5729746;
}

double f(double x) {
        double r5729747 = 1.0;
        double r5729748 = x;
        double r5729749 = r5729747 + r5729748;
        double r5729750 = 1.000051798647897;
        bool r5729751 = r5729749 <= r5729750;
        double r5729752 = 0.3333333333333333;
        double r5729753 = r5729748 * r5729748;
        double r5729754 = r5729753 * r5729748;
        double r5729755 = r5729752 * r5729754;
        double r5729756 = fma(r5729747, r5729748, r5729755);
        double r5729757 = 0.5;
        double r5729758 = r5729757 * r5729753;
        double r5729759 = r5729756 - r5729758;
        double r5729760 = log(r5729749);
        double r5729761 = r5729751 ? r5729759 : r5729760;
        return r5729761;
}

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 flip-+59.0

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

      \[\leadsto \color{blue}{\log \left(1 \cdot 1 - x \cdot x\right) - \log \left(1 - x\right)}\]
    5. Taylor expanded around 0 0.2

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(1, x, 0.3333333333333333148296162562473909929395 \cdot \left(\left(x \cdot x\right) \cdot x\right)\right) - 0.5 \cdot \left(x \cdot x\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}\;1 + x \le 1.000051798647896950100744106748607009649:\\ \;\;\;\;\mathsf{fma}\left(1, x, 0.3333333333333333148296162562473909929395 \cdot \left(\left(x \cdot x\right) \cdot x\right)\right) - 0.5 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\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)))