Average Error: 13.4 → 1.0
Time: 7.9s
Precision: binary64
Cost: 14401
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
\[\begin{array}{l} \mathbf{if}\;wj \leq 1.4176093501666414 \cdot 10^{-12}:\\ \;\;\;\;x + wj \cdot \left(wj - wj \cdot wj\right)\\ \mathbf{else}:\\ \;\;\;\;wj + \frac{\frac{x}{e^{wj}} - wj}{1 + {wj}^{3}} \cdot \left(wj \cdot wj + \left(1 - wj\right)\right)\\ \end{array}\]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\begin{array}{l}
\mathbf{if}\;wj \leq 1.4176093501666414 \cdot 10^{-12}:\\
\;\;\;\;x + wj \cdot \left(wj - wj \cdot wj\right)\\

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

\end{array}
(FPCore (wj x)
 :precision binary64
 (- wj (/ (- (* wj (exp wj)) x) (+ (exp wj) (* wj (exp wj))))))
(FPCore (wj x)
 :precision binary64
 (if (<= wj 1.4176093501666414e-12)
   (+ x (* wj (- wj (* wj wj))))
   (+
    wj
    (*
     (/ (- (/ x (exp wj)) wj) (+ 1.0 (pow wj 3.0)))
     (+ (* wj wj) (- 1.0 wj))))))
double code(double wj, double x) {
	return wj - (((wj * exp(wj)) - x) / (exp(wj) + (wj * exp(wj))));
}
double code(double wj, double x) {
	double tmp;
	if (wj <= 1.4176093501666414e-12) {
		tmp = x + (wj * (wj - (wj * wj)));
	} else {
		tmp = wj + ((((x / exp(wj)) - wj) / (1.0 + pow(wj, 3.0))) * ((wj * wj) + (1.0 - wj)));
	}
	return tmp;
}

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.4
Target12.6
Herbie1.0
\[wj - \left(\frac{wj}{wj + 1} - \frac{x}{e^{wj} + wj \cdot e^{wj}}\right)\]

Alternatives

Alternative 1
Error1.0
Cost7425
\[\begin{array}{l} \mathbf{if}\;wj \leq 4.44488751859428 \cdot 10^{-12}:\\ \;\;\;\;x + wj \cdot \left(wj - wj \cdot wj\right)\\ \mathbf{else}:\\ \;\;\;\;wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}\\ \end{array}\]
Alternative 2
Error1.5
Cost897
\[\begin{array}{l} \mathbf{if}\;wj \leq 0.6520064569069947:\\ \;\;\;\;x + wj \cdot \left(wj - wj \cdot wj\right)\\ \mathbf{else}:\\ \;\;\;\;wj - \frac{wj}{wj + 1}\\ \end{array}\]
Alternative 3
Error1.7
Cost769
\[\begin{array}{l} \mathbf{if}\;wj \leq 0.6520064569069947:\\ \;\;\;\;x + wj \cdot wj\\ \mathbf{else}:\\ \;\;\;\;wj - \frac{wj}{wj + 1}\\ \end{array}\]
Alternative 4
Error2.1
Cost641
\[\begin{array}{l} \mathbf{if}\;wj \leq 3.947283906137509:\\ \;\;\;\;x + wj \cdot wj\\ \mathbf{else}:\\ \;\;\;\;wj - 1\\ \end{array}\]
Alternative 5
Error8.9
Cost513
\[\begin{array}{l} \mathbf{if}\;wj \leq 1.0190921207572299:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;wj - 1\\ \end{array}\]
Alternative 6
Error9.5
Cost64
\[x\]
Alternative 7
Error61.4
Cost64
\[1\]

Error

Derivation

  1. Split input into 2 regimes
  2. if wj < 1.4176093501666414e-12

    1. Initial program 12.9

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

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

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

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

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

      \[\leadsto x + \color{blue}{\left(wj \cdot wj - {wj}^{3}\right)}\]
    7. Using strategy rm
    8. Applied unpow3_binary64_49180.9

      \[\leadsto x + \left(wj \cdot wj - \color{blue}{\left(wj \cdot wj\right) \cdot wj}\right)\]
    9. Applied distribute-rgt-out--_binary64_48060.9

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

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

    if 1.4176093501666414e-12 < wj

    1. Initial program 25.5

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

      \[\leadsto \color{blue}{wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}}\]
    3. Using strategy rm
    4. Applied flip3-+_binary64_48554.7

      \[\leadsto wj + \frac{\frac{x}{e^{wj}} - wj}{\color{blue}{\frac{{wj}^{3} + {1}^{3}}{wj \cdot wj + \left(1 \cdot 1 - wj \cdot 1\right)}}}\]
    5. Applied associate-/r/_binary64_47984.6

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;wj \leq 1.4176093501666414 \cdot 10^{-12}:\\ \;\;\;\;x + wj \cdot \left(wj - wj \cdot wj\right)\\ \mathbf{else}:\\ \;\;\;\;wj + \frac{\frac{x}{e^{wj}} - wj}{1 + {wj}^{3}} \cdot \left(wj \cdot wj + \left(1 - wj\right)\right)\\ \end{array}\]

Reproduce

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