Average Error: 38.8 → 0.2
Time: 12.1s
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 r40751 = 1.0;
        double r40752 = x;
        double r40753 = r40751 + r40752;
        double r40754 = log(r40753);
        return r40754;
}

double f(double x) {
        double r40755 = x;
        double r40756 = 1.0;
        double r40757 = r40755 + r40756;
        double r40758 = 1.0000074622806896;
        bool r40759 = r40757 <= r40758;
        double r40760 = 3.0;
        double r40761 = pow(r40755, r40760);
        double r40762 = pow(r40756, r40760);
        double r40763 = r40761 / r40762;
        double r40764 = 0.3333333333333333;
        double r40765 = 0.5;
        double r40766 = r40765 / r40756;
        double r40767 = r40755 * r40755;
        double r40768 = r40767 / r40756;
        double r40769 = r40756 * r40755;
        double r40770 = fma(r40766, r40768, r40769);
        double r40771 = r40756 * r40756;
        double r40772 = r40761 / r40771;
        double r40773 = fma(r40755, r40755, r40772);
        double r40774 = r40756 * r40773;
        double r40775 = r40770 - r40774;
        double r40776 = fma(r40763, r40764, r40775);
        double r40777 = fma(r40761, r40756, r40776);
        double r40778 = log(r40757);
        double r40779 = r40759 ? r40777 : r40778;
        return r40779;
}

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)))