Average Error: 30.0 → 1.3
Time: 4.5s
Precision: binary64
\[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
\[\begin{array}{l} t_0 := \frac{2}{1 + e^{-2 \cdot x}} - 1\\ \mathbf{if}\;-2 \cdot x \leq -8.006266000637776 \cdot 10^{+24}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;-2 \cdot x \leq 0.0001314255759160979:\\ \;\;\;\;\mathsf{fma}\left({x}^{3}, -0.3333333333333333, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
t_0 := \frac{2}{1 + e^{-2 \cdot x}} - 1\\
\mathbf{if}\;-2 \cdot x \leq -8.006266000637776 \cdot 10^{+24}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;-2 \cdot x \leq 0.0001314255759160979:\\
\;\;\;\;\mathsf{fma}\left({x}^{3}, -0.3333333333333333, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, x\right)\right)\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\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 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0)))
   (if (<= (* -2.0 x) -8.006266000637776e+24)
     t_0
     (if (<= (* -2.0 x) 0.0001314255759160979)
       (fma
        (pow x 3.0)
        -0.3333333333333333
        (fma 0.13333333333333333 (pow x 5.0) x))
       t_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 = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
	double tmp;
	if ((-2.0 * x) <= -8.006266000637776e+24) {
		tmp = t_0;
	} else if ((-2.0 * x) <= 0.0001314255759160979) {
		tmp = fma(pow(x, 3.0), -0.3333333333333333, fma(0.13333333333333333, pow(x, 5.0), x));
	} else {
		tmp = t_0;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Derivation

  1. Split input into 2 regimes
  2. if (*.f64 -2 x) < -8.0062660006377761e24 or 1.31425575916097889e-4 < (*.f64 -2 x)

    1. Initial program 0.1

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

    if -8.0062660006377761e24 < (*.f64 -2 x) < 1.31425575916097889e-4

    1. Initial program 56.8

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

      \[\leadsto \color{blue}{\left(0.13333333333333333 \cdot {x}^{5} + x\right) - 0.3333333333333333 \cdot {x}^{3}} \]
    3. Simplified2.4

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{3}, -0.3333333333333333, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, x\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification1.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -8.006266000637776 \cdot 10^{+24}:\\ \;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} - 1\\ \mathbf{elif}\;-2 \cdot x \leq 0.0001314255759160979:\\ \;\;\;\;\mathsf{fma}\left({x}^{3}, -0.3333333333333333, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} - 1\\ \end{array} \]

Reproduce

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