Average Error: 13.6 → 2.2
Time: 24.5s
Precision: 64
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
\[\left(wj \cdot x\right) \cdot -2 + \mathsf{fma}\left(wj, wj, x\right)\]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\left(wj \cdot x\right) \cdot -2 + \mathsf{fma}\left(wj, wj, x\right)
double f(double wj, double x) {
        double r9090944 = wj;
        double r9090945 = exp(r9090944);
        double r9090946 = r9090944 * r9090945;
        double r9090947 = x;
        double r9090948 = r9090946 - r9090947;
        double r9090949 = r9090945 + r9090946;
        double r9090950 = r9090948 / r9090949;
        double r9090951 = r9090944 - r9090950;
        return r9090951;
}

double f(double wj, double x) {
        double r9090952 = wj;
        double r9090953 = x;
        double r9090954 = r9090952 * r9090953;
        double r9090955 = -2.0;
        double r9090956 = r9090954 * r9090955;
        double r9090957 = fma(r9090952, r9090952, r9090953);
        double r9090958 = r9090956 + r9090957;
        return r9090958;
}

Error

Bits error versus wj

Bits error versus x

Target

Original13.6
Target12.9
Herbie2.2
\[wj - \left(\frac{wj}{wj + 1} - \frac{x}{e^{wj} + wj \cdot e^{wj}}\right)\]

Derivation

  1. Initial program 13.6

    \[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
  2. Taylor expanded around 0 2.2

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(wj, wj, x\right) + \left(x \cdot wj\right) \cdot -2}\]
  4. Final simplification2.2

    \[\leadsto \left(wj \cdot x\right) \cdot -2 + \mathsf{fma}\left(wj, wj, x\right)\]

Reproduce

herbie shell --seed 2019130 +o rules:numerics
(FPCore (wj x)
  :name "Jmat.Real.lambertw, newton loop step"

  :herbie-target
  (- wj (- (/ wj (+ wj 1)) (/ x (+ (exp wj) (* wj (exp wj))))))

  (- wj (/ (- (* wj (exp wj)) x) (+ (exp wj) (* wj (exp wj))))))