Average Error: 0.5 → 0.5
Time: 13.8s
Precision: 64
\[\log \left(1 + e^{x}\right) - x \cdot y\]
\[\mathsf{fma}\left(-x, y, \log \left(e^{x} + 1\right)\right)\]
\log \left(1 + e^{x}\right) - x \cdot y
\mathsf{fma}\left(-x, y, \log \left(e^{x} + 1\right)\right)
double f(double x, double y) {
        double r131612 = 1.0;
        double r131613 = x;
        double r131614 = exp(r131613);
        double r131615 = r131612 + r131614;
        double r131616 = log(r131615);
        double r131617 = y;
        double r131618 = r131613 * r131617;
        double r131619 = r131616 - r131618;
        return r131619;
}

double f(double x, double y) {
        double r131620 = x;
        double r131621 = -r131620;
        double r131622 = y;
        double r131623 = exp(r131620);
        double r131624 = 1.0;
        double r131625 = r131623 + r131624;
        double r131626 = log(r131625);
        double r131627 = fma(r131621, r131622, r131626);
        return r131627;
}

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. Using strategy rm
  3. Applied add-sqr-sqrt1.3

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

    \[\leadsto \color{blue}{\left(\log \left(\sqrt{1 + e^{x}}\right) + \log \left(\sqrt{1 + e^{x}}\right)\right)} - x \cdot y\]
  5. Taylor expanded around inf 1.0

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

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

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

Reproduce

herbie shell --seed 2019305 +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)))