Average Error: 38.8 → 0.2
Time: 12.5s
Precision: 64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;x + 1 \le 1.000007462280689596312299727287609130144:\\ \;\;\;\;\mathsf{fma}\left({x}^{3}, 1, \mathsf{fma}\left(\frac{{x}^{3}}{{1}^{3}}, 0.3333333333333333148296162562473909929395, \mathsf{fma}\left(\frac{0.5}{1}, \frac{x \cdot x}{1}, 1 \cdot x\right) - 1 \cdot \mathsf{fma}\left(x, x, \frac{{x}^{3}}{1 \cdot 1}\right)\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.000007462280689596312299727287609130144:\\
\;\;\;\;\mathsf{fma}\left({x}^{3}, 1, \mathsf{fma}\left(\frac{{x}^{3}}{{1}^{3}}, 0.3333333333333333148296162562473909929395, \mathsf{fma}\left(\frac{0.5}{1}, \frac{x \cdot x}{1}, 1 \cdot x\right) - 1 \cdot \mathsf{fma}\left(x, x, \frac{{x}^{3}}{1 \cdot 1}\right)\right)\right)\\

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

\end{array}
double f(double x) {
        double r55845 = 1.0;
        double r55846 = x;
        double r55847 = r55845 + r55846;
        double r55848 = log(r55847);
        return r55848;
}

double f(double x) {
        double r55849 = x;
        double r55850 = 1.0;
        double r55851 = r55849 + r55850;
        double r55852 = 1.0000074622806896;
        bool r55853 = r55851 <= r55852;
        double r55854 = 3.0;
        double r55855 = pow(r55849, r55854);
        double r55856 = pow(r55850, r55854);
        double r55857 = r55855 / r55856;
        double r55858 = 0.3333333333333333;
        double r55859 = 0.5;
        double r55860 = r55859 / r55850;
        double r55861 = r55849 * r55849;
        double r55862 = r55861 / r55850;
        double r55863 = r55850 * r55849;
        double r55864 = fma(r55860, r55862, r55863);
        double r55865 = r55850 * r55850;
        double r55866 = r55855 / r55865;
        double r55867 = fma(r55849, r55849, r55866);
        double r55868 = r55850 * r55867;
        double r55869 = r55864 - r55868;
        double r55870 = fma(r55857, r55858, r55869);
        double r55871 = fma(r55855, r55850, r55870);
        double r55872 = log(r55851);
        double r55873 = r55853 ? r55871 : r55872;
        return r55873;
}

Error

Bits error versus x

Target

Original38.8
Target0.1
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.0000074622806896

    1. Initial program 59.1

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

      \[\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-div59.1

      \[\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. Simplified59.1

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

      \[\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.3

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

    if 1.0000074622806896 < (+ 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.000007462280689596312299727287609130144:\\ \;\;\;\;\mathsf{fma}\left({x}^{3}, 1, \mathsf{fma}\left(\frac{{x}^{3}}{{1}^{3}}, 0.3333333333333333148296162562473909929395, \mathsf{fma}\left(\frac{0.5}{1}, \frac{x \cdot x}{1}, 1 \cdot x\right) - 1 \cdot \mathsf{fma}\left(x, x, \frac{{x}^{3}}{1 \cdot 1}\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(x + 1\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2019194 +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)))