Average Error: 38.8 → 0.5
Time: 2.8s
Precision: binary64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;1 + x \leq 1.0000000000000153:\\ \;\;\;\;x + x \cdot \left(x \cdot \left(x \cdot 0.3333333333333333 + -0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\sqrt{1 + x}\right) + 0.5 \cdot \log \left(1 + x\right)\\ \end{array}\]
\log \left(1 + x\right)
\begin{array}{l}
\mathbf{if}\;1 + x \leq 1.0000000000000153:\\
\;\;\;\;x + x \cdot \left(x \cdot \left(x \cdot 0.3333333333333333 + -0.5\right)\right)\\

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

\end{array}
(FPCore (x) :precision binary64 (log (+ 1.0 x)))
(FPCore (x)
 :precision binary64
 (if (<= (+ 1.0 x) 1.0000000000000153)
   (+ x (* x (* x (+ (* x 0.3333333333333333) -0.5))))
   (+ (log (sqrt (+ 1.0 x))) (* 0.5 (log (+ 1.0 x))))))
double code(double x) {
	return log(1.0 + x);
}
double code(double x) {
	double tmp;
	if ((1.0 + x) <= 1.0000000000000153) {
		tmp = x + (x * (x * ((x * 0.3333333333333333) + -0.5)));
	} else {
		tmp = log(sqrt(1.0 + x)) + (0.5 * log(1.0 + x));
	}
	return tmp;
}

Error

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.8
Target0.3
Herbie0.5
\[\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 (+.f64 1 x) < 1.00000000000001532

    1. Initial program 59.3

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

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

      \[\leadsto \color{blue}{x \cdot \left(1 + x \cdot \left(-0.5 + x \cdot 0.3333333333333333\right)\right)}\]
    4. Using strategy rm
    5. Applied distribute-rgt-in_binary64_6330.3

      \[\leadsto \color{blue}{1 \cdot x + \left(x \cdot \left(-0.5 + x \cdot 0.3333333333333333\right)\right) \cdot x}\]
    6. Simplified0.3

      \[\leadsto \color{blue}{x} + \left(x \cdot \left(-0.5 + x \cdot 0.3333333333333333\right)\right) \cdot x\]
    7. Simplified0.3

      \[\leadsto x + \color{blue}{x \cdot \left(x \cdot \left(x \cdot 0.3333333333333333 + -0.5\right)\right)}\]

    if 1.00000000000001532 < (+.f64 1 x)

    1. Initial program 1.0

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;1 + x \leq 1.0000000000000153:\\ \;\;\;\;x + x \cdot \left(x \cdot \left(x \cdot 0.3333333333333333 + -0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\sqrt{1 + x}\right) + 0.5 \cdot \log \left(1 + x\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2020232 
(FPCore (x)
  :name "ln(1 + x)"
  :precision binary64

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

  (log (+ 1.0 x)))