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

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

\end{array}
double f(double x) {
        double r69472 = 1.0;
        double r69473 = x;
        double r69474 = r69472 + r69473;
        double r69475 = log(r69474);
        return r69475;
}

double f(double x) {
        double r69476 = 1.0;
        double r69477 = x;
        double r69478 = r69476 + r69477;
        double r69479 = 1.000000000000481;
        bool r69480 = r69478 <= r69479;
        double r69481 = -r69476;
        double r69482 = 3.0;
        double r69483 = pow(r69477, r69482);
        double r69484 = 2.0;
        double r69485 = pow(r69476, r69484);
        double r69486 = r69483 / r69485;
        double r69487 = fma(r69477, r69477, r69486);
        double r69488 = 0.3333333333333333;
        double r69489 = pow(r69476, r69482);
        double r69490 = r69483 / r69489;
        double r69491 = 0.5;
        double r69492 = pow(r69477, r69484);
        double r69493 = r69492 / r69485;
        double r69494 = r69476 * r69477;
        double r69495 = fma(r69491, r69493, r69494);
        double r69496 = fma(r69476, r69483, r69495);
        double r69497 = fma(r69488, r69490, r69496);
        double r69498 = fma(r69481, r69487, r69497);
        double r69499 = log(r69478);
        double r69500 = r69480 ? r69498 : r69499;
        return r69500;
}

Error

Bits error versus x

Target

Original38.9
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.000000000000481

    1. Initial program 59.3

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

      \[\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.4

      \[\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. Taylor expanded around 0 0.3

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

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

    if 1.000000000000481 < (+ 1.0 x)

    1. Initial program 0.8

      \[\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.0000000000004809:\\ \;\;\;\;\mathsf{fma}\left(-1, \mathsf{fma}\left(x, x, \frac{{x}^{3}}{{1}^{2}}\right), \mathsf{fma}\left(0.333333333333333315, \frac{{x}^{3}}{{1}^{3}}, \mathsf{fma}\left(1, {x}^{3}, \mathsf{fma}\left(0.5, \frac{{x}^{2}}{{1}^{2}}, 1 \cdot x\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array}\]

Reproduce

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