Average Error: 13.3 → 1.7
Time: 22.4s
Precision: 64
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
\[\begin{array}{l} \mathbf{if}\;wj \le 5.475491025907151 \cdot 10^{-09}:\\ \;\;\;\;\left(x \cdot wj\right) \cdot -2 + \mathsf{fma}\left(wj, wj, x\right)\\ \mathbf{else}:\\ \;\;\;\;wj - \frac{e^{wj} \cdot wj - x}{e^{wj} \cdot \left(1 + wj\right)}\\ \end{array}\]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\begin{array}{l}
\mathbf{if}\;wj \le 5.475491025907151 \cdot 10^{-09}:\\
\;\;\;\;\left(x \cdot wj\right) \cdot -2 + \mathsf{fma}\left(wj, wj, x\right)\\

\mathbf{else}:\\
\;\;\;\;wj - \frac{e^{wj} \cdot wj - x}{e^{wj} \cdot \left(1 + wj\right)}\\

\end{array}
double f(double wj, double x) {
        double r7630386 = wj;
        double r7630387 = exp(r7630386);
        double r7630388 = r7630386 * r7630387;
        double r7630389 = x;
        double r7630390 = r7630388 - r7630389;
        double r7630391 = r7630387 + r7630388;
        double r7630392 = r7630390 / r7630391;
        double r7630393 = r7630386 - r7630392;
        return r7630393;
}

double f(double wj, double x) {
        double r7630394 = wj;
        double r7630395 = 5.475491025907151e-09;
        bool r7630396 = r7630394 <= r7630395;
        double r7630397 = x;
        double r7630398 = r7630397 * r7630394;
        double r7630399 = -2.0;
        double r7630400 = r7630398 * r7630399;
        double r7630401 = fma(r7630394, r7630394, r7630397);
        double r7630402 = r7630400 + r7630401;
        double r7630403 = exp(r7630394);
        double r7630404 = r7630403 * r7630394;
        double r7630405 = r7630404 - r7630397;
        double r7630406 = 1.0;
        double r7630407 = r7630406 + r7630394;
        double r7630408 = r7630403 * r7630407;
        double r7630409 = r7630405 / r7630408;
        double r7630410 = r7630394 - r7630409;
        double r7630411 = r7630396 ? r7630402 : r7630410;
        return r7630411;
}

Error

Bits error versus wj

Bits error versus x

Target

Original13.3
Target12.6
Herbie1.7
\[wj - \left(\frac{wj}{wj + 1} - \frac{x}{e^{wj} + wj \cdot e^{wj}}\right)\]

Derivation

  1. Split input into 2 regimes
  2. if wj < 5.475491025907151e-09

    1. Initial program 12.9

      \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
    2. Taylor expanded around 0 0.9

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(wj, wj, x\right) + \left(x \cdot wj\right) \cdot -2}\]

    if 5.475491025907151e-09 < wj

    1. Initial program 24.6

      \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
    2. Using strategy rm
    3. Applied distribute-rgt1-in24.8

      \[\leadsto wj - \frac{wj \cdot e^{wj} - x}{\color{blue}{\left(wj + 1\right) \cdot e^{wj}}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification1.7

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

Reproduce

herbie shell --seed 2019129 +o rules:numerics
(FPCore (wj x)
  :name "Jmat.Real.lambertw, newton loop step"

  :herbie-target
  (- wj (- (/ wj (+ wj 1)) (/ x (+ (exp wj) (* wj (exp wj))))))

  (- wj (/ (- (* wj (exp wj)) x) (+ (exp wj) (* wj (exp wj))))))