Average Error: 29.4 → 0.1
Time: 18.8s
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 r48922 = 2.0;
        double r48923 = 1.0;
        double r48924 = -2.0;
        double r48925 = x;
        double r48926 = r48924 * r48925;
        double r48927 = exp(r48926);
        double r48928 = r48923 + r48927;
        double r48929 = r48922 / r48928;
        double r48930 = r48929 - r48923;
        return r48930;
}

double f(double x, double __attribute__((unused)) y) {
        double r48931 = -2.0;
        double r48932 = x;
        double r48933 = r48931 * r48932;
        double r48934 = -0.006660654438723101;
        bool r48935 = r48933 <= r48934;
        double r48936 = 1.0;
        double r48937 = 1.0;
        double r48938 = exp(r48933);
        double r48939 = r48937 + r48938;
        double r48940 = sqrt(r48939);
        double r48941 = r48936 / r48940;
        double r48942 = 2.0;
        double r48943 = r48942 / r48940;
        double r48944 = -r48937;
        double r48945 = fma(r48941, r48943, r48944);
        double r48946 = 5.083912517384329e-05;
        bool r48947 = r48933 <= r48946;
        double r48948 = r48937 * r48932;
        double r48949 = 5.551115123125783e-17;
        double r48950 = 4.0;
        double r48951 = pow(r48932, r48950);
        double r48952 = 0.33333333333333337;
        double r48953 = 3.0;
        double r48954 = pow(r48932, r48953);
        double r48955 = r48952 * r48954;
        double r48956 = fma(r48949, r48951, r48955);
        double r48957 = r48948 - r48956;
        double r48958 = sqrt(r48942);
        double r48959 = r48958 / r48939;
        double r48960 = fma(r48958, r48959, r48944);
        double r48961 = r48947 ? r48957 : r48960;
        double r48962 = r48935 ? r48945 : r48961;
        return r48962;
}

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))