Average Error: 13.8 → 0.9
Time: 4.1s
Precision: binary64
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
\[\begin{array}{l} \mathbf{if}\;wj \le 9.2952183473196775 \cdot 10^{-9}:\\ \;\;\;\;\left(x + {wj}^{2}\right) - 2 \cdot \left(wj \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot e^{\left(0 - \log \left(wj + 1\right)\right) - wj} + wj\right) - \frac{wj}{wj + 1}\\ \end{array}\]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\begin{array}{l}
\mathbf{if}\;wj \le 9.2952183473196775 \cdot 10^{-9}:\\
\;\;\;\;\left(x + {wj}^{2}\right) - 2 \cdot \left(wj \cdot x\right)\\

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

\end{array}
double code(double wj, double x) {
	return ((double) (wj - ((double) (((double) (((double) (wj * ((double) exp(wj)))) - x)) / ((double) (((double) exp(wj)) + ((double) (wj * ((double) exp(wj))))))))));
}
double code(double wj, double x) {
	double VAR;
	if ((wj <= 9.295218347319678e-09)) {
		VAR = ((double) (((double) (x + ((double) pow(wj, 2.0)))) - ((double) (2.0 * ((double) (wj * x))))));
	} else {
		VAR = ((double) (((double) (((double) (x * ((double) exp(((double) (((double) (0.0 - ((double) log(((double) (wj + 1.0)))))) - wj)))))) + wj)) - ((double) (wj / ((double) (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.8
Target13.1
Herbie0.9
\[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 < 9.2952183473196775e-9

    1. Initial program 13.4

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

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

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

    if 9.2952183473196775e-9 < wj

    1. Initial program 28.1

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

      \[\leadsto \color{blue}{\left(\frac{\frac{x}{wj + 1}}{e^{wj}} + wj\right) - \frac{wj}{wj + 1}}\]
    3. Using strategy rm
    4. Applied *-un-lft-identity2.7

      \[\leadsto \left(\frac{\frac{x}{wj + 1}}{\color{blue}{1 \cdot e^{wj}}} + wj\right) - \frac{wj}{wj + 1}\]
    5. Applied div-inv2.7

      \[\leadsto \left(\frac{\color{blue}{x \cdot \frac{1}{wj + 1}}}{1 \cdot e^{wj}} + wj\right) - \frac{wj}{wj + 1}\]
    6. Applied times-frac2.7

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

      \[\leadsto \left(\color{blue}{x} \cdot \frac{\frac{1}{wj + 1}}{e^{wj}} + wj\right) - \frac{wj}{wj + 1}\]
    8. Using strategy rm
    9. Applied add-exp-log2.8

      \[\leadsto \left(x \cdot \frac{\frac{1}{\color{blue}{e^{\log \left(wj + 1\right)}}}}{e^{wj}} + wj\right) - \frac{wj}{wj + 1}\]
    10. Applied rec-exp2.8

      \[\leadsto \left(x \cdot \frac{\color{blue}{e^{-\log \left(wj + 1\right)}}}{e^{wj}} + wj\right) - \frac{wj}{wj + 1}\]
    11. Applied div-exp2.8

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

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

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

Reproduce

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