Average Error: 38.8 → 0.2
Time: 9.8s
Precision: 64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;1 + x \le 1.00000051390067646:\\ \;\;\;\;\mathsf{fma}\left(x, 1 - 0.5 \cdot x, \log 1\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array}\]
\log \left(1 + x\right)
\begin{array}{l}
\mathbf{if}\;1 + x \le 1.00000051390067646:\\
\;\;\;\;\mathsf{fma}\left(x, 1 - 0.5 \cdot x, \log 1\right)\\

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

\end{array}
double f(double x) {
        double r114506 = 1.0;
        double r114507 = x;
        double r114508 = r114506 + r114507;
        double r114509 = log(r114508);
        return r114509;
}

double f(double x) {
        double r114510 = 1.0;
        double r114511 = x;
        double r114512 = r114510 + r114511;
        double r114513 = 1.0000005139006765;
        bool r114514 = r114512 <= r114513;
        double r114515 = 0.5;
        double r114516 = r114515 * r114511;
        double r114517 = r114510 - r114516;
        double r114518 = log(r114510);
        double r114519 = fma(r114511, r114517, r114518);
        double r114520 = log(r114512);
        double r114521 = r114514 ? r114519 : r114520;
        return r114521;
}

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.0000005139006765

    1. Initial program 59.2

      \[\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(\frac{{x}^{2}}{{1}^{2}}, \frac{-1}{2}, \mathsf{fma}\left(1, x, \log 1\right)\right)}\]
    4. Taylor expanded around 0 0.3

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

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

    if 1.0000005139006765 < (+ 1.0 x)

    1. Initial program 0.2

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

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

Reproduce

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