Average Error: 0.5 → 0.3
Time: 4.6s
Precision: 64
\[\log \left(1 + e^{x}\right) - x \cdot y\]
\[\begin{array}{l} \mathbf{if}\;x \le -4.14987374688656041 \cdot 10^{-4}:\\ \;\;\;\;\left(\left(\log \left(e^{x} + 1\right) + 1\right) - 1\right) - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\left(\log 2 + \left(\mathsf{fma}\left(0.25, {x}^{2}, 0.5 \cdot x\right) - \frac{1}{2} \cdot \frac{{x}^{2}}{{2}^{2}}\right)\right) - x \cdot y\\ \end{array}\]
\log \left(1 + e^{x}\right) - x \cdot y
\begin{array}{l}
\mathbf{if}\;x \le -4.14987374688656041 \cdot 10^{-4}:\\
\;\;\;\;\left(\left(\log \left(e^{x} + 1\right) + 1\right) - 1\right) - x \cdot y\\

\mathbf{else}:\\
\;\;\;\;\left(\log 2 + \left(\mathsf{fma}\left(0.25, {x}^{2}, 0.5 \cdot x\right) - \frac{1}{2} \cdot \frac{{x}^{2}}{{2}^{2}}\right)\right) - x \cdot y\\

\end{array}
double f(double x, double y) {
        double r141735 = 1.0;
        double r141736 = x;
        double r141737 = exp(r141736);
        double r141738 = r141735 + r141737;
        double r141739 = log(r141738);
        double r141740 = y;
        double r141741 = r141736 * r141740;
        double r141742 = r141739 - r141741;
        return r141742;
}

double f(double x, double y) {
        double r141743 = x;
        double r141744 = -0.00041498737468865604;
        bool r141745 = r141743 <= r141744;
        double r141746 = exp(r141743);
        double r141747 = 1.0;
        double r141748 = r141746 + r141747;
        double r141749 = log(r141748);
        double r141750 = 1.0;
        double r141751 = r141749 + r141750;
        double r141752 = r141751 - r141750;
        double r141753 = y;
        double r141754 = r141743 * r141753;
        double r141755 = r141752 - r141754;
        double r141756 = 2.0;
        double r141757 = log(r141756);
        double r141758 = 0.25;
        double r141759 = 2.0;
        double r141760 = pow(r141743, r141759);
        double r141761 = 0.5;
        double r141762 = r141761 * r141743;
        double r141763 = fma(r141758, r141760, r141762);
        double r141764 = 0.5;
        double r141765 = pow(r141756, r141759);
        double r141766 = r141760 / r141765;
        double r141767 = r141764 * r141766;
        double r141768 = r141763 - r141767;
        double r141769 = r141757 + r141768;
        double r141770 = r141769 - r141754;
        double r141771 = r141745 ? r141755 : r141770;
        return r141771;
}

Error

Bits error versus x

Bits error versus y

Target

Original0.5
Target0.0
Herbie0.3
\[\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. Split input into 2 regimes
  2. if x < -0.00041498737468865604

    1. Initial program 0.1

      \[\log \left(1 + e^{x}\right) - x \cdot y\]
    2. Using strategy rm
    3. Applied expm1-log1p-u0.1

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\log \left(1 + e^{x}\right)\right)\right)} - x \cdot y\]
    4. Using strategy rm
    5. Applied expm1-udef0.1

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

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

    if -0.00041498737468865604 < x

    1. Initial program 0.6

      \[\log \left(1 + e^{x}\right) - x \cdot y\]
    2. Using strategy rm
    3. Applied expm1-log1p-u0.6

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\log \left(1 + e^{x}\right)\right)\right)} - x \cdot y\]
    4. Using strategy rm
    5. Applied expm1-udef1.3

      \[\leadsto \color{blue}{\left(e^{\mathsf{log1p}\left(\log \left(1 + e^{x}\right)\right)} - 1\right)} - x \cdot y\]
    6. Simplified1.3

      \[\leadsto \left(\color{blue}{\left(\log \left(e^{x} + 1\right) + 1\right)} - 1\right) - x \cdot y\]
    7. Taylor expanded around 0 0.4

      \[\leadsto \color{blue}{\left(\left(\log 2 + \left(0.25 \cdot {x}^{2} + 0.5 \cdot x\right)\right) - \frac{1}{2} \cdot \frac{{x}^{2}}{{2}^{2}}\right)} - x \cdot y\]
    8. Simplified0.4

      \[\leadsto \color{blue}{\left(\log 2 + \left(\mathsf{fma}\left(0.25, {x}^{2}, 0.5 \cdot x\right) - \frac{1}{2} \cdot \frac{{x}^{2}}{{2}^{2}}\right)\right)} - x \cdot y\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -4.14987374688656041 \cdot 10^{-4}:\\ \;\;\;\;\left(\left(\log \left(e^{x} + 1\right) + 1\right) - 1\right) - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\left(\log 2 + \left(\mathsf{fma}\left(0.25, {x}^{2}, 0.5 \cdot x\right) - \frac{1}{2} \cdot \frac{{x}^{2}}{{2}^{2}}\right)\right) - x \cdot y\\ \end{array}\]

Reproduce

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