Average Error: 0.5 → 1.0
Time: 13.7s
Precision: 64
\[\log \left(1 + e^{x}\right) - x \cdot y\]
\[\mathsf{fma}\left(-y, x, \log \left(\sqrt{1 + e^{x}}\right) + \log \left(\sqrt{1 + e^{x}}\right)\right)\]
\log \left(1 + e^{x}\right) - x \cdot y
\mathsf{fma}\left(-y, x, \log \left(\sqrt{1 + e^{x}}\right) + \log \left(\sqrt{1 + e^{x}}\right)\right)
double f(double x, double y) {
        double r164235 = 1.0;
        double r164236 = x;
        double r164237 = exp(r164236);
        double r164238 = r164235 + r164237;
        double r164239 = log(r164238);
        double r164240 = y;
        double r164241 = r164236 * r164240;
        double r164242 = r164239 - r164241;
        return r164242;
}

double f(double x, double y) {
        double r164243 = y;
        double r164244 = -r164243;
        double r164245 = x;
        double r164246 = 1.0;
        double r164247 = exp(r164245);
        double r164248 = r164246 + r164247;
        double r164249 = sqrt(r164248);
        double r164250 = log(r164249);
        double r164251 = r164250 + r164250;
        double r164252 = fma(r164244, r164245, r164251);
        return r164252;
}

Error

Bits error versus x

Bits error versus y

Target

Original0.5
Target0.0
Herbie1.0
\[\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. Simplified0.5

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

    \[\leadsto \mathsf{fma}\left(-y, x, \log \color{blue}{\left(\sqrt{1 + e^{x}} \cdot \sqrt{1 + e^{x}}\right)}\right)\]
  5. Applied log-prod1.0

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

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

Reproduce

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

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

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