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

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

\end{array}
double f(double x) {
        double r83571 = 1.0;
        double r83572 = x;
        double r83573 = r83571 + r83572;
        double r83574 = log(r83573);
        return r83574;
}

double f(double x) {
        double r83575 = 1.0;
        double r83576 = x;
        double r83577 = r83575 + r83576;
        double r83578 = 1.00000000442313;
        bool r83579 = r83577 <= r83578;
        double r83580 = -r83575;
        double r83581 = 3.0;
        double r83582 = pow(r83576, r83581);
        double r83583 = 2.0;
        double r83584 = pow(r83575, r83583);
        double r83585 = r83582 / r83584;
        double r83586 = fma(r83576, r83576, r83585);
        double r83587 = 0.5;
        double r83588 = pow(r83576, r83583);
        double r83589 = 1.3333333333333333;
        double r83590 = r83589 * r83582;
        double r83591 = fma(r83575, r83576, r83590);
        double r83592 = fma(r83587, r83588, r83591);
        double r83593 = fma(r83580, r83586, r83592);
        double r83594 = log(r83577);
        double r83595 = r83579 ? r83593 : r83594;
        return r83595;
}

Error

Bits error versus x

Target

Original38.9
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.00000000442313

    1. Initial program 59.3

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

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

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

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

      \[\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)}\]
    7. Taylor expanded around 0 0.2

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

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

    if 1.00000000442313 < (+ 1.0 x)

    1. Initial program 0.3

      \[\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.00000000442313008:\\ \;\;\;\;\mathsf{fma}\left(-1, \mathsf{fma}\left(x, x, \frac{{x}^{3}}{{1}^{2}}\right), \mathsf{fma}\left(0.5, {x}^{2}, \mathsf{fma}\left(1, x, 1.33333333333333326 \cdot {x}^{3}\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array}\]

Reproduce

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