Average Error: 13.6 → 0.4
Time: 19.4s
Precision: binary64
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
\[\begin{array}{l} \mathbf{if}\;\begin{array}{l} t_0 := wj \cdot e^{wj}\\ wj - \frac{t_0 - x}{e^{wj} + t_0} \leq 2.063916403535308 \cdot 10^{-13} \end{array}:\\ \;\;\;\;\left(\mathsf{fma}\left(wj, wj, x\right) + x \cdot \mathsf{fma}\left(wj, \mathsf{fma}\left(2.5, wj, -2\right), {wj}^{3} \cdot -2.6666666666666665\right)\right) - {wj}^{3}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{e^{wj} \cdot \left(wj + 1\right)} + \left(wj - \frac{wj}{wj + 1}\right)\\ \end{array} \]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\begin{array}{l}
\mathbf{if}\;\begin{array}{l}
t_0 := wj \cdot e^{wj}\\
wj - \frac{t_0 - x}{e^{wj} + t_0} \leq 2.063916403535308 \cdot 10^{-13}
\end{array}:\\
\;\;\;\;\left(\mathsf{fma}\left(wj, wj, x\right) + x \cdot \mathsf{fma}\left(wj, \mathsf{fma}\left(2.5, wj, -2\right), {wj}^{3} \cdot -2.6666666666666665\right)\right) - {wj}^{3}\\

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


\end{array}
(FPCore (wj x)
 :precision binary64
 (- wj (/ (- (* wj (exp wj)) x) (+ (exp wj) (* wj (exp wj))))))
(FPCore (wj x)
 :precision binary64
 (if (let* ((t_0 (* wj (exp wj))))
       (<= (- wj (/ (- t_0 x) (+ (exp wj) t_0))) 2.063916403535308e-13))
   (-
    (+
     (fma wj wj x)
     (* x (fma wj (fma 2.5 wj -2.0) (* (pow wj 3.0) -2.6666666666666665))))
    (pow wj 3.0))
   (+ (/ x (* (exp wj) (+ wj 1.0))) (- wj (/ wj (+ wj 1.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 = wj * exp(wj);
	double tmp;
	if ((wj - ((t_0 - x) / (exp(wj) + t_0))) <= 2.063916403535308e-13) {
		tmp = (fma(wj, wj, x) + (x * fma(wj, fma(2.5, wj, -2.0), (pow(wj, 3.0) * -2.6666666666666665)))) - pow(wj, 3.0);
	} else {
		tmp = (x / (exp(wj) * (wj + 1.0))) + (wj - (wj / (wj + 1.0)));
	}
	return tmp;
}

Error

Bits error versus wj

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj))))) < 2.0639164035e-13

    1. Initial program 18.0

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

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

      \[\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.4

      \[\leadsto \color{blue}{\mathsf{fma}\left(2.5, x \cdot \left(wj \cdot wj\right), \mathsf{fma}\left(wj, wj, x\right)\right) - \mathsf{fma}\left(x, \mathsf{fma}\left(2, wj, 2.6666666666666665 \cdot {wj}^{3}\right), {wj}^{3}\right)} \]
    5. Applied fma-udef_binary640.4

      \[\leadsto \mathsf{fma}\left(2.5, x \cdot \left(wj \cdot wj\right), \mathsf{fma}\left(wj, wj, x\right)\right) - \color{blue}{\left(x \cdot \mathsf{fma}\left(2, wj, 2.6666666666666665 \cdot {wj}^{3}\right) + {wj}^{3}\right)} \]
    6. Applied associate--r+_binary640.4

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

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

    if 2.0639164035e-13 < (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj)))))

    1. Initial program 2.4

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

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

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

      \[\leadsto \color{blue}{1 \cdot wj} + 1 \cdot \frac{\frac{x}{e^{wj}} - wj}{wj + 1} \]
    5. Applied distribute-lft-out_binary640.3

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

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

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

Reproduce

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