Average Error: 0.5 → 1.0
Time: 24.2s
Precision: 64
\[\log \left(1 + e^{x}\right) - x \cdot y\]
\[\mathsf{fma}\left(\sqrt{\mathsf{log1p}\left(e^{x}\right)}, \sqrt{\mathsf{log1p}\left(e^{x}\right)}, x \cdot \left(-y\right)\right)\]
\log \left(1 + e^{x}\right) - x \cdot y
\mathsf{fma}\left(\sqrt{\mathsf{log1p}\left(e^{x}\right)}, \sqrt{\mathsf{log1p}\left(e^{x}\right)}, x \cdot \left(-y\right)\right)
double f(double x, double y) {
        double r5721452 = 1.0;
        double r5721453 = x;
        double r5721454 = exp(r5721453);
        double r5721455 = r5721452 + r5721454;
        double r5721456 = log(r5721455);
        double r5721457 = y;
        double r5721458 = r5721453 * r5721457;
        double r5721459 = r5721456 - r5721458;
        return r5721459;
}

double f(double x, double y) {
        double r5721460 = x;
        double r5721461 = exp(r5721460);
        double r5721462 = log1p(r5721461);
        double r5721463 = sqrt(r5721462);
        double r5721464 = y;
        double r5721465 = -r5721464;
        double r5721466 = r5721460 * r5721465;
        double r5721467 = fma(r5721463, r5721463, r5721466);
        return r5721467;
}

Error

Bits error versus x

Bits error versus y

Target

Original0.5
Target0.1
Herbie1.0
\[\begin{array}{l} \mathbf{if}\;x \le 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{log1p}\left(e^{x}\right) - y \cdot x}\]
  3. Using strategy rm
  4. Applied add-sqr-sqrt1.0

    \[\leadsto \color{blue}{\sqrt{\mathsf{log1p}\left(e^{x}\right)} \cdot \sqrt{\mathsf{log1p}\left(e^{x}\right)}} - y \cdot x\]
  5. Applied fma-neg1.0

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

    \[\leadsto \mathsf{fma}\left(\sqrt{\mathsf{log1p}\left(e^{x}\right)}, \sqrt{\mathsf{log1p}\left(e^{x}\right)}, x \cdot \left(-y\right)\right)\]

Reproduce

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

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

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