Average Error: 39.2 → 0.2
Time: 19.7s
Precision: 64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;1 + x \le 1.000003244205803554223166429437696933746:\\ \;\;\;\;\mathsf{fma}\left(0.3333333333333333148296162562473909929395, {\left(\frac{x}{1}\right)}^{3}, \mathsf{fma}\left(\frac{{x}^{2}}{{1}^{2}}, 0.5, 1 \cdot \left({x}^{3} + x\right)\right)\right) - \mathsf{fma}\left(x, 1 \cdot x, \frac{{x}^{3}}{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.000003244205803554223166429437696933746:\\
\;\;\;\;\mathsf{fma}\left(0.3333333333333333148296162562473909929395, {\left(\frac{x}{1}\right)}^{3}, \mathsf{fma}\left(\frac{{x}^{2}}{{1}^{2}}, 0.5, 1 \cdot \left({x}^{3} + x\right)\right)\right) - \mathsf{fma}\left(x, 1 \cdot x, \frac{{x}^{3}}{1}\right)\\

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

\end{array}
double f(double x) {
        double r93342 = 1.0;
        double r93343 = x;
        double r93344 = r93342 + r93343;
        double r93345 = log(r93344);
        return r93345;
}

double f(double x) {
        double r93346 = 1.0;
        double r93347 = x;
        double r93348 = r93346 + r93347;
        double r93349 = 1.0000032442058036;
        bool r93350 = r93348 <= r93349;
        double r93351 = 0.3333333333333333;
        double r93352 = r93347 / r93346;
        double r93353 = 3.0;
        double r93354 = pow(r93352, r93353);
        double r93355 = 2.0;
        double r93356 = pow(r93347, r93355);
        double r93357 = pow(r93346, r93355);
        double r93358 = r93356 / r93357;
        double r93359 = 0.5;
        double r93360 = pow(r93347, r93353);
        double r93361 = r93360 + r93347;
        double r93362 = r93346 * r93361;
        double r93363 = fma(r93358, r93359, r93362);
        double r93364 = fma(r93351, r93354, r93363);
        double r93365 = r93346 * r93347;
        double r93366 = r93360 / r93346;
        double r93367 = fma(r93347, r93365, r93366);
        double r93368 = r93364 - r93367;
        double r93369 = log(r93348);
        double r93370 = r93350 ? r93368 : r93369;
        return r93370;
}

Error

Bits error versus x

Target

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

    1. Initial program 59.0

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

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

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

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

      \[\leadsto \color{blue}{\left(0.3333333333333333148296162562473909929395 \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)}\]
    7. Simplified0.2

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

    if 1.0000032442058036 < (+ 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}\;1 + x \le 1.000003244205803554223166429437696933746:\\ \;\;\;\;\mathsf{fma}\left(0.3333333333333333148296162562473909929395, {\left(\frac{x}{1}\right)}^{3}, \mathsf{fma}\left(\frac{{x}^{2}}{{1}^{2}}, 0.5, 1 \cdot \left({x}^{3} + x\right)\right)\right) - \mathsf{fma}\left(x, 1 \cdot x, \frac{{x}^{3}}{1}\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array}\]

Reproduce

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