Average Error: 0.5 → 0.5
Time: 19.5s
Precision: 64
\[\log \left(1 + e^{x}\right) - x \cdot y\]
\[\mathsf{fma}\left(x, -y, e^{\log \left(\log \left(1 + e^{x}\right)\right)}\right)\]
\log \left(1 + e^{x}\right) - x \cdot y
\mathsf{fma}\left(x, -y, e^{\log \left(\log \left(1 + e^{x}\right)\right)}\right)
double f(double x, double y) {
        double r141553 = 1.0;
        double r141554 = x;
        double r141555 = exp(r141554);
        double r141556 = r141553 + r141555;
        double r141557 = log(r141556);
        double r141558 = y;
        double r141559 = r141554 * r141558;
        double r141560 = r141557 - r141559;
        return r141560;
}

double f(double x, double y) {
        double r141561 = x;
        double r141562 = y;
        double r141563 = -r141562;
        double r141564 = 1.0;
        double r141565 = exp(r141561);
        double r141566 = r141564 + r141565;
        double r141567 = log(r141566);
        double r141568 = log(r141567);
        double r141569 = exp(r141568);
        double r141570 = fma(r141561, r141563, r141569);
        return r141570;
}

Error

Bits error versus x

Bits error versus y

Target

Original0.5
Target0.1
Herbie0.5
\[\begin{array}{l} \mathbf{if}\;x \le 0.0:\\ \;\;\;\;\log \left(1 + e^{x}\right) - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + e^{-x}\right) - \left(-x\right) \cdot \left(1 - y\right)\\ \end{array}\]

Derivation

  1. Initial program 0.5

    \[\log \left(1 + e^{x}\right) - x \cdot y\]
  2. Taylor expanded around inf 0.5

    \[\leadsto \color{blue}{\log \left(e^{x} + 1\right) - x \cdot y}\]
  3. Simplified0.5

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, -y, \log \left(1 + e^{x}\right)\right)}\]
  4. Using strategy rm
  5. Applied add-exp-log0.5

    \[\leadsto \mathsf{fma}\left(x, -y, \color{blue}{e^{\log \left(\log \left(1 + e^{x}\right)\right)}}\right)\]
  6. Final simplification0.5

    \[\leadsto \mathsf{fma}\left(x, -y, e^{\log \left(\log \left(1 + e^{x}\right)\right)}\right)\]

Reproduce

herbie shell --seed 2019304 +o rules:numerics
(FPCore (x y)
  :name "Logistic regression 2"
  :precision binary64

  :herbie-target
  (if (<= x 0.0) (- (log (+ 1 (exp x))) (* x y)) (- (log (+ 1 (exp (- x)))) (* (- x) (- 1 y))))

  (- (log (+ 1 (exp x))) (* x y)))