Average Error: 39.2 → 0.6
Time: 3.6s
Precision: 64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;1 + x \le 1.000000000000000666133814775093924254179:\\ \;\;\;\;\mathsf{fma}\left(x, 1, \log 1 - \frac{1}{2} \cdot \frac{{x}^{2}}{{1}^{2}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{2} \cdot \log \left(1 + x\right) + \frac{1}{2} \cdot \log \left(1 + x\right)\\ \end{array}\]
\log \left(1 + x\right)
\begin{array}{l}
\mathbf{if}\;1 + x \le 1.000000000000000666133814775093924254179:\\
\;\;\;\;\mathsf{fma}\left(x, 1, \log 1 - \frac{1}{2} \cdot \frac{{x}^{2}}{{1}^{2}}\right)\\

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

\end{array}
double f(double x) {
        double r69945 = 1.0;
        double r69946 = x;
        double r69947 = r69945 + r69946;
        double r69948 = log(r69947);
        return r69948;
}

double f(double x) {
        double r69949 = 1.0;
        double r69950 = x;
        double r69951 = r69949 + r69950;
        double r69952 = 1.0000000000000007;
        bool r69953 = r69951 <= r69952;
        double r69954 = log(r69949);
        double r69955 = 0.5;
        double r69956 = 2.0;
        double r69957 = pow(r69950, r69956);
        double r69958 = pow(r69949, r69956);
        double r69959 = r69957 / r69958;
        double r69960 = r69955 * r69959;
        double r69961 = r69954 - r69960;
        double r69962 = fma(r69950, r69949, r69961);
        double r69963 = log(r69951);
        double r69964 = r69955 * r69963;
        double r69965 = r69964 + r69964;
        double r69966 = r69953 ? r69962 : r69965;
        return r69966;
}

Error

Bits error versus x

Target

Original39.2
Target0.3
Herbie0.6
\[\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.0000000000000007

    1. Initial program 59.5

      \[\log \left(1 + x\right)\]
    2. Taylor expanded around 0 0.3

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

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

    if 1.0000000000000007 < (+ 1.0 x)

    1. Initial program 1.0

      \[\log \left(1 + x\right)\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt1.1

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

      \[\leadsto \color{blue}{\log \left(\sqrt{1 + x}\right) + \log \left(\sqrt{1 + x}\right)}\]
    5. Using strategy rm
    6. Applied pow11.1

      \[\leadsto \log \left(\sqrt{1 + x}\right) + \log \left(\sqrt{\color{blue}{{\left(1 + x\right)}^{1}}}\right)\]
    7. Applied sqrt-pow11.1

      \[\leadsto \log \left(\sqrt{1 + x}\right) + \log \color{blue}{\left({\left(1 + x\right)}^{\left(\frac{1}{2}\right)}\right)}\]
    8. Applied log-pow1.0

      \[\leadsto \log \left(\sqrt{1 + x}\right) + \color{blue}{\frac{1}{2} \cdot \log \left(1 + x\right)}\]
    9. Using strategy rm
    10. Applied pow11.0

      \[\leadsto \log \left(\sqrt{\color{blue}{{\left(1 + x\right)}^{1}}}\right) + \frac{1}{2} \cdot \log \left(1 + x\right)\]
    11. Applied sqrt-pow11.0

      \[\leadsto \log \color{blue}{\left({\left(1 + x\right)}^{\left(\frac{1}{2}\right)}\right)} + \frac{1}{2} \cdot \log \left(1 + x\right)\]
    12. Applied log-pow1.0

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \log \left(1 + x\right)} + \frac{1}{2} \cdot \log \left(1 + x\right)\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.6

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

Reproduce

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