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

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

\end{array}
double f(double x) {
        double r2397273 = 1.0;
        double r2397274 = x;
        double r2397275 = r2397273 + r2397274;
        double r2397276 = log(r2397275);
        return r2397276;
}

double f(double x) {
        double r2397277 = x;
        double r2397278 = 6.0212587856537e-06;
        bool r2397279 = r2397277 <= r2397278;
        double r2397280 = -0.5;
        double r2397281 = 1.0;
        double r2397282 = r2397277 / r2397281;
        double r2397283 = r2397280 * r2397282;
        double r2397284 = log(r2397281);
        double r2397285 = fma(r2397281, r2397277, r2397284);
        double r2397286 = fma(r2397283, r2397282, r2397285);
        double r2397287 = r2397277 + r2397281;
        double r2397288 = log(r2397287);
        double r2397289 = r2397279 ? r2397286 : r2397288;
        return r2397289;
}

Error

Bits error versus x

Target

Original39.3
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 < 6.0212587856537e-06

    1. Initial program 59.1

      \[\log \left(1 + x\right)\]
    2. 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}}}\]
    3. Simplified0.3

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

    if 6.0212587856537e-06 < x

    1. Initial program 0.1

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

      \[\leadsto \color{blue}{\log \left(e^{\log \left(1 + x\right)}\right)}\]
    4. Simplified0.1

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

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

Reproduce

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