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

\mathbf{else}:\\
\;\;\;\;\log \left(1 + x\right) \cdot \frac{1}{2} + \log \left(\sqrt{1 + x}\right)\\

\end{array}
double f(double x) {
        double r36457 = 1.0;
        double r36458 = x;
        double r36459 = r36457 + r36458;
        double r36460 = log(r36459);
        return r36460;
}

double f(double x) {
        double r36461 = x;
        double r36462 = 6.379153737872092e-06;
        bool r36463 = r36461 <= r36462;
        double r36464 = 1.0;
        double r36465 = r36461 / r36464;
        double r36466 = r36465 * r36465;
        double r36467 = -0.5;
        double r36468 = log(r36464);
        double r36469 = fma(r36461, r36464, r36468);
        double r36470 = fma(r36466, r36467, r36469);
        double r36471 = r36464 + r36461;
        double r36472 = log(r36471);
        double r36473 = 0.5;
        double r36474 = r36472 * r36473;
        double r36475 = sqrt(r36471);
        double r36476 = log(r36475);
        double r36477 = r36474 + r36476;
        double r36478 = r36463 ? r36470 : r36477;
        return r36478;
}

Error

Bits error versus x

Target

Original39.3
Target0.2
Herbie0.3
\[\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 x < 6.379153737872092e-06

    1. Initial program 59.0

      \[\log \left(1 + x\right)\]
    2. Simplified59.0

      \[\leadsto \color{blue}{\log \left(x + 1\right)}\]
    3. Taylor expanded around 0 0.4

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

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

    if 6.379153737872092e-06 < x

    1. Initial program 0.1

      \[\log \left(1 + x\right)\]
    2. Simplified0.1

      \[\leadsto \color{blue}{\log \left(x + 1\right)}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt0.1

      \[\leadsto \log \color{blue}{\left(\sqrt{x + 1} \cdot \sqrt{x + 1}\right)}\]
    5. Applied log-prod0.1

      \[\leadsto \color{blue}{\log \left(\sqrt{x + 1}\right) + \log \left(\sqrt{x + 1}\right)}\]
    6. Simplified0.1

      \[\leadsto \color{blue}{\log \left(\sqrt{1 + x}\right)} + \log \left(\sqrt{x + 1}\right)\]
    7. Simplified0.1

      \[\leadsto \log \left(\sqrt{1 + x}\right) + \color{blue}{\log \left(\sqrt{1 + x}\right)}\]
    8. Using strategy rm
    9. Applied pow1/20.1

      \[\leadsto \log \color{blue}{\left({\left(1 + x\right)}^{\frac{1}{2}}\right)} + \log \left(\sqrt{1 + x}\right)\]
    10. Applied log-pow0.1

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

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

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

Reproduce

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