Average Error: 28.8 → 0.3
Time: 6.5s
Precision: binary64
\[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
\[\begin{array}{l} t_0 := e^{-2 \cdot x}\\ \mathbf{if}\;-2 \cdot x \leq -0.2058449771208138:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\log \left(e^{\frac{2}{1 + t_0} - 1}\right)\right)\right)\\ \mathbf{elif}\;-2 \cdot x \leq 6.329283615984725 \cdot 10^{-12}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{2}{\mathsf{fma}\left(1, 1, t_0\right)} - 1}\right)\\ \end{array} \]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
t_0 := e^{-2 \cdot x}\\
\mathbf{if}\;-2 \cdot x \leq -0.2058449771208138:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\log \left(e^{\frac{2}{1 + t_0} - 1}\right)\right)\right)\\

\mathbf{elif}\;-2 \cdot x \leq 6.329283615984725 \cdot 10^{-12}:\\
\;\;\;\;x\\

\mathbf{else}:\\
\;\;\;\;\log \left(e^{\frac{2}{\mathsf{fma}\left(1, 1, t_0\right)} - 1}\right)\\


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

Error

Bits error versus x

Bits error versus y

Derivation

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

    1. Initial program 0.0

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

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

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

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

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

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

    if -0.20584497712081379 < (*.f64 -2 x) < 6.3292836159847248e-12

    1. Initial program 59.5

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

      \[\leadsto \color{blue}{x} \]

    if 6.3292836159847248e-12 < (*.f64 -2 x)

    1. Initial program 0.7

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Applied add-log-exp_binary640.7

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

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

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

      \[\leadsto \log \color{blue}{\left(e^{\frac{2}{1 + e^{x \cdot -2}} - 1}\right)} \]
    6. Applied *-un-lft-identity_binary640.7

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.2058449771208138:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)\right)\right)\\ \mathbf{elif}\;-2 \cdot x \leq 6.329283615984725 \cdot 10^{-12}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{2}{\mathsf{fma}\left(1, 1, e^{-2 \cdot x}\right)} - 1}\right)\\ \end{array} \]

Reproduce

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