Average Error: 29.4 → 0.3
Time: 3.3s
Precision: 64
\[\frac{2}{1 + e^{-2 \cdot x}} - 1\]
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \le -0.339964758204841233 \lor \neg \left(-2 \cdot x \le 1.74315037003436618 \cdot 10^{-13}\right):\\ \;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} - 1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(1, x, -\mathsf{fma}\left(5.55112 \cdot 10^{-17}, {x}^{4}, 0.33333333333333337 \cdot {x}^{3}\right)\right)\\ \end{array}\]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \le -0.339964758204841233 \lor \neg \left(-2 \cdot x \le 1.74315037003436618 \cdot 10^{-13}\right):\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} - 1\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(1, x, -\mathsf{fma}\left(5.55112 \cdot 10^{-17}, {x}^{4}, 0.33333333333333337 \cdot {x}^{3}\right)\right)\\

\end{array}
double f(double x, double __attribute__((unused)) y) {
        double r46647 = 2.0;
        double r46648 = 1.0;
        double r46649 = -2.0;
        double r46650 = x;
        double r46651 = r46649 * r46650;
        double r46652 = exp(r46651);
        double r46653 = r46648 + r46652;
        double r46654 = r46647 / r46653;
        double r46655 = r46654 - r46648;
        return r46655;
}

double f(double x, double __attribute__((unused)) y) {
        double r46656 = -2.0;
        double r46657 = x;
        double r46658 = r46656 * r46657;
        double r46659 = -0.33996475820484123;
        bool r46660 = r46658 <= r46659;
        double r46661 = 1.7431503700343662e-13;
        bool r46662 = r46658 <= r46661;
        double r46663 = !r46662;
        bool r46664 = r46660 || r46663;
        double r46665 = 2.0;
        double r46666 = 1.0;
        double r46667 = exp(r46658);
        double r46668 = r46666 + r46667;
        double r46669 = r46665 / r46668;
        double r46670 = r46669 - r46666;
        double r46671 = 5.551115123125783e-17;
        double r46672 = 4.0;
        double r46673 = pow(r46657, r46672);
        double r46674 = 0.33333333333333337;
        double r46675 = 3.0;
        double r46676 = pow(r46657, r46675);
        double r46677 = r46674 * r46676;
        double r46678 = fma(r46671, r46673, r46677);
        double r46679 = -r46678;
        double r46680 = fma(r46666, r46657, r46679);
        double r46681 = r46664 ? r46670 : r46680;
        return r46681;
}

Error

Bits error versus x

Bits error versus y

Derivation

  1. Split input into 2 regimes
  2. if (* -2.0 x) < -0.33996475820484123 or 1.7431503700343662e-13 < (* -2.0 x)

    1. Initial program 0.4

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1\]

    if -0.33996475820484123 < (* -2.0 x) < 1.7431503700343662e-13

    1. Initial program 59.5

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(1, x, -\mathsf{fma}\left(5.55112 \cdot 10^{-17}, {x}^{4}, 0.33333333333333337 \cdot {x}^{3}\right)\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;-2 \cdot x \le -0.339964758204841233 \lor \neg \left(-2 \cdot x \le 1.74315037003436618 \cdot 10^{-13}\right):\\ \;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} - 1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(1, x, -\mathsf{fma}\left(5.55112 \cdot 10^{-17}, {x}^{4}, 0.33333333333333337 \cdot {x}^{3}\right)\right)\\ \end{array}\]

Reproduce

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