Average Error: 29.6 → 0.1
Time: 4.7s
Precision: binary64
\[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.01178193138190102:\\ \;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{-2 \cdot x}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{fma}\left(x, x \cdot -0.5, x\right)\right)\\ \end{array} \]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.01178193138190102:\\
\;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{-2 \cdot x}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{fma}\left(x, x \cdot -0.5, x\right)\right)\\


\end{array}
(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
(FPCore (x y)
 :precision binary64
 (if (<= (* -2.0 x) -0.01178193138190102)
   (expm1 (- (log 2.0) (log1p (exp (* -2.0 x)))))
   (expm1 (fma x (* x -0.5) x))))
double code(double x, double y) {
	return (2.0 / (1.0 + exp(-2.0 * x))) - 1.0;
}
double code(double x, double y) {
	double tmp;
	if ((-2.0 * x) <= -0.01178193138190102) {
		tmp = expm1(log(2.0) - log1p(exp(-2.0 * x)));
	} else {
		tmp = expm1(fma(x, (x * -0.5), x));
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Derivation

  1. Split input into 2 regimes
  2. if (*.f64 -2 x) < -0.01178193138190102

    1. Initial program 0.0

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Applied add-exp-log_binary640.0

      \[\leadsto \frac{2}{\color{blue}{e^{\log \left(1 + e^{-2 \cdot x}\right)}}} - 1 \]
    3. Applied add-exp-log_binary640.0

      \[\leadsto \frac{\color{blue}{e^{\log 2}}}{e^{\log \left(1 + e^{-2 \cdot x}\right)}} - 1 \]
    4. Applied div-exp_binary640.0

      \[\leadsto \color{blue}{e^{\log 2 - \log \left(1 + e^{-2 \cdot x}\right)}} - 1 \]
    5. Applied expm1-def_binary640.0

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\log 2 - \log \left(1 + e^{-2 \cdot x}\right)\right)} \]
    6. Applied log1p-expm1-u_binary640.0

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

      \[\leadsto \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(\color{blue}{e^{x \cdot -2}}\right)\right) \]

    if -0.01178193138190102 < (*.f64 -2 x)

    1. Initial program 39.4

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Applied add-exp-log_binary6439.4

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

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

      \[\leadsto \color{blue}{e^{\log 2 - \log \left(1 + e^{-2 \cdot x}\right)}} - 1 \]
    5. Applied expm1-def_binary6439.4

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\log 2 - \log \left(1 + e^{-2 \cdot x}\right)\right)} \]
    6. Taylor expanded in x around 0 0.2

      \[\leadsto \mathsf{expm1}\left(\color{blue}{x - 0.5 \cdot {x}^{2}}\right) \]
    7. Simplified0.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.01178193138190102:\\ \;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{-2 \cdot x}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{fma}\left(x, x \cdot -0.5, x\right)\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2022068 
(FPCore (x y)
  :name "Logistic function from Lakshay Garg"
  :precision binary64
  (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))