Average Error: 29.0 → 0.1
Time: 4.3s
Precision: binary64
\[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.26266776566857986:\\ \;\;\;\;\mathsf{fma}\left(\frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)}, -\mathsf{expm1}\left(-2 \cdot x\right), -1\right)\\ \mathbf{elif}\;-2 \cdot x \leq 6.3780832365879 \cdot 10^{-6}:\\ \;\;\;\;\mathsf{fma}\left({x}^{3}, -0.3333333333333333, x\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\frac{e^{\frac{2}{1 + e^{-2 \cdot x}}}}{e}\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.26266776566857986)
   (fma (/ -2.0 (expm1 (* x -4.0))) (- (expm1 (* -2.0 x))) -1.0)
   (if (<= (* -2.0 x) 6.3780832365879e-6)
     (fma (pow x 3.0) -0.3333333333333333 x)
     (log (/ (exp (/ 2.0 (+ 1.0 (exp (* -2.0 x))))) E)))))
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.26266776566857986) {
		tmp = fma((-2.0 / expm1((x * -4.0))), -expm1((-2.0 * x)), -1.0);
	} else if ((-2.0 * x) <= 6.3780832365879e-6) {
		tmp = fma(pow(x, 3.0), -0.3333333333333333, x);
	} else {
		tmp = log((exp((2.0 / (1.0 + exp((-2.0 * x))))) / ((double) M_E)));
	}
	return tmp;
}
function code(x, y)
	return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0)
end
function code(x, y)
	tmp = 0.0
	if (Float64(-2.0 * x) <= -0.26266776566857986)
		tmp = fma(Float64(-2.0 / expm1(Float64(x * -4.0))), Float64(-expm1(Float64(-2.0 * x))), -1.0);
	elseif (Float64(-2.0 * x) <= 6.3780832365879e-6)
		tmp = fma((x ^ 3.0), -0.3333333333333333, x);
	else
		tmp = log(Float64(exp(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x))))) / exp(1)));
	end
	return tmp
end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.26266776566857986], N[(N[(-2.0 / N[(Exp[N[(x * -4.0), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision] * (-N[(Exp[N[(-2.0 * x), $MachinePrecision]] - 1), $MachinePrecision]) + -1.0), $MachinePrecision], If[LessEqual[N[(-2.0 * x), $MachinePrecision], 6.3780832365879e-6], N[(N[Power[x, 3.0], $MachinePrecision] * -0.3333333333333333 + x), $MachinePrecision], N[Log[N[(N[Exp[N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / E), $MachinePrecision]], $MachinePrecision]]]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.26266776566857986:\\
\;\;\;\;\mathsf{fma}\left(\frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)}, -\mathsf{expm1}\left(-2 \cdot x\right), -1\right)\\

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

\mathbf{else}:\\
\;\;\;\;\log \left(\frac{e^{\frac{2}{1 + e^{-2 \cdot x}}}}{e}\right)\\


\end{array}

Error

Bits error versus x

Bits error versus y

Derivation

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

    1. Initial program 0.0

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

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

      \[\leadsto \frac{2}{\color{blue}{\frac{1 \cdot 1 - e^{-2 \cdot x} \cdot e^{-2 \cdot x}}{1 - e^{-2 \cdot x}}}} - \left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right) \cdot \sqrt[3]{1} \]
    4. Applied associate-/r/_binary640.0

      \[\leadsto \color{blue}{\frac{2}{1 \cdot 1 - e^{-2 \cdot x} \cdot e^{-2 \cdot x}} \cdot \left(1 - e^{-2 \cdot x}\right)} - \left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right) \cdot \sqrt[3]{1} \]
    5. Applied prod-diff_binary640.0

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{2}{-\mathsf{expm1}\left(x \cdot -4\right)}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right)} + \mathsf{fma}\left(-\sqrt[3]{1}, \sqrt[3]{1} \cdot \sqrt[3]{1}, \sqrt[3]{1} \cdot \left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right)\right) \]
    7. Simplified0.0

      \[\leadsto \mathsf{fma}\left(\frac{2}{-\mathsf{expm1}\left(x \cdot -4\right)}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) + \color{blue}{0} \]
    8. Taylor expanded in x around inf 0.0

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{-2}{e^{-4 \cdot x} - 1}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) + 0 \]
    9. Simplified0.0

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) + 0 \]

    if -0.26266776566857986 < (*.f64 -2 x) < 6.37808323658789988e-6

    1. Initial program 59.0

      \[\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 6.37808323658789988e-6 < (*.f64 -2 x)

    1. Initial program 0.2

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

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

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

      \[\leadsto \color{blue}{\log \left(\frac{e^{\frac{2}{1 + e^{-2 \cdot x}}}}{e^{1}}\right)} \]
    5. Taylor expanded in x around inf 0.1

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

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

Reproduce

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