Average Error: 13.2 → 10.7
Time: 7.6s
Precision: binary64
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
\[\begin{array}{l} t_0 := \mathsf{fma}\left(wj, e^{wj}, e^{wj}\right)\\ \mathbf{if}\;wj \leq -1.7279931553913336 \cdot 10^{-158}:\\ \;\;\;\;wj - x \cdot -1\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{t_0} + \left(wj - \frac{wj \cdot e^{wj}}{t_0}\right)\\ \end{array} \]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\begin{array}{l}
t_0 := \mathsf{fma}\left(wj, e^{wj}, e^{wj}\right)\\
\mathbf{if}\;wj \leq -1.7279931553913336 \cdot 10^{-158}:\\
\;\;\;\;wj - x \cdot -1\\

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


\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 (fma wj (exp wj) (exp wj))))
   (if (<= wj -1.7279931553913336e-158)
     (- wj (* x -1.0))
     (+ (/ x t_0) (- wj (/ (* wj (exp wj)) 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 = fma(wj, exp(wj), exp(wj));
	double tmp;
	if (wj <= -1.7279931553913336e-158) {
		tmp = wj - (x * -1.0);
	} else {
		tmp = (x / t_0) + (wj - ((wj * exp(wj)) / t_0));
	}
	return tmp;
}

Error

Bits error versus wj

Bits error versus x

Target

Original13.2
Target12.6
Herbie10.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 < -1.72799315539133361e-158

    1. Initial program 19.1

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

      \[\leadsto wj - \color{blue}{-1 \cdot x} \]
    3. Simplified25.2

      \[\leadsto wj - \color{blue}{x \cdot -1} \]

    if -1.72799315539133361e-158 < wj

    1. Initial program 11.1

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

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

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

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

Reproduce

herbie shell --seed 2022127 
(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))))))