Average Error: 13.9 → 0.1
Time: 11.4s
Precision: binary64
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
\[\begin{array}{l} t_0 := \frac{x}{e^{wj}} - wj\\ \mathbf{if}\;wj \leq -2.6140157849411603 \cdot 10^{-6}:\\ \;\;\;\;wj + \frac{t_0}{wj + 1}\\ \mathbf{elif}\;wj \leq 3.8566291204319244 \cdot 10^{-10}:\\ \;\;\;\;\left(\mathsf{fma}\left(wj, wj, x\right) + x \cdot \mathsf{fma}\left(2.5, wj \cdot wj, wj \cdot -2\right)\right) - {wj}^{3}\\ \mathbf{else}:\\ \;\;\;\;wj + \frac{1}{\frac{wj + 1}{t_0}}\\ \end{array} \]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\begin{array}{l}
t_0 := \frac{x}{e^{wj}} - wj\\
\mathbf{if}\;wj \leq -2.6140157849411603 \cdot 10^{-6}:\\
\;\;\;\;wj + \frac{t_0}{wj + 1}\\

\mathbf{elif}\;wj \leq 3.8566291204319244 \cdot 10^{-10}:\\
\;\;\;\;\left(\mathsf{fma}\left(wj, wj, x\right) + x \cdot \mathsf{fma}\left(2.5, wj \cdot wj, wj \cdot -2\right)\right) - {wj}^{3}\\

\mathbf{else}:\\
\;\;\;\;wj + \frac{1}{\frac{wj + 1}{t_0}}\\


\end{array}
(FPCore (wj x)
 :precision binary64
 (- wj (/ (- (* wj (exp wj)) x) (+ (exp wj) (* wj (exp wj))))))
(FPCore (wj x)
 :precision binary64
 (let* ((t_0 (- (/ x (exp wj)) wj)))
   (if (<= wj -2.6140157849411603e-6)
     (+ wj (/ t_0 (+ wj 1.0)))
     (if (<= wj 3.8566291204319244e-10)
       (- (+ (fma wj wj x) (* x (fma 2.5 (* wj wj) (* wj -2.0)))) (pow wj 3.0))
       (+ wj (/ 1.0 (/ (+ wj 1.0) t_0)))))))
double code(double wj, double x) {
	return wj - (((wj * exp(wj)) - x) / (exp(wj) + (wj * exp(wj))));
}
double code(double wj, double x) {
	double t_0 = (x / exp(wj)) - wj;
	double tmp;
	if (wj <= -2.6140157849411603e-6) {
		tmp = wj + (t_0 / (wj + 1.0));
	} else if (wj <= 3.8566291204319244e-10) {
		tmp = (fma(wj, wj, x) + (x * fma(2.5, (wj * wj), (wj * -2.0)))) - pow(wj, 3.0);
	} else {
		tmp = wj + (1.0 / ((wj + 1.0) / t_0));
	}
	return tmp;
}

Error

Bits error versus wj

Bits error versus x

Target

Original13.9
Target13.3
Herbie0.1
\[wj - \left(\frac{wj}{wj + 1} - \frac{x}{e^{wj} + wj \cdot e^{wj}}\right) \]

Derivation

  1. Split input into 3 regimes
  2. if wj < -2.61401578494116029e-6

    1. Initial program 31.1

      \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
    2. Simplified1.2

      \[\leadsto \color{blue}{wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}} \]
    3. Applied *-un-lft-identity_binary641.2

      \[\leadsto wj + \frac{\frac{x}{e^{wj}} - \color{blue}{1 \cdot wj}}{wj + 1} \]
    4. Applied cancel-sign-sub-inv_binary641.2

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

    if -2.61401578494116029e-6 < wj < 3.85662912043192441e-10

    1. Initial program 13.2

      \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
    2. Simplified13.2

      \[\leadsto \color{blue}{wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}} \]
    3. Taylor expanded in wj around 0 0.0

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

      \[\leadsto \color{blue}{\left(\mathsf{fma}\left(wj, wj, x\right) + x \cdot \mathsf{fma}\left(2.5, wj \cdot wj, wj \cdot -2\right)\right) - {wj}^{3} \cdot \left(2.6666666666666665 \cdot x + 1\right)} \]
    5. Taylor expanded in x around 0 0.0

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

    if 3.85662912043192441e-10 < wj

    1. Initial program 24.3

      \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
    2. Simplified3.0

      \[\leadsto \color{blue}{wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}} \]
    3. Applied clear-num_binary643.1

      \[\leadsto wj + \color{blue}{\frac{1}{\frac{wj + 1}{\frac{x}{e^{wj}} - wj}}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;wj \leq -2.6140157849411603 \cdot 10^{-6}:\\ \;\;\;\;wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}\\ \mathbf{elif}\;wj \leq 3.8566291204319244 \cdot 10^{-10}:\\ \;\;\;\;\left(\mathsf{fma}\left(wj, wj, x\right) + x \cdot \mathsf{fma}\left(2.5, wj \cdot wj, wj \cdot -2\right)\right) - {wj}^{3}\\ \mathbf{else}:\\ \;\;\;\;wj + \frac{1}{\frac{wj + 1}{\frac{x}{e^{wj}} - wj}}\\ \end{array} \]

Reproduce

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

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

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