Average Error: 14.1 → 0.2
Time: 5.1s
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 9.406397361605653 \cdot 10^{-16} \end{array}:\\ \;\;\;\;x \cdot \frac{e^{-wj}}{wj + 1} + \left(\left({wj}^{4} + {wj}^{2}\right) - {wj}^{3}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(wj + \frac{x}{e^{wj} \cdot \left(wj + 1\right)}\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}\;\begin{array}{l}
t_0 := wj \cdot e^{wj}\\
wj - \frac{t_0 - x}{e^{wj} + t_0} \leq 9.406397361605653 \cdot 10^{-16}
\end{array}:\\
\;\;\;\;x \cdot \frac{e^{-wj}}{wj + 1} + \left(\left({wj}^{4} + {wj}^{2}\right) - {wj}^{3}\right)\\

\mathbf{else}:\\
\;\;\;\;\left(wj + \frac{x}{e^{wj} \cdot \left(wj + 1\right)}\right) - \frac{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 (let* ((t_0 (* wj (exp wj))))
       (<= (- wj (/ (- t_0 x) (+ (exp wj) t_0))) 9.406397361605653e-16))
   (+
    (* x (/ (exp (- wj)) (+ wj 1.0)))
    (- (+ (pow wj 4.0) (pow wj 2.0)) (pow wj 3.0)))
   (- (+ wj (/ x (* (exp wj) (+ wj 1.0)))) (/ 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))) <= 9.406397361605653e-16) {
		tmp = (x * (exp(-wj) / (wj + 1.0))) + ((pow(wj, 4.0) + pow(wj, 2.0)) - pow(wj, 3.0));
	} else {
		tmp = (wj + (x / (exp(wj) * (wj + 1.0)))) - (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

Original14.1
Target13.4
Herbie0.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 (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj))))) < 9.406397362e-16

    1. Initial program 18.6

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

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

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

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

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

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

      \[\leadsto 1 \cdot \left(\frac{x}{e^{wj} \cdot \left(wj + 1\right)} + \color{blue}{\left(\left({wj}^{4} + {wj}^{2}\right) - {wj}^{3}\right)}\right) \]
    8. Applied div-inv_binary640.2

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

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

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

    1. Initial program 2.6

      \[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. Taylor expanded in x around 0 0.3

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

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

Reproduce

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