Average Error: 29.4 → 0.1
Time: 20.6s
Precision: 64
\[\frac{2}{1 + e^{-2 \cdot x}} - 1\]
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \le -0.006660654438723100610741933991221230826341:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{\sqrt{1 + e^{-2 \cdot x}}}, \frac{2}{\sqrt{1 + e^{-2 \cdot x}}}, -1\right)\\ \mathbf{elif}\;-2 \cdot x \le 5.083912517384328794752684776980800052115 \cdot 10^{-5}:\\ \;\;\;\;1 \cdot x - \mathsf{fma}\left(5.5511151231257827021181583404541015625 \cdot 10^{-17}, {x}^{4}, 0.3333333333333333703407674875052180141211 \cdot {x}^{3}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\sqrt{2}, \frac{\sqrt{2}}{1 + e^{-2 \cdot x}}, -1\right)\\ \end{array}\]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \le -0.006660654438723100610741933991221230826341:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{\sqrt{1 + e^{-2 \cdot x}}}, \frac{2}{\sqrt{1 + e^{-2 \cdot x}}}, -1\right)\\

\mathbf{elif}\;-2 \cdot x \le 5.083912517384328794752684776980800052115 \cdot 10^{-5}:\\
\;\;\;\;1 \cdot x - \mathsf{fma}\left(5.5511151231257827021181583404541015625 \cdot 10^{-17}, {x}^{4}, 0.3333333333333333703407674875052180141211 \cdot {x}^{3}\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\sqrt{2}, \frac{\sqrt{2}}{1 + e^{-2 \cdot x}}, -1\right)\\

\end{array}
double f(double x, double __attribute__((unused)) y) {
        double r34354 = 2.0;
        double r34355 = 1.0;
        double r34356 = -2.0;
        double r34357 = x;
        double r34358 = r34356 * r34357;
        double r34359 = exp(r34358);
        double r34360 = r34355 + r34359;
        double r34361 = r34354 / r34360;
        double r34362 = r34361 - r34355;
        return r34362;
}

double f(double x, double __attribute__((unused)) y) {
        double r34363 = -2.0;
        double r34364 = x;
        double r34365 = r34363 * r34364;
        double r34366 = -0.006660654438723101;
        bool r34367 = r34365 <= r34366;
        double r34368 = 1.0;
        double r34369 = 1.0;
        double r34370 = exp(r34365);
        double r34371 = r34369 + r34370;
        double r34372 = sqrt(r34371);
        double r34373 = r34368 / r34372;
        double r34374 = 2.0;
        double r34375 = r34374 / r34372;
        double r34376 = -r34369;
        double r34377 = fma(r34373, r34375, r34376);
        double r34378 = 5.083912517384329e-05;
        bool r34379 = r34365 <= r34378;
        double r34380 = r34369 * r34364;
        double r34381 = 5.551115123125783e-17;
        double r34382 = 4.0;
        double r34383 = pow(r34364, r34382);
        double r34384 = 0.33333333333333337;
        double r34385 = 3.0;
        double r34386 = pow(r34364, r34385);
        double r34387 = r34384 * r34386;
        double r34388 = fma(r34381, r34383, r34387);
        double r34389 = r34380 - r34388;
        double r34390 = sqrt(r34374);
        double r34391 = r34390 / r34371;
        double r34392 = fma(r34390, r34391, r34376);
        double r34393 = r34379 ? r34389 : r34392;
        double r34394 = r34367 ? r34377 : r34393;
        return r34394;
}

Error

Bits error versus x

Bits error versus y

Derivation

  1. Split input into 3 regimes
  2. if (* -2.0 x) < -0.006660654438723101

    1. Initial program 0.0

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt0.0

      \[\leadsto \frac{2}{\color{blue}{\sqrt{1 + e^{-2 \cdot x}} \cdot \sqrt{1 + e^{-2 \cdot x}}}} - 1\]
    4. Applied *-un-lft-identity0.0

      \[\leadsto \frac{\color{blue}{1 \cdot 2}}{\sqrt{1 + e^{-2 \cdot x}} \cdot \sqrt{1 + e^{-2 \cdot x}}} - 1\]
    5. Applied times-frac0.0

      \[\leadsto \color{blue}{\frac{1}{\sqrt{1 + e^{-2 \cdot x}}} \cdot \frac{2}{\sqrt{1 + e^{-2 \cdot x}}}} - 1\]
    6. Applied fma-neg0.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{\sqrt{1 + e^{-2 \cdot x}}}, \frac{2}{\sqrt{1 + e^{-2 \cdot x}}}, -1\right)}\]

    if -0.006660654438723101 < (* -2.0 x) < 5.083912517384329e-05

    1. Initial program 59.2

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1\]
    2. Taylor expanded around 0 0.0

      \[\leadsto \color{blue}{1 \cdot x - \left(5.5511151231257827021181583404541015625 \cdot 10^{-17} \cdot {x}^{4} + 0.3333333333333333703407674875052180141211 \cdot {x}^{3}\right)}\]
    3. Simplified0.0

      \[\leadsto \color{blue}{1 \cdot x - \mathsf{fma}\left(5.5511151231257827021181583404541015625 \cdot 10^{-17}, {x}^{4}, 0.3333333333333333703407674875052180141211 \cdot {x}^{3}\right)}\]

    if 5.083912517384329e-05 < (* -2.0 x)

    1. Initial program 0.1

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1\]
    2. Using strategy rm
    3. Applied *-un-lft-identity0.1

      \[\leadsto \frac{2}{\color{blue}{1 \cdot \left(1 + e^{-2 \cdot x}\right)}} - 1\]
    4. Applied add-sqr-sqrt0.2

      \[\leadsto \frac{\color{blue}{\sqrt{2} \cdot \sqrt{2}}}{1 \cdot \left(1 + e^{-2 \cdot x}\right)} - 1\]
    5. Applied times-frac0.1

      \[\leadsto \color{blue}{\frac{\sqrt{2}}{1} \cdot \frac{\sqrt{2}}{1 + e^{-2 \cdot x}}} - 1\]
    6. Applied fma-neg0.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;-2 \cdot x \le -0.006660654438723100610741933991221230826341:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{\sqrt{1 + e^{-2 \cdot x}}}, \frac{2}{\sqrt{1 + e^{-2 \cdot x}}}, -1\right)\\ \mathbf{elif}\;-2 \cdot x \le 5.083912517384328794752684776980800052115 \cdot 10^{-5}:\\ \;\;\;\;1 \cdot x - \mathsf{fma}\left(5.5511151231257827021181583404541015625 \cdot 10^{-17}, {x}^{4}, 0.3333333333333333703407674875052180141211 \cdot {x}^{3}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\sqrt{2}, \frac{\sqrt{2}}{1 + e^{-2 \cdot x}}, -1\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2019325 +o rules:numerics
(FPCore (x y)
  :name "Logistic function from Lakshay Garg"
  :precision binary64
  (- (/ 2 (+ 1 (exp (* -2 x)))) 1))