Average Error: 29.3 → 0.2
Time: 4.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 -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}:\\ \;\;\;\;\begin{array}{l} t_1 := \frac{\sqrt{2}}{\sqrt{t_0 + 1}}\\ \mathsf{fma}\left(t_1, t_1, -1\right) \end{array}\\ \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}:\\
\;\;\;\;\begin{array}{l}
t_1 := \frac{\sqrt{2}}{\sqrt{t_0 + 1}}\\
\mathsf{fma}\left(t_1, t_1, -1\right)
\end{array}\\


\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)
       (let* ((t_1 (/ (sqrt 2.0) (sqrt (+ t_0 1.0))))) (fma t_1 t_1 -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 {
		double t_1 = sqrt(2.0) / sqrt(t_0 + 1.0);
		tmp = fma(t_1, t_1, -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.5

      \[\leadsto \frac{2}{\color{blue}{\sqrt{1 + e^{-2 \cdot x}} \cdot \sqrt{1 + e^{-2 \cdot x}}}} - 1 \]
    3. Applied add-sqr-sqrt_binary640.5

      \[\leadsto \frac{\color{blue}{\sqrt{2} \cdot \sqrt{2}}}{\sqrt{1 + e^{-2 \cdot x}} \cdot \sqrt{1 + e^{-2 \cdot x}}} - 1 \]
    4. Applied times-frac_binary640.5

      \[\leadsto \color{blue}{\frac{\sqrt{2}}{\sqrt{1 + e^{-2 \cdot x}}} \cdot \frac{\sqrt{2}}{\sqrt{1 + e^{-2 \cdot x}}}} - 1 \]
    5. Applied fma-neg_binary640.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\sqrt{2}}{\sqrt{1 + e^{-2 \cdot x}}}, \frac{\sqrt{2}}{\sqrt{1 + e^{-2 \cdot x}}}, -1\right)} \]
    6. Simplified0.5

      \[\leadsto \mathsf{fma}\left(\frac{\sqrt{2}}{\sqrt{1 + e^{-2 \cdot x}}}, \frac{\sqrt{2}}{\sqrt{1 + e^{-2 \cdot x}}}, \color{blue}{-1}\right) \]
  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}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\sqrt{2}}{\sqrt{e^{-2 \cdot x} + 1}}, \frac{\sqrt{2}}{\sqrt{e^{-2 \cdot x} + 1}}, -1\right)\\ \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))