Average Error: 13.6 → 0.5
Time: 7.9s
Precision: binary64
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
\[\begin{array}{l} \mathbf{if}\;wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \leq 3.5043546686350665 \cdot 10^{-16}:\\ \;\;\;\;x + \left(\left(wj \cdot wj\right) \cdot \left(\left(x \cdot 2.5 + 1\right) - wj\right) + x \cdot \left(wj \cdot -2 - 2.6666666666666665 \cdot {wj}^{3}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;wj + \frac{\frac{\frac{x}{\sqrt{e^{wj}}}}{\sqrt{e^{wj}}} - wj}{wj + 1}\\ \end{array}\]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\begin{array}{l}
\mathbf{if}\;wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \leq 3.5043546686350665 \cdot 10^{-16}:\\
\;\;\;\;x + \left(\left(wj \cdot wj\right) \cdot \left(\left(x \cdot 2.5 + 1\right) - wj\right) + x \cdot \left(wj \cdot -2 - 2.6666666666666665 \cdot {wj}^{3}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;wj + \frac{\frac{\frac{x}{\sqrt{e^{wj}}}}{\sqrt{e^{wj}}} - wj}{wj + 1}\\

\end{array}
(FPCore (wj x)
 :precision binary64
 (- wj (/ (- (* wj (exp wj)) x) (+ (exp wj) (* wj (exp wj))))))
(FPCore (wj x)
 :precision binary64
 (if (<=
      (- wj (/ (- (* wj (exp wj)) x) (+ (exp wj) (* wj (exp wj)))))
      3.5043546686350665e-16)
   (+
    x
    (+
     (* (* wj wj) (- (+ (* x 2.5) 1.0) wj))
     (* x (- (* wj -2.0) (* 2.6666666666666665 (pow wj 3.0))))))
   (+ wj (/ (- (/ (/ x (sqrt (exp wj))) (sqrt (exp 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 tmp;
	if ((wj - (((wj * exp(wj)) - x) / (exp(wj) + (wj * exp(wj))))) <= 3.5043546686350665e-16) {
		tmp = x + (((wj * wj) * (((x * 2.5) + 1.0) - wj)) + (x * ((wj * -2.0) - (2.6666666666666665 * pow(wj, 3.0)))));
	} else {
		tmp = wj + ((((x / sqrt(exp(wj))) / sqrt(exp(wj))) - wj) / (wj + 1.0));
	}
	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.6
Target13.0
Herbie0.5
\[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))))) < 3.504354669e-16

    1. Initial program 18.1

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

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

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

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

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

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

    if 3.504354669e-16 < (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj)))))

    1. Initial program 2.5

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

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

      \[\leadsto wj + \frac{\frac{x}{\color{blue}{\sqrt{e^{wj}} \cdot \sqrt{e^{wj}}}} - wj}{wj + 1}\]
    5. Applied associate-/r*_binary640.4

      \[\leadsto wj + \frac{\color{blue}{\frac{\frac{x}{\sqrt{e^{wj}}}}{\sqrt{e^{wj}}}} - wj}{wj + 1}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.5

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

Alternatives

Reproduce

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