Average Error: 39.2 → 0.3
Time: 5.7s
Precision: 64
\[\log \left(1 + x\right)\]
\[\begin{array}{l} \mathbf{if}\;x \le 4.228318145661536008386716112283920665504 \cdot 10^{-6}:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{1} \cdot \frac{x}{1}, \frac{-1}{2}, \mathsf{fma}\left(x, 1, \log 1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array}\]
\log \left(1 + x\right)
\begin{array}{l}
\mathbf{if}\;x \le 4.228318145661536008386716112283920665504 \cdot 10^{-6}:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{1} \cdot \frac{x}{1}, \frac{-1}{2}, \mathsf{fma}\left(x, 1, \log 1\right)\right)\\

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

\end{array}
double f(double x) {
        double r27436 = 1.0;
        double r27437 = x;
        double r27438 = r27436 + r27437;
        double r27439 = log(r27438);
        return r27439;
}

double f(double x) {
        double r27440 = x;
        double r27441 = 4.228318145661536e-06;
        bool r27442 = r27440 <= r27441;
        double r27443 = 1.0;
        double r27444 = r27440 / r27443;
        double r27445 = r27444 * r27444;
        double r27446 = -0.5;
        double r27447 = log(r27443);
        double r27448 = fma(r27440, r27443, r27447);
        double r27449 = fma(r27445, r27446, r27448);
        double r27450 = r27443 + r27440;
        double r27451 = log(r27450);
        double r27452 = r27442 ? r27449 : r27451;
        return r27452;
}

Error

Bits error versus x

Target

Original39.2
Target0.2
Herbie0.3
\[\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 < 4.228318145661536e-06

    1. Initial program 59.2

      \[\log \left(1 + x\right)\]
    2. Simplified59.2

      \[\leadsto \color{blue}{\log \left(x + 1\right)}\]
    3. Taylor expanded around 0 0.3

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

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

    if 4.228318145661536e-06 < x

    1. Initial program 0.1

      \[\log \left(1 + x\right)\]
    2. Simplified0.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le 4.228318145661536008386716112283920665504 \cdot 10^{-6}:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{1} \cdot \frac{x}{1}, \frac{-1}{2}, \mathsf{fma}\left(x, 1, \log 1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array}\]

Reproduce

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