Average Error: 29.3 → 0.2
Time: 5.0s
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 -1.3115628904211583:\\ \;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(t_0\right)\right)\\ \mathbf{elif}\;-2 \cdot x \leq 5.061423180174997 \cdot 10^{-11}:\\ \;\;\;\;\mathsf{fma}\left({x}^{3}, -0.3333333333333333, x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{t_0 + 1} + -1\\ \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 -1.3115628904211583:\\
\;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(t_0\right)\right)\\

\mathbf{elif}\;-2 \cdot x \leq 5.061423180174997 \cdot 10^{-11}:\\
\;\;\;\;\mathsf{fma}\left({x}^{3}, -0.3333333333333333, x\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{2}{t_0 + 1} + -1\\


\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) -1.3115628904211583)
     (expm1 (- (log 2.0) (log1p t_0)))
     (if (<= (* -2.0 x) 5.061423180174997e-11)
       (fma (pow x 3.0) -0.3333333333333333 x)
       (+ (/ 2.0 (+ t_0 1.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) <= -1.3115628904211583) {
		tmp = expm1(log(2.0) - log1p(t_0));
	} else if ((-2.0 * x) <= 5.061423180174997e-11) {
		tmp = fma(pow(x, 3.0), -0.3333333333333333, x);
	} else {
		tmp = (2.0 / (t_0 + 1.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) < -1.31156289042115826

    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. Simplified0.0

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

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

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

    if -1.31156289042115826 < (*.f64 -2 x) < 5.0614231801749972e-11

    1. Initial program 59.4

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

      \[\leadsto \color{blue}{x - 0.3333333333333333 \cdot {x}^{3}} \]
    3. Simplified0.1

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{3}, -0.3333333333333333, x\right)} \]

    if 5.0614231801749972e-11 < (*.f64 -2 x)

    1. Initial program 0.4

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

      \[\leadsto \frac{2}{1 + e^{-2 \cdot x}} - \color{blue}{\sqrt{1} \cdot \sqrt{1}} \]
    3. Applied cancel-sign-sub-inv_binary640.4

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

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

Reproduce

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