Average Error: 13.5 → 0.3
Time: 16.1s
Precision: binary64
Cost: 13764
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
\[\begin{array}{l} \mathbf{if}\;wj \leq -7.5 \cdot 10^{-12}:\\ \;\;\;\;wj + \frac{x - wj \cdot e^{wj}}{e^{wj} \cdot \left(wj + 1\right)}\\ \mathbf{elif}\;wj \leq -1 \cdot 10^{-189}:\\ \;\;\;\;\mathsf{fma}\left(wj, wj, x\right)\\ \mathbf{elif}\;wj \leq 4.2 \cdot 10^{-7}:\\ \;\;\;\;{wj}^{3} \cdot \left(x \cdot -0.6666666666666666 + \left(x \cdot 3 + \left(-1 + \left(x \cdot 1.5 - x \cdot 4\right) \cdot 2\right)\right)\right) + \left(wj \cdot wj + \left(x + -2 \cdot \left(wj \cdot x\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;wj + \frac{\frac{x}{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 -7.5e-12)
   (+ wj (/ (- x (* wj (exp wj))) (* (exp wj) (+ wj 1.0))))
   (if (<= wj -1e-189)
     (fma wj wj x)
     (if (<= wj 4.2e-7)
       (+
        (*
         (pow wj 3.0)
         (+
          (* x -0.6666666666666666)
          (+ (* x 3.0) (+ -1.0 (* (- (* x 1.5) (* x 4.0)) 2.0)))))
        (+ (* wj wj) (+ x (* -2.0 (* wj x)))))
       (+ wj (/ (- (/ x (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 <= -7.5e-12) {
		tmp = wj + ((x - (wj * exp(wj))) / (exp(wj) * (wj + 1.0)));
	} else if (wj <= -1e-189) {
		tmp = fma(wj, wj, x);
	} else if (wj <= 4.2e-7) {
		tmp = (pow(wj, 3.0) * ((x * -0.6666666666666666) + ((x * 3.0) + (-1.0 + (((x * 1.5) - (x * 4.0)) * 2.0))))) + ((wj * wj) + (x + (-2.0 * (wj * x))));
	} else {
		tmp = wj + (((x / exp(wj)) - wj) / (wj + 1.0));
	}
	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)
	tmp = 0.0
	if (wj <= -7.5e-12)
		tmp = Float64(wj + Float64(Float64(x - Float64(wj * exp(wj))) / Float64(exp(wj) * Float64(wj + 1.0))));
	elseif (wj <= -1e-189)
		tmp = fma(wj, wj, x);
	elseif (wj <= 4.2e-7)
		tmp = Float64(Float64((wj ^ 3.0) * Float64(Float64(x * -0.6666666666666666) + Float64(Float64(x * 3.0) + Float64(-1.0 + Float64(Float64(Float64(x * 1.5) - Float64(x * 4.0)) * 2.0))))) + Float64(Float64(wj * wj) + Float64(x + Float64(-2.0 * Float64(wj * x)))));
	else
		tmp = Float64(wj + Float64(Float64(Float64(x / exp(wj)) - wj) / Float64(wj + 1.0)));
	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_] := If[LessEqual[wj, -7.5e-12], N[(wj + N[(N[(x - N[(wj * N[Exp[wj], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[Exp[wj], $MachinePrecision] * N[(wj + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[wj, -1e-189], N[(wj * wj + x), $MachinePrecision], If[LessEqual[wj, 4.2e-7], N[(N[(N[Power[wj, 3.0], $MachinePrecision] * N[(N[(x * -0.6666666666666666), $MachinePrecision] + N[(N[(x * 3.0), $MachinePrecision] + N[(-1.0 + N[(N[(N[(x * 1.5), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(wj * wj), $MachinePrecision] + N[(x + N[(-2.0 * N[(wj * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(wj + N[(N[(N[(x / N[Exp[wj], $MachinePrecision]), $MachinePrecision] - wj), $MachinePrecision] / N[(wj + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\begin{array}{l}
\mathbf{if}\;wj \leq -7.5 \cdot 10^{-12}:\\
\;\;\;\;wj + \frac{x - wj \cdot e^{wj}}{e^{wj} \cdot \left(wj + 1\right)}\\

\mathbf{elif}\;wj \leq -1 \cdot 10^{-189}:\\
\;\;\;\;\mathsf{fma}\left(wj, wj, x\right)\\

\mathbf{elif}\;wj \leq 4.2 \cdot 10^{-7}:\\
\;\;\;\;{wj}^{3} \cdot \left(x \cdot -0.6666666666666666 + \left(x \cdot 3 + \left(-1 + \left(x \cdot 1.5 - x \cdot 4\right) \cdot 2\right)\right)\right) + \left(wj \cdot wj + \left(x + -2 \cdot \left(wj \cdot x\right)\right)\right)\\

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


\end{array}

Error

Target

Original13.5
Target12.9
Herbie0.3
\[wj - \left(\frac{wj}{wj + 1} - \frac{x}{e^{wj} + wj \cdot e^{wj}}\right) \]

Derivation

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

    1. Initial program 7.8

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

      \[\leadsto \color{blue}{wj - \frac{wj \cdot e^{wj} - x}{\left(wj + 1\right) \cdot e^{wj}}} \]
      Proof
      (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (*.f64 (+.f64 wj 1) (exp.f64 wj)))): 0 points increase in error, 0 points decrease in error
      (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (Rewrite<= distribute-rgt1-in_binary64 (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj)))))): 0 points increase in error, 0 points decrease in error

    if -7.5e-12 < wj < -1.00000000000000007e-189

    1. Initial program 19.0

      \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}} \]
    2. Simplified19.0

      \[\leadsto \color{blue}{wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}} \]
      Proof
      (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (*.f64 (+.f64 wj 1) (exp.f64 wj)))): 0 points increase in error, 0 points decrease in error
      (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (Rewrite<= distribute-rgt1-in_binary64 (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj)))))): 0 points increase in error, 0 points decrease in error
    3. Taylor expanded in wj around 0 0.1

      \[\leadsto \color{blue}{\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)} \]
    4. Taylor expanded in x around 0 0.1

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

      \[\leadsto \color{blue}{wj \cdot wj} + \left(-2 \cdot \left(wj \cdot x\right) + x\right) \]
      Proof
      (+.f64 (*.f64 wj wj) (+.f64 (*.f64 -2 (*.f64 wj x)) x)): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= unpow2_binary64 (pow.f64 wj 2)) (+.f64 (*.f64 -2 (*.f64 wj x)) x)): 0 points increase in error, 0 points decrease in error
    6. Taylor expanded in wj around 0 0.1

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(wj, wj, \mathsf{fma}\left(-2, x \cdot wj, x\right)\right)} \]
      Proof
      (fma.f64 wj wj (fma.f64 -2 (*.f64 x wj) x)): 0 points increase in error, 0 points decrease in error
      (fma.f64 wj wj (fma.f64 -2 (Rewrite<= *-commutative_binary64 (*.f64 wj x)) x)): 0 points increase in error, 0 points decrease in error
      (Rewrite=> fma-udef_binary64 (+.f64 (*.f64 wj wj) (fma.f64 -2 (*.f64 wj x) x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= unpow2_binary64 (pow.f64 wj 2)) (fma.f64 -2 (*.f64 wj x) x)): 0 points increase in error, 0 points decrease in error
      (+.f64 (pow.f64 wj 2) (Rewrite<= fma-def_binary64 (+.f64 (*.f64 -2 (*.f64 wj x)) x))): 0 points increase in error, 0 points decrease in error
    8. Taylor expanded in wj around 0 0.2

      \[\leadsto \mathsf{fma}\left(wj, wj, \color{blue}{x}\right) \]

    if -1.00000000000000007e-189 < wj < 4.2e-7

    1. Initial program 10.9

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

      \[\leadsto \color{blue}{wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}} \]
      Proof
      (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (*.f64 (+.f64 wj 1) (exp.f64 wj)))): 0 points increase in error, 0 points decrease in error
      (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (Rewrite<= distribute-rgt1-in_binary64 (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj)))))): 0 points increase in error, 0 points decrease in error
    3. Taylor expanded in wj around 0 0.1

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

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

      \[\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
      (+.f64 (*.f64 wj wj) (+.f64 (*.f64 -2 (*.f64 wj x)) x)): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= unpow2_binary64 (pow.f64 wj 2)) (+.f64 (*.f64 -2 (*.f64 wj x)) x)): 0 points increase in error, 0 points decrease in error

    if 4.2e-7 < wj

    1. Initial program 27.0

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

      \[\leadsto \color{blue}{wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}} \]
      Proof
      (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (*.f64 (+.f64 wj 1) (exp.f64 wj)))): 0 points increase in error, 0 points decrease in error
      (-.f64 wj (/.f64 (-.f64 (*.f64 wj (exp.f64 wj)) x) (Rewrite<= distribute-rgt1-in_binary64 (+.f64 (exp.f64 wj) (*.f64 wj (exp.f64 wj)))))): 0 points increase in error, 0 points decrease in error
  3. Recombined 4 regimes into one program.
  4. Final simplification0.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;wj \leq -7.5 \cdot 10^{-12}:\\ \;\;\;\;wj + \frac{x - wj \cdot e^{wj}}{e^{wj} \cdot \left(wj + 1\right)}\\ \mathbf{elif}\;wj \leq -1 \cdot 10^{-189}:\\ \;\;\;\;\mathsf{fma}\left(wj, wj, x\right)\\ \mathbf{elif}\;wj \leq 4.2 \cdot 10^{-7}:\\ \;\;\;\;{wj}^{3} \cdot \left(x \cdot -0.6666666666666666 + \left(x \cdot 3 + \left(-1 + \left(x \cdot 1.5 - x \cdot 4\right) \cdot 2\right)\right)\right) + \left(wj \cdot wj + \left(x + -2 \cdot \left(wj \cdot x\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}\\ \end{array} \]

Alternatives

Alternative 1
Error0.3
Cost13513
\[\begin{array}{l} \mathbf{if}\;wj \leq -6.5 \cdot 10^{-9} \lor \neg \left(wj \leq 9.5 \cdot 10^{-9}\right):\\ \;\;\;\;wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(wj, wj, \mathsf{fma}\left(-2, wj \cdot x, x\right)\right)\\ \end{array} \]
Alternative 2
Error0.3
Cost7369
\[\begin{array}{l} \mathbf{if}\;wj \leq -5.2 \cdot 10^{-9} \lor \neg \left(wj \leq 9.5 \cdot 10^{-9}\right):\\ \;\;\;\;wj + \frac{\frac{x}{e^{wj}} - wj}{wj + 1}\\ \mathbf{else}:\\ \;\;\;\;wj \cdot wj + \left(x + -2 \cdot \left(wj \cdot x\right)\right)\\ \end{array} \]
Alternative 3
Error10.7
Cost713
\[\begin{array}{l} \mathbf{if}\;x \leq -1.3 \cdot 10^{-170} \lor \neg \left(x \leq -3.8 \cdot 10^{-299}\right):\\ \;\;\;\;x + -2 \cdot \left(wj \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;wj \cdot wj\\ \end{array} \]
Alternative 4
Error10.6
Cost712
\[\begin{array}{l} \mathbf{if}\;x \leq -1.46 \cdot 10^{-170}:\\ \;\;\;\;x + -2 \cdot \left(wj \cdot x\right)\\ \mathbf{elif}\;x \leq -2.25 \cdot 10^{-300}:\\ \;\;\;\;wj \cdot wj\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{1 - wj \cdot -2}\\ \end{array} \]
Alternative 5
Error1.9
Cost704
\[wj \cdot wj + \left(x + -2 \cdot \left(wj \cdot x\right)\right) \]
Alternative 6
Error11.0
Cost456
\[\begin{array}{l} \mathbf{if}\;x \leq -1.3 \cdot 10^{-170}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq -6.2 \cdot 10^{-301}:\\ \;\;\;\;wj \cdot wj\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 7
Error61.2
Cost64
\[wj \]
Alternative 8
Error9.3
Cost64
\[x \]

Error

Reproduce

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