Average Error: 29.3 → 0.5
Time: 7.2s
Precision: binary64
Cost: 72324
\[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
\[\begin{array}{l} t_0 := 1 + {\left(e^{x}\right)}^{-2}\\ \mathbf{if}\;\frac{2}{1 + e^{-2 \cdot x}} \leq 1.000005:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(8, {t_0}^{-3}, -1\right)}{\mathsf{fma}\left(4, {t_0}^{-2}, 1\right) + \frac{2}{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 (+ 1.0 (pow (exp x) -2.0))))
   (if (<= (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.000005)
     (expm1 x)
     (/
      (fma 8.0 (pow t_0 -3.0) -1.0)
      (+ (fma 4.0 (pow t_0 -2.0) 1.0) (/ 2.0 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 = 1.0 + pow(exp(x), -2.0);
	double tmp;
	if ((2.0 / (1.0 + exp((-2.0 * x)))) <= 1.000005) {
		tmp = expm1(x);
	} else {
		tmp = fma(8.0, pow(t_0, -3.0), -1.0) / (fma(4.0, pow(t_0, -2.0), 1.0) + (2.0 / t_0));
	}
	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)
	t_0 = Float64(1.0 + (exp(x) ^ -2.0))
	tmp = 0.0
	if (Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) <= 1.000005)
		tmp = expm1(x);
	else
		tmp = Float64(fma(8.0, (t_0 ^ -3.0), -1.0) / Float64(fma(4.0, (t_0 ^ -2.0), 1.0) + Float64(2.0 / t_0)));
	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_] := Block[{t$95$0 = N[(1.0 + N[Power[N[Exp[x], $MachinePrecision], -2.0], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1.000005], N[(Exp[x] - 1), $MachinePrecision], N[(N[(8.0 * N[Power[t$95$0, -3.0], $MachinePrecision] + -1.0), $MachinePrecision] / N[(N[(4.0 * N[Power[t$95$0, -2.0], $MachinePrecision] + 1.0), $MachinePrecision] + N[(2.0 / t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
t_0 := 1 + {\left(e^{x}\right)}^{-2}\\
\mathbf{if}\;\frac{2}{1 + e^{-2 \cdot x}} \leq 1.000005:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(8, {t_0}^{-3}, -1\right)}{\mathsf{fma}\left(4, {t_0}^{-2}, 1\right) + \frac{2}{t_0}}\\


\end{array}

Error

Derivation

  1. Split input into 2 regimes
  2. if (/.f64 2 (+.f64 1 (exp.f64 (*.f64 -2 x)))) < 1.00000500000000003

    1. Initial program 39.6

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Applied egg-rr39.6

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1\right) - \mathsf{log1p}\left({\left(e^{x}\right)}^{-2}\right)\right)} \]
    3. Taylor expanded in x around 0 0.7

      \[\leadsto \mathsf{expm1}\left(\color{blue}{x}\right) \]

    if 1.00000500000000003 < (/.f64 2 (+.f64 1 (exp.f64 (*.f64 -2 x))))

    1. Initial program 0.1

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Applied egg-rr0.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{2}{1 + e^{-2 \cdot x}} \leq 1.000005:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(8, {\left(1 + {\left(e^{x}\right)}^{-2}\right)}^{-3}, -1\right)}{\mathsf{fma}\left(4, {\left(1 + {\left(e^{x}\right)}^{-2}\right)}^{-2}, 1\right) + \frac{2}{1 + {\left(e^{x}\right)}^{-2}}}\\ \end{array} \]

Alternatives

Alternative 1
Error0.5
Cost39684
\[\begin{array}{l} t_0 := 1 + {\left(e^{x}\right)}^{-2}\\ \mathbf{if}\;-2 \cdot x \leq -1 \cdot 10^{-5}:\\ \;\;\;\;\frac{\mathsf{fma}\left(4, {t_0}^{-2}, -1\right)}{1 + \frac{2}{t_0}}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \]
Alternative 2
Error0.5
Cost26180
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -1 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(1\right) - \mathsf{log1p}\left(e^{-2 \cdot x}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \]
Alternative 3
Error0.5
Cost13892
\[\begin{array}{l} t_0 := \frac{2}{1 + e^{-2 \cdot x}}\\ \mathbf{if}\;t_0 \leq 1.000005:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 + -1\\ \end{array} \]
Alternative 4
Error14.0
Cost6724
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -5 \cdot 10^{-15}:\\ \;\;\;\;\frac{1}{\frac{2 + x}{2 \cdot x}}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \]
Alternative 5
Error14.3
Cost836
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \leq 0.002:\\ \;\;\;\;x \cdot \frac{2}{2 + x}\\ \mathbf{else}:\\ \;\;\;\;-1 + \frac{2}{2 + -2 \cdot x}\\ \end{array} \]
Alternative 6
Error28.5
Cost452
\[\begin{array}{l} \mathbf{if}\;x \leq 0.04099080273022204:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;2 + \frac{-4}{x}\\ \end{array} \]
Alternative 7
Error29.4
Cost448
\[x \cdot \frac{2}{2 + x} \]
Alternative 8
Error28.5
Cost196
\[\begin{array}{l} \mathbf{if}\;x \leq 0.04099080273022204:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;2\\ \end{array} \]
Alternative 9
Error30.7
Cost64
\[x \]

Error

Reproduce

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