Average Error: 38.7 → 0.4
Time: 9.5s
Precision: 64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;1 + x \le 1.000000000000810462807976364274509251118:\\ \;\;\;\;\mathsf{fma}\left(1, x, 0.3333333333333333148296162562473909929395 \cdot {x}^{3}\right) - 0.5 \cdot {x}^{2}\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array}\]
\log \left(1 + x\right)
\begin{array}{l}
\mathbf{if}\;1 + x \le 1.000000000000810462807976364274509251118:\\
\;\;\;\;\mathsf{fma}\left(1, x, 0.3333333333333333148296162562473909929395 \cdot {x}^{3}\right) - 0.5 \cdot {x}^{2}\\

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

\end{array}
double f(double x) {
        double r73720 = 1.0;
        double r73721 = x;
        double r73722 = r73720 + r73721;
        double r73723 = log(r73722);
        return r73723;
}

double f(double x) {
        double r73724 = 1.0;
        double r73725 = x;
        double r73726 = r73724 + r73725;
        double r73727 = 1.0000000000008105;
        bool r73728 = r73726 <= r73727;
        double r73729 = 0.3333333333333333;
        double r73730 = 3.0;
        double r73731 = pow(r73725, r73730);
        double r73732 = r73729 * r73731;
        double r73733 = fma(r73724, r73725, r73732);
        double r73734 = 0.5;
        double r73735 = 2.0;
        double r73736 = pow(r73725, r73735);
        double r73737 = r73734 * r73736;
        double r73738 = r73733 - r73737;
        double r73739 = log(r73726);
        double r73740 = r73728 ? r73738 : r73739;
        return r73740;
}

Error

Bits error versus x

Target

Original38.7
Target0.2
Herbie0.4
\[\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.0000000000008105

    1. Initial program 59.4

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

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

      \[\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(\frac{1}{3} \cdot \frac{{x}^{3}}{{1}^{3}} + \left(1 \cdot x + \frac{1}{2} \cdot \frac{{x}^{2}}{{1}^{2}}\right)\right) - 1 \cdot {x}^{2}}\]
    6. Simplified0.2

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

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

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

    if 1.0000000000008105 < (+ 1.0 x)

    1. Initial program 0.7

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

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

Reproduce

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

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

  (log (+ 1 x)))