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

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

\end{array}
double f(double x) {
        double r58828 = 1.0;
        double r58829 = x;
        double r58830 = r58828 + r58829;
        double r58831 = log(r58830);
        return r58831;
}

double f(double x) {
        double r58832 = x;
        double r58833 = 1.0;
        double r58834 = r58832 + r58833;
        double r58835 = 1.000051798647897;
        bool r58836 = r58834 <= r58835;
        double r58837 = 0.3333333333333333;
        double r58838 = r58837 * r58832;
        double r58839 = 0.5;
        double r58840 = r58838 - r58839;
        double r58841 = r58832 * r58832;
        double r58842 = r58840 * r58841;
        double r58843 = fma(r58833, r58832, r58842);
        double r58844 = log(r58834);
        double r58845 = r58836 ? r58843 : r58844;
        return r58845;
}

Error

Bits error versus x

Target

Original39.0
Target0.3
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.000051798647897

    1. Initial program 58.9

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

      \[\leadsto \log \color{blue}{\left(\frac{1 \cdot 1 - x \cdot x}{1 - x}\right)}\]
    4. Applied log-div58.9

      \[\leadsto \color{blue}{\log \left(1 \cdot 1 - x \cdot x\right) - \log \left(1 - x\right)}\]
    5. Simplified58.9

      \[\leadsto \color{blue}{\log \left(\mathsf{fma}\left(1, 1, -x \cdot x\right)\right)} - \log \left(1 - x\right)\]
    6. Taylor expanded around 0 0.2

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{{x}^{3}}{{1}^{3}}, \frac{1}{3}, \mathsf{fma}\left(\frac{\frac{1}{2}}{1}, \frac{x \cdot x}{1}, 1 \cdot x\right)\right) - 1 \cdot \left(x \cdot x\right)}\]
    8. Taylor expanded around 0 0.2

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

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

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

Reproduce

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