Average Error: 39.1 → 0.1
Time: 12.4s
Precision: 64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;x \le 0.0001323788943398679:\\ \;\;\;\;x + \left(x \cdot \left(\frac{-1}{2} + \frac{1}{3} \cdot x\right)\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;\log \left(\sqrt{x + 1}\right) + \log \left(x + 1\right) \cdot \frac{1}{2}\\ \end{array}\]

Error

Bits error versus x

Target

Original39.1
Target0.3
Herbie0.1
\[\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 < 0.0001323788943398679

    1. Initial program 59.0

      \[\log \left(1 + x\right)\]
    2. Taylor expanded around 0 0.2

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

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

    if 0.0001323788943398679 < x

    1. Initial program 0.1

      \[\log \left(1 + x\right)\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt0.1

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

      \[\leadsto \color{blue}{\log \left(\sqrt{1 + x}\right) + \log \left(\sqrt{1 + x}\right)}\]
    5. Using strategy rm
    6. Applied pow10.1

      \[\leadsto \log \left(\sqrt{1 + x}\right) + \log \left(\sqrt{\color{blue}{{\left(1 + x\right)}^{1}}}\right)\]
    7. Applied sqrt-pow10.1

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le 0.0001323788943398679:\\ \;\;\;\;x + \left(x \cdot \left(\frac{-1}{2} + \frac{1}{3} \cdot x\right)\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;\log \left(\sqrt{x + 1}\right) + \log \left(x + 1\right) \cdot \frac{1}{2}\\ \end{array}\]

Reproduce

herbie shell --seed 2019100 
(FPCore (x)
  :name "ln(1 + x)"

  :herbie-target
  (if (== (+ 1 x) 1) x (/ (* x (log (+ 1 x))) (- (+ 1 x) 1)))

  (log (+ 1 x)))