Average Error: 39.3 → 0.6
Time: 13.8s
Precision: 64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;x + 1 \le 1:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{1} \cdot \frac{x}{1}, \frac{-1}{2}, \mathsf{fma}\left(1, x, \log 1\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:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{1} \cdot \frac{x}{1}, \frac{-1}{2}, \mathsf{fma}\left(1, x, \log 1\right)\right)\\

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

\end{array}
double f(double x) {
        double r3217770 = 1.0;
        double r3217771 = x;
        double r3217772 = r3217770 + r3217771;
        double r3217773 = log(r3217772);
        return r3217773;
}

double f(double x) {
        double r3217774 = x;
        double r3217775 = 1.0;
        double r3217776 = r3217774 + r3217775;
        bool r3217777 = r3217776 <= r3217775;
        double r3217778 = r3217774 / r3217775;
        double r3217779 = r3217778 * r3217778;
        double r3217780 = -0.5;
        double r3217781 = log(r3217775);
        double r3217782 = fma(r3217775, r3217774, r3217781);
        double r3217783 = fma(r3217779, r3217780, r3217782);
        double r3217784 = log(r3217776);
        double r3217785 = r3217777 ? r3217783 : r3217784;
        return r3217785;
}

Error

Bits error versus x

Target

Original39.3
Target0.2
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.0

    1. Initial program 59.6

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

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

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

    if 1.0 < (+ 1.0 x)

    1. Initial program 1.3

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

      \[\leadsto \log \color{blue}{\left({\left(1 + x\right)}^{1}\right)}\]
    4. Applied log-pow1.3

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

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

Reproduce

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