Average Error: 14.2 → 0.5
Time: 18.8s
Precision: binary64
Cost: 33860
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
\[\begin{array}{l} t_0 := wj \cdot e^{wj}\\ \mathbf{if}\;wj + \frac{x - t_0}{e^{wj} + t_0} \leq 2 \cdot 10^{-17}:\\ \;\;\;\;\left(wj \cdot wj + \left(x + -2 \cdot \left(wj \cdot x\right)\right)\right) - {wj}^{3}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\frac{x}{e^{wj}} - wj}{1 - wj \cdot wj}, 1 - wj, wj\right)\\ \end{array} \]
(FPCore (wj x)
 :precision binary64
 (- wj (/ (- (* wj (exp wj)) x) (+ (exp wj) (* wj (exp wj))))))
(FPCore (wj x)
 :precision binary64
 (let* ((t_0 (* wj (exp wj))))
   (if (<= (+ wj (/ (- x t_0) (+ (exp wj) t_0))) 2e-17)
     (- (+ (* wj wj) (+ x (* -2.0 (* wj x)))) (pow wj 3.0))
     (fma (/ (- (/ x (exp wj)) wj) (- 1.0 (* wj wj))) (- 1.0 wj) 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 t_0 = wj * exp(wj);
	double tmp;
	if ((wj + ((x - t_0) / (exp(wj) + t_0))) <= 2e-17) {
		tmp = ((wj * wj) + (x + (-2.0 * (wj * x)))) - pow(wj, 3.0);
	} else {
		tmp = fma((((x / exp(wj)) - wj) / (1.0 - (wj * wj))), (1.0 - wj), wj);
	}
	return tmp;
}
function code(wj, x)
	return Float64(wj - Float64(Float64(Float64(wj * exp(wj)) - x) / Float64(exp(wj) + Float64(wj * exp(wj)))))
end
function code(wj, x)
	t_0 = Float64(wj * exp(wj))
	tmp = 0.0
	if (Float64(wj + Float64(Float64(x - t_0) / Float64(exp(wj) + t_0))) <= 2e-17)
		tmp = Float64(Float64(Float64(wj * wj) + Float64(x + Float64(-2.0 * Float64(wj * x)))) - (wj ^ 3.0));
	else
		tmp = fma(Float64(Float64(Float64(x / exp(wj)) - wj) / Float64(1.0 - Float64(wj * wj))), Float64(1.0 - wj), wj);
	end
	return tmp
end
code[wj_, x_] := N[(wj - N[(N[(N[(wj * N[Exp[wj], $MachinePrecision]), $MachinePrecision] - x), $MachinePrecision] / N[(N[Exp[wj], $MachinePrecision] + N[(wj * N[Exp[wj], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[wj_, x_] := Block[{t$95$0 = N[(wj * N[Exp[wj], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(wj + N[(N[(x - t$95$0), $MachinePrecision] / N[(N[Exp[wj], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2e-17], N[(N[(N[(wj * wj), $MachinePrecision] + N[(x + N[(-2.0 * N[(wj * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[Power[wj, 3.0], $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(x / N[Exp[wj], $MachinePrecision]), $MachinePrecision] - wj), $MachinePrecision] / N[(1.0 - N[(wj * wj), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(1.0 - wj), $MachinePrecision] + wj), $MachinePrecision]]]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\begin{array}{l}
t_0 := wj \cdot e^{wj}\\
\mathbf{if}\;wj + \frac{x - t_0}{e^{wj} + t_0} \leq 2 \cdot 10^{-17}:\\
\;\;\;\;\left(wj \cdot wj + \left(x + -2 \cdot \left(wj \cdot x\right)\right)\right) - {wj}^{3}\\

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


\end{array}

Error

Target

Original14.2
Target13.5
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))))) < 2.00000000000000014e-17

    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}} \]
      Proof

      [Start]18.6

      \[ wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]

      sub-neg [=>]18.6

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

      neg-mul-1 [=>]18.6

      \[ wj + \color{blue}{-1 \cdot \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}} \]

      *-commutative [=>]18.6

      \[ wj + \color{blue}{\frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \cdot -1} \]

      *-commutative [<=]18.6

      \[ wj + \color{blue}{-1 \cdot \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}} \]

      neg-mul-1 [<=]18.6

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

      neg-sub0 [=>]18.6

      \[ wj + \color{blue}{\left(0 - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\right)} \]

      div-sub [=>]18.6

      \[ wj + \left(0 - \color{blue}{\left(\frac{wj \cdot e^{wj}}{e^{wj} + wj \cdot e^{wj}} - \frac{x}{e^{wj} + wj \cdot e^{wj}}\right)}\right) \]

      associate--r- [=>]18.6

      \[ wj + \color{blue}{\left(\left(0 - \frac{wj \cdot e^{wj}}{e^{wj} + wj \cdot e^{wj}}\right) + \frac{x}{e^{wj} + wj \cdot e^{wj}}\right)} \]

      +-commutative [=>]18.6

      \[ wj + \color{blue}{\left(\frac{x}{e^{wj} + wj \cdot e^{wj}} + \left(0 - \frac{wj \cdot e^{wj}}{e^{wj} + wj \cdot e^{wj}}\right)\right)} \]

      sub0-neg [=>]18.6

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

      sub-neg [<=]18.6

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

      \[\leadsto \color{blue}{-1 \cdot \left(\left(0.6666666666666666 \cdot x + \left(-3 \cdot x + \left(1 + -2 \cdot \left(-4 \cdot x + 1.5 \cdot x\right)\right)\right)\right) \cdot {wj}^{3}\right) + \left(\left(1 - \left(-4 \cdot x + 1.5 \cdot x\right)\right) \cdot {wj}^{2} + \left(-2 \cdot \left(wj \cdot x\right) + x\right)\right)} \]
    4. Taylor expanded in x around 0 0.6

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

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

      [Start]0.6

      \[ -1 \cdot \left(\left(0.6666666666666666 \cdot x + \left(-3 \cdot x + \left(1 + -2 \cdot \left(-4 \cdot x + 1.5 \cdot x\right)\right)\right)\right) \cdot {wj}^{3}\right) + \left({wj}^{2} + \left(-2 \cdot \left(wj \cdot x\right) + x\right)\right) \]

      unpow2 [=>]0.6

      \[ -1 \cdot \left(\left(0.6666666666666666 \cdot x + \left(-3 \cdot x + \left(1 + -2 \cdot \left(-4 \cdot x + 1.5 \cdot x\right)\right)\right)\right) \cdot {wj}^{3}\right) + \left(\color{blue}{wj \cdot wj} + \left(-2 \cdot \left(wj \cdot x\right) + x\right)\right) \]
    6. Taylor expanded in x around 0 0.5

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

    if 2.00000000000000014e-17 < (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj)))))

    1. Initial program 2.9

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

      \[\leadsto \color{blue}{wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}} \]
      Proof

      [Start]2.9

      \[ wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]

      sub-neg [=>]2.9

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

      neg-mul-1 [=>]2.9

      \[ wj + \color{blue}{-1 \cdot \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}} \]

      *-commutative [=>]2.9

      \[ wj + \color{blue}{\frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \cdot -1} \]

      *-commutative [<=]2.9

      \[ wj + \color{blue}{-1 \cdot \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}} \]

      neg-mul-1 [<=]2.9

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

      neg-sub0 [=>]2.9

      \[ wj + \color{blue}{\left(0 - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\right)} \]

      div-sub [=>]2.9

      \[ wj + \left(0 - \color{blue}{\left(\frac{wj \cdot e^{wj}}{e^{wj} + wj \cdot e^{wj}} - \frac{x}{e^{wj} + wj \cdot e^{wj}}\right)}\right) \]

      associate--r- [=>]2.9

      \[ wj + \color{blue}{\left(\left(0 - \frac{wj \cdot e^{wj}}{e^{wj} + wj \cdot e^{wj}}\right) + \frac{x}{e^{wj} + wj \cdot e^{wj}}\right)} \]

      +-commutative [=>]2.9

      \[ wj + \color{blue}{\left(\frac{x}{e^{wj} + wj \cdot e^{wj}} + \left(0 - \frac{wj \cdot e^{wj}}{e^{wj} + wj \cdot e^{wj}}\right)\right)} \]

      sub0-neg [=>]2.9

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

      sub-neg [<=]2.9

      \[ wj + \color{blue}{\left(\frac{x}{e^{wj} + wj \cdot e^{wj}} - \frac{wj \cdot e^{wj}}{e^{wj} + wj \cdot e^{wj}}\right)} \]
    3. Applied egg-rr0.6

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

      \[\leadsto wj + \color{blue}{\frac{\frac{x}{e^{wj}} - wj}{\frac{1 - wj \cdot wj}{1 - wj}}} \]
      Proof

      [Start]0.6

      \[ wj + \frac{\frac{x}{e^{wj}} - wj}{-\mathsf{fma}\left(wj, wj, -1\right)} \cdot \left(-\left(wj + -1\right)\right) \]

      distribute-neg-in [=>]0.6

      \[ wj + \frac{\frac{x}{e^{wj}} - wj}{-\mathsf{fma}\left(wj, wj, -1\right)} \cdot \color{blue}{\left(\left(-wj\right) + \left(--1\right)\right)} \]

      metadata-eval [=>]0.6

      \[ wj + \frac{\frac{x}{e^{wj}} - wj}{-\mathsf{fma}\left(wj, wj, -1\right)} \cdot \left(\left(-wj\right) + \color{blue}{1}\right) \]

      +-commutative [<=]0.6

      \[ wj + \frac{\frac{x}{e^{wj}} - wj}{-\mathsf{fma}\left(wj, wj, -1\right)} \cdot \color{blue}{\left(1 + \left(-wj\right)\right)} \]

      sub-neg [<=]0.6

      \[ wj + \frac{\frac{x}{e^{wj}} - wj}{-\mathsf{fma}\left(wj, wj, -1\right)} \cdot \color{blue}{\left(1 - wj\right)} \]

      associate-*l/ [=>]0.6

      \[ wj + \color{blue}{\frac{\left(\frac{x}{e^{wj}} - wj\right) \cdot \left(1 - wj\right)}{-\mathsf{fma}\left(wj, wj, -1\right)}} \]

      fma-udef [=>]0.6

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

      distribute-neg-in [=>]0.6

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

      metadata-eval [=>]0.6

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

      +-commutative [<=]0.6

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

      sub-neg [<=]0.6

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

      associate-/l* [=>]0.6

      \[ wj + \color{blue}{\frac{\frac{x}{e^{wj}} - wj}{\frac{1 - wj \cdot wj}{1 - wj}}} \]
    5. Applied egg-rr0.6

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

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

Alternatives

Alternative 1
Error1.9
Cost8576
\[\left(wj \cdot wj + \left(x + -2 \cdot \left(wj \cdot x\right)\right)\right) + {wj}^{3} \cdot \left(x \cdot -0.6666666666666666 + \left(x \cdot 3 - \left(1 + -2 \cdot \left(x \cdot 1.5 - x \cdot 4\right)\right)\right)\right) \]
Alternative 2
Error1.8
Cost7296
\[\left(wj \cdot wj + \left(x + -2 \cdot \left(wj \cdot x\right)\right)\right) - {wj}^{3} \]
Alternative 3
Error2.1
Cost7040
\[\left(x + -2 \cdot \left(wj \cdot x\right)\right) + {wj}^{2} \]
Alternative 4
Error8.4
Cost1737
\[\begin{array}{l} \mathbf{if}\;wj \leq -3.9 \cdot 10^{-28} \lor \neg \left(wj \leq -3.2 \cdot 10^{-46}\right):\\ \;\;\;\;\frac{x - wj \cdot \left(x + \left(wj \cdot x\right) \cdot -0.5\right)}{wj + 1} + \left(wj - \frac{wj}{wj + 1}\right)\\ \mathbf{else}:\\ \;\;\;\;wj \cdot wj\\ \end{array} \]
Alternative 5
Error8.6
Cost1476
\[\begin{array}{l} \mathbf{if}\;wj \leq -2.7 \cdot 10^{-28}:\\ \;\;\;\;wj + \frac{\left(\left(wj \cdot wj\right) \cdot \left(x \cdot 0.5\right) - \left(wj \cdot x - x\right)\right) - wj}{wj + 1}\\ \mathbf{elif}\;wj \leq -1 \cdot 10^{-46}:\\ \;\;\;\;wj \cdot wj\\ \mathbf{elif}\;wj \leq 7.8 \cdot 10^{-19}:\\ \;\;\;\;x \cdot \left(1 + wj \cdot -2\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(wj \cdot wj\right) \cdot \left(-2 - wj\right)}{\left(wj + 1\right) \cdot \left(-2 - wj\right)}\\ \end{array} \]
Alternative 6
Error8.7
Cost1356
\[\begin{array}{l} \mathbf{if}\;wj \leq -1.65 \cdot 10^{-29}:\\ \;\;\;\;wj + \frac{\left(x - wj \cdot x\right) - wj}{wj + 1}\\ \mathbf{elif}\;wj \leq -3.2 \cdot 10^{-46}:\\ \;\;\;\;wj \cdot wj\\ \mathbf{elif}\;wj \leq 8.5 \cdot 10^{-19}:\\ \;\;\;\;x \cdot \left(1 + wj \cdot -2\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(wj \cdot wj\right) \cdot \left(-2 - wj\right)}{\left(wj + 1\right) \cdot \left(-2 - wj\right)}\\ \end{array} \]
Alternative 7
Error8.6
Cost1228
\[\begin{array}{l} t_0 := wj + \frac{\left(x - wj \cdot x\right) - wj}{wj + 1}\\ \mathbf{if}\;wj \leq -1.12 \cdot 10^{-29}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;wj \leq -3.2 \cdot 10^{-46}:\\ \;\;\;\;wj \cdot wj\\ \mathbf{elif}\;wj \leq 6.2 \cdot 10^{-51}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error9.1
Cost844
\[\begin{array}{l} \mathbf{if}\;wj \leq -7.2 \cdot 10^{-27}:\\ \;\;\;\;x \cdot \left(1 - wj \cdot 2\right)\\ \mathbf{elif}\;wj \leq -1.2 \cdot 10^{-49}:\\ \;\;\;\;wj \cdot wj\\ \mathbf{elif}\;wj \leq 9.2 \cdot 10^{-10}:\\ \;\;\;\;x + -2 \cdot \left(wj \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;wj - \frac{wj}{wj + 1}\\ \end{array} \]
Alternative 9
Error9.9
Cost713
\[\begin{array}{l} \mathbf{if}\;wj \leq -5.4 \cdot 10^{-29} \lor \neg \left(wj \leq -3.2 \cdot 10^{-46}\right):\\ \;\;\;\;x \cdot \left(1 - wj \cdot 2\right)\\ \mathbf{else}:\\ \;\;\;\;wj \cdot wj\\ \end{array} \]
Alternative 10
Error10.0
Cost712
\[\begin{array}{l} \mathbf{if}\;wj \leq -3.3 \cdot 10^{-28}:\\ \;\;\;\;x \cdot \left(1 - wj \cdot 2\right)\\ \mathbf{elif}\;wj \leq -6.2 \cdot 10^{-48}:\\ \;\;\;\;wj \cdot wj\\ \mathbf{else}:\\ \;\;\;\;x + -2 \cdot \left(wj \cdot x\right)\\ \end{array} \]
Alternative 11
Error61.2
Cost64
\[wj \]
Alternative 12
Error10.0
Cost64
\[x \]

Error

Reproduce

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