Average Error: 0.5 → 0.5
Time: 17.2s
Precision: 64
\[\log \left(1 + e^{x}\right) - x \cdot y\]
\[\mathsf{fma}\left(-x, y, \log \left(1 + e^{x}\right)\right)\]
\log \left(1 + e^{x}\right) - x \cdot y
\mathsf{fma}\left(-x, y, \log \left(1 + e^{x}\right)\right)
double f(double x, double y) {
        double r89931 = 1.0;
        double r89932 = x;
        double r89933 = exp(r89932);
        double r89934 = r89931 + r89933;
        double r89935 = log(r89934);
        double r89936 = y;
        double r89937 = r89932 * r89936;
        double r89938 = r89935 - r89937;
        return r89938;
}

double f(double x, double y) {
        double r89939 = x;
        double r89940 = -r89939;
        double r89941 = y;
        double r89942 = 1.0;
        double r89943 = exp(r89939);
        double r89944 = r89942 + r89943;
        double r89945 = log(r89944);
        double r89946 = fma(r89940, r89941, r89945);
        return r89946;
}

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 *-un-lft-identity0.5

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

    \[\leadsto \color{blue}{\left(\log 1 + \log \left(1 + e^{x}\right)\right)} - x \cdot y\]
  5. Applied associate--l+0.5

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

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

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

Reproduce

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