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

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

\end{array}
double f(double x) {
        double r85446 = 1.0;
        double r85447 = x;
        double r85448 = r85446 + r85447;
        double r85449 = log(r85448);
        return r85449;
}

double f(double x) {
        double r85450 = 1.0;
        double r85451 = x;
        double r85452 = r85450 + r85451;
        double r85453 = 1.0000000000568305;
        bool r85454 = r85452 <= r85453;
        double r85455 = log(r85450);
        double r85456 = 0.5;
        double r85457 = 2.0;
        double r85458 = pow(r85451, r85457);
        double r85459 = pow(r85450, r85457);
        double r85460 = r85458 / r85459;
        double r85461 = r85456 * r85460;
        double r85462 = r85455 - r85461;
        double r85463 = fma(r85451, r85450, r85462);
        double r85464 = log(r85452);
        double r85465 = 1.0;
        double r85466 = pow(r85464, r85465);
        double r85467 = r85454 ? r85463 : r85466;
        return r85467;
}

Error

Bits error versus x

Target

Original39.3
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.0000000000568305

    1. Initial program 59.3

      \[\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.0000000000568305 < (+ 1.0 x)

    1. Initial program 0.5

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

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

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

Reproduce

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