Average Error: 13.4 → 2.3
Time: 20.5s
Precision: 64
\[wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}\]
\[\mathsf{fma}\left(x \cdot wj, -2, \mathsf{fma}\left(wj, wj, x\right)\right)\]
wj - \frac{wj \cdot e^{wj} - x}{e^{wj} + wj \cdot e^{wj}}
\mathsf{fma}\left(x \cdot wj, -2, \mathsf{fma}\left(wj, wj, x\right)\right)
double f(double wj, double x) {
        double r14554491 = wj;
        double r14554492 = exp(r14554491);
        double r14554493 = r14554491 * r14554492;
        double r14554494 = x;
        double r14554495 = r14554493 - r14554494;
        double r14554496 = r14554492 + r14554493;
        double r14554497 = r14554495 / r14554496;
        double r14554498 = r14554491 - r14554497;
        return r14554498;
}

double f(double wj, double x) {
        double r14554499 = x;
        double r14554500 = wj;
        double r14554501 = r14554499 * r14554500;
        double r14554502 = -2.0;
        double r14554503 = fma(r14554500, r14554500, r14554499);
        double r14554504 = fma(r14554501, r14554502, r14554503);
        return r14554504;
}

Error

Bits error versus wj

Bits error versus x

Target

Original13.4
Target12.8
Herbie2.3
\[wj - \left(\frac{wj}{wj + 1} - \frac{x}{e^{wj} + wj \cdot e^{wj}}\right)\]

Derivation

  1. Initial program 13.4

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

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

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

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

Reproduce

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

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

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