Average Error: 13.4 → 2.7
Time: 4.5s
Precision: 64
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
\[\begin{array}{l} \mathbf{if}\;x \le 1.15551325306561863 \cdot 10^{-202}:\\ \;\;\;\;x + wj \cdot \left(wj - 2 \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\left(wj - \frac{wj}{\frac{\left(wj + 1\right) \cdot e^{wj}}{e^{wj}}}\right) + \frac{x}{e^{wj} + wj \cdot e^{wj}}\\ \end{array}\]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\begin{array}{l}
\mathbf{if}\;x \le 1.15551325306561863 \cdot 10^{-202}:\\
\;\;\;\;x + wj \cdot \left(wj - 2 \cdot x\right)\\

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

\end{array}
double f(double wj, double x) {
        double r286846 = wj;
        double r286847 = exp(r286846);
        double r286848 = r286846 * r286847;
        double r286849 = x;
        double r286850 = r286848 - r286849;
        double r286851 = r286847 + r286848;
        double r286852 = r286850 / r286851;
        double r286853 = r286846 - r286852;
        return r286853;
}

double f(double wj, double x) {
        double r286854 = x;
        double r286855 = 1.1555132530656186e-202;
        bool r286856 = r286854 <= r286855;
        double r286857 = wj;
        double r286858 = 2.0;
        double r286859 = r286858 * r286854;
        double r286860 = r286857 - r286859;
        double r286861 = r286857 * r286860;
        double r286862 = r286854 + r286861;
        double r286863 = 1.0;
        double r286864 = r286857 + r286863;
        double r286865 = exp(r286857);
        double r286866 = r286864 * r286865;
        double r286867 = r286866 / r286865;
        double r286868 = r286857 / r286867;
        double r286869 = r286857 - r286868;
        double r286870 = r286857 * r286865;
        double r286871 = r286865 + r286870;
        double r286872 = r286854 / r286871;
        double r286873 = r286869 + r286872;
        double r286874 = r286856 ? r286862 : r286873;
        return r286874;
}

Error

Bits error versus wj

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original13.4
Target12.7
Herbie2.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 x < 1.1555132530656186e-202

    1. Initial program 17.9

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

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

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

    if 1.1555132530656186e-202 < x

    1. Initial program 6.9

      \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
    2. Using strategy rm
    3. Applied div-sub6.9

      \[\leadsto wj - \color{blue}{\left(\frac{wj \cdot e^{wj}}{e^{wj} + wj \cdot e^{wj}} - \frac{x}{e^{wj} + wj \cdot e^{wj}}\right)}\]
    4. Applied associate--r-3.8

      \[\leadsto \color{blue}{\left(wj - \frac{wj \cdot e^{wj}}{e^{wj} + wj \cdot e^{wj}}\right) + \frac{x}{e^{wj} + wj \cdot e^{wj}}}\]
    5. Simplified3.8

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

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

Reproduce

herbie shell --seed 2020024 
(FPCore (wj x)
  :name "Jmat.Real.lambertw, newton loop step"
  :precision binary64

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

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