Average Error: 13.5 → 1.2
Time: 5.4s
Precision: 64
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
\[\begin{array}{l} \mathbf{if}\;wj \le 7.68538994010771746 \cdot 10^{-7}:\\ \;\;\;\;\mathsf{fma}\left(wj, wj, x\right) - 2 \cdot \left(wj \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\sqrt{\frac{\frac{x}{wj + 1}}{e^{wj}} + wj}, \sqrt{\frac{\frac{x}{wj + 1}}{e^{wj}} + 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}\;wj \le 7.68538994010771746 \cdot 10^{-7}:\\
\;\;\;\;\mathsf{fma}\left(wj, wj, x\right) - 2 \cdot \left(wj \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\sqrt{\frac{\frac{x}{wj + 1}}{e^{wj}} + wj}, \sqrt{\frac{\frac{x}{wj + 1}}{e^{wj}} + wj}, -\frac{wj}{wj + 1}\right)\\

\end{array}
double code(double wj, double x) {
	return (wj - (((wj * exp(wj)) - x) / (exp(wj) + (wj * exp(wj)))));
}
double code(double wj, double x) {
	double VAR;
	if ((wj <= 7.685389940107717e-07)) {
		VAR = (fma(wj, wj, x) - (2.0 * (wj * x)));
	} else {
		VAR = fma(sqrt((((x / (wj + 1.0)) / exp(wj)) + wj)), sqrt((((x / (wj + 1.0)) / exp(wj)) + wj)), -(wj / (wj + 1.0)));
	}
	return VAR;
}

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.5
Target12.7
Herbie1.2
\[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 < 7.685389940107717e-07

    1. Initial program 13.0

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

      \[\leadsto \color{blue}{\left(\frac{\frac{x}{wj + 1}}{e^{wj}} + wj\right) - \frac{wj}{wj + 1}}\]
    3. Taylor expanded around 0 0.9

      \[\leadsto \color{blue}{\left(x + {wj}^{2}\right) - 2 \cdot \left(wj \cdot x\right)}\]
    4. Taylor expanded around 0 0.9

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

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

    if 7.685389940107717e-07 < wj

    1. Initial program 32.9

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

      \[\leadsto \color{blue}{\left(\frac{\frac{x}{wj + 1}}{e^{wj}} + wj\right) - \frac{wj}{wj + 1}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt11.3

      \[\leadsto \color{blue}{\sqrt{\frac{\frac{x}{wj + 1}}{e^{wj}} + wj} \cdot \sqrt{\frac{\frac{x}{wj + 1}}{e^{wj}} + wj}} - \frac{wj}{wj + 1}\]
    5. Applied fma-neg11.2

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

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

Reproduce

herbie shell --seed 2020075 +o rules:numerics
(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))))))