Average Error: 38.8 → 0.2
Time: 4.3s
Precision: 64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;1 + x \le 1.0001395119845617:\\ \;\;\;\;\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.0001395119845617:\\
\;\;\;\;\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 r73022 = 1.0;
        double r73023 = x;
        double r73024 = r73022 + r73023;
        double r73025 = log(r73024);
        return r73025;
}

double f(double x) {
        double r73026 = 1.0;
        double r73027 = x;
        double r73028 = r73026 + r73027;
        double r73029 = 1.0001395119845617;
        bool r73030 = r73028 <= r73029;
        double r73031 = -r73026;
        double r73032 = 3.0;
        double r73033 = pow(r73027, r73032);
        double r73034 = 2.0;
        double r73035 = pow(r73026, r73034);
        double r73036 = r73033 / r73035;
        double r73037 = fma(r73027, r73027, r73036);
        double r73038 = 0.3333333333333333;
        double r73039 = pow(r73026, r73032);
        double r73040 = r73033 / r73039;
        double r73041 = 0.5;
        double r73042 = pow(r73027, r73034);
        double r73043 = r73042 / r73035;
        double r73044 = r73026 * r73027;
        double r73045 = fma(r73041, r73043, r73044);
        double r73046 = fma(r73026, r73033, r73045);
        double r73047 = fma(r73038, r73040, r73046);
        double r73048 = fma(r73031, r73037, r73047);
        double r73049 = log(r73028);
        double r73050 = r73030 ? r73048 : r73049;
        return r73050;
}

Error

Bits error versus x

Target

Original38.8
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.0001395119845617

    1. Initial program 58.7

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

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

      \[\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.0001395119845617 < (+ 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.0001395119845617:\\ \;\;\;\;\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 2020049 +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)))