Logistic function from Lakshay Garg

Percentage Accurate: 54.1% → 99.8%
Time: 5.0s
Alternatives: 4
Speedup: 0.9×

Specification

?
\[\begin{array}{l} \\ \frac{2}{1 + e^{-2 \cdot x}} - 1 \end{array} \]
(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
double code(double x, double y) {
	return (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) - 1.0d0
end function
public static double code(double x, double y) {
	return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
def code(x, y):
	return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
function code(x, y)
	return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0)
end
function tmp = code(x, y)
	tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}

\\
\frac{2}{1 + e^{-2 \cdot x}} - 1
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 54.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{2}{1 + e^{-2 \cdot x}} - 1 \end{array} \]
(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
double code(double x, double y) {
	return (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) - 1.0d0
end function
public static double code(double x, double y) {
	return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
def code(x, y):
	return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
function code(x, y)
	return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0)
end
function tmp = code(x, y)
	tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}

\\
\frac{2}{1 + e^{-2 \cdot x}} - 1
\end{array}

Alternative 1: 99.8% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.5:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{expm1}\left(-2 \cdot x\right), \frac{2}{\mathsf{expm1}\left(x \cdot -4\right)}, -1\right)\\ \mathbf{elif}\;-2 \cdot x \leq 4 \cdot 10^{-11}:\\ \;\;\;\;-0.05396825396825397 \cdot {x}^{7} + \left(-0.3333333333333333 \cdot {x}^{3} + \left(x + 0.13333333333333333 \cdot {x}^{5}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (* -2.0 x) -0.5)
   (fma (expm1 (* -2.0 x)) (/ 2.0 (expm1 (* x -4.0))) -1.0)
   (if (<= (* -2.0 x) 4e-11)
     (+
      (* -0.05396825396825397 (pow x 7.0))
      (+
       (* -0.3333333333333333 (pow x 3.0))
       (+ x (* 0.13333333333333333 (pow x 5.0)))))
     (+ (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) -1.0))))
double code(double x, double y) {
	double tmp;
	if ((-2.0 * x) <= -0.5) {
		tmp = fma(expm1((-2.0 * x)), (2.0 / expm1((x * -4.0))), -1.0);
	} else if ((-2.0 * x) <= 4e-11) {
		tmp = (-0.05396825396825397 * pow(x, 7.0)) + ((-0.3333333333333333 * pow(x, 3.0)) + (x + (0.13333333333333333 * pow(x, 5.0))));
	} else {
		tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if (Float64(-2.0 * x) <= -0.5)
		tmp = fma(expm1(Float64(-2.0 * x)), Float64(2.0 / expm1(Float64(x * -4.0))), -1.0);
	elseif (Float64(-2.0 * x) <= 4e-11)
		tmp = Float64(Float64(-0.05396825396825397 * (x ^ 7.0)) + Float64(Float64(-0.3333333333333333 * (x ^ 3.0)) + Float64(x + Float64(0.13333333333333333 * (x ^ 5.0)))));
	else
		tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) + -1.0);
	end
	return tmp
end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.5], N[(N[(Exp[N[(-2.0 * x), $MachinePrecision]] - 1), $MachinePrecision] * N[(2.0 / N[(Exp[N[(x * -4.0), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[N[(-2.0 * x), $MachinePrecision], 4e-11], N[(N[(-0.05396825396825397 * N[Power[x, 7.0], $MachinePrecision]), $MachinePrecision] + N[(N[(-0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(x + N[(0.13333333333333333 * N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.5:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{expm1}\left(-2 \cdot x\right), \frac{2}{\mathsf{expm1}\left(x \cdot -4\right)}, -1\right)\\

\mathbf{elif}\;-2 \cdot x \leq 4 \cdot 10^{-11}:\\
\;\;\;\;-0.05396825396825397 \cdot {x}^{7} + \left(-0.3333333333333333 \cdot {x}^{3} + \left(x + 0.13333333333333333 \cdot {x}^{5}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 -2 x) < -0.5

    1. Initial program 100.0%

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Step-by-step derivation
      1. flip-+100.0%

        \[\leadsto \frac{2}{\color{blue}{\frac{1 \cdot 1 - e^{-2 \cdot x} \cdot e^{-2 \cdot x}}{1 - e^{-2 \cdot x}}}} - 1 \]
      2. associate-/r/100.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)} - 1 \]
      3. fma-neg100.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}, -1\right)} \]
      4. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{\color{blue}{1} - e^{-2 \cdot x} \cdot e^{-2 \cdot x}}, 1 - e^{-2 \cdot x}, -1\right) \]
      5. pow2100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - \color{blue}{{\left(e^{-2 \cdot x}\right)}^{2}}}, 1 - e^{-2 \cdot x}, -1\right) \]
      6. *-commutative100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{\color{blue}{x \cdot -2}}\right)}^{2}}, 1 - e^{-2 \cdot x}, -1\right) \]
      7. exp-prod100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\color{blue}{\left({\left(e^{x}\right)}^{-2}\right)}}^{2}}, 1 - e^{-2 \cdot x}, -1\right) \]
      8. pow-pow100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - \color{blue}{{\left(e^{x}\right)}^{\left(-2 \cdot 2\right)}}}, 1 - e^{-2 \cdot x}, -1\right) \]
      9. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{\color{blue}{-4}}}, 1 - e^{-2 \cdot x}, -1\right) \]
      10. exp-prod100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, 1 - \color{blue}{{\left(e^{-2}\right)}^{x}}, -1\right) \]
      11. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, 1 - {\left(e^{-2}\right)}^{x}, \color{blue}{-1}\right) \]
    3. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, 1 - {\left(e^{-2}\right)}^{x}, -1\right)} \]
    4. Step-by-step derivation
      1. sub-neg100.0%

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

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, \color{blue}{\left(-{\left(e^{-2}\right)}^{x}\right) + 1}, -1\right) \]
      3. neg-sub0100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, \color{blue}{\left(0 - {\left(e^{-2}\right)}^{x}\right)} + 1, -1\right) \]
      4. associate-+l-100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, \color{blue}{0 - \left({\left(e^{-2}\right)}^{x} - 1\right)}, -1\right) \]
      5. exp-prod100.0%

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

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, 0 - \color{blue}{\mathsf{expm1}\left(-2 \cdot x\right)}, -1\right) \]
      7. sub0-neg100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, \color{blue}{-\mathsf{expm1}\left(-2 \cdot x\right)}, -1\right) \]
      8. *-commutative100.0%

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right)} \]
    6. Step-by-step derivation
      1. pow-exp100.0%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - \color{blue}{e^{x \cdot -4}}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
    7. Applied egg-rr100.0%

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{2}{1 - e^{-4 \cdot x}}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
    9. Step-by-step derivation
      1. metadata-eval100.0%

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

        \[\leadsto \mathsf{fma}\left(\frac{\frac{-2}{-1}}{1 - \color{blue}{{\left(e^{-4}\right)}^{x}}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      3. associate-/r*100.0%

        \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{-2}{-1 \cdot \left(1 - {\left(e^{-4}\right)}^{x}\right)}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      4. neg-mul-1100.0%

        \[\leadsto \mathsf{fma}\left(\frac{-2}{\color{blue}{-\left(1 - {\left(e^{-4}\right)}^{x}\right)}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      5. sub-neg100.0%

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

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

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

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

        \[\leadsto \mathsf{fma}\left(\frac{-2}{\color{blue}{{\left(e^{-4}\right)}^{x} - 1}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      10. exp-prod100.0%

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

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

        \[\leadsto \mathsf{fma}\left(\frac{-2}{\mathsf{expm1}\left(\color{blue}{x \cdot -4}\right)}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
    10. Simplified100.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) \]
    11. Taylor expanded in x around inf 100.0%

      \[\leadsto \color{blue}{-2 \cdot \frac{1 - e^{-2 \cdot x}}{e^{-4 \cdot x} - 1} - 1} \]
    12. Step-by-step derivation
      1. associate-*r/100.0%

        \[\leadsto \color{blue}{\frac{-2 \cdot \left(1 - e^{-2 \cdot x}\right)}{e^{-4 \cdot x} - 1}} - 1 \]
      2. expm1-def100.0%

        \[\leadsto \frac{-2 \cdot \left(1 - e^{-2 \cdot x}\right)}{\color{blue}{\mathsf{expm1}\left(-4 \cdot x\right)}} - 1 \]
      3. *-commutative100.0%

        \[\leadsto \frac{-2 \cdot \left(1 - e^{-2 \cdot x}\right)}{\mathsf{expm1}\left(\color{blue}{x \cdot -4}\right)} - 1 \]
      4. associate-/l*100.0%

        \[\leadsto \color{blue}{\frac{-2}{\frac{\mathsf{expm1}\left(x \cdot -4\right)}{1 - e^{-2 \cdot x}}}} - 1 \]
      5. associate-/r/100.0%

        \[\leadsto \color{blue}{\frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \left(1 - e^{-2 \cdot x}\right)} - 1 \]
      6. sub-neg100.0%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \color{blue}{\left(1 + \left(-e^{-2 \cdot x}\right)\right)} - 1 \]
      7. +-commutative100.0%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \color{blue}{\left(\left(-e^{-2 \cdot x}\right) + 1\right)} - 1 \]
      8. neg-sub0100.0%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \left(\color{blue}{\left(0 - e^{-2 \cdot x}\right)} + 1\right) - 1 \]
      9. associate-+l-100.0%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \color{blue}{\left(0 - \left(e^{-2 \cdot x} - 1\right)\right)} - 1 \]
      10. expm1-def100.0%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \left(0 - \color{blue}{\mathsf{expm1}\left(-2 \cdot x\right)}\right) - 1 \]
      11. neg-sub0100.0%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \color{blue}{\left(-\mathsf{expm1}\left(-2 \cdot x\right)\right)} - 1 \]
      12. distribute-rgt-neg-in100.0%

        \[\leadsto \color{blue}{\left(-\frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \mathsf{expm1}\left(-2 \cdot x\right)\right)} - 1 \]
      13. *-commutative100.0%

        \[\leadsto \left(-\color{blue}{\mathsf{expm1}\left(-2 \cdot x\right) \cdot \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)}}\right) - 1 \]
      14. distribute-rgt-neg-in100.0%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(-2 \cdot x\right) \cdot \left(-\frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)}\right)} - 1 \]
      15. mul-1-neg100.0%

        \[\leadsto \mathsf{expm1}\left(-2 \cdot x\right) \cdot \color{blue}{\left(-1 \cdot \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)}\right)} - 1 \]
    13. Simplified100.0%

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

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

    1. Initial program 7.1%

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

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

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

    1. Initial program 100.0%

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
  3. Recombined 3 regimes into one program.
  4. Final simplification100.0%

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

Alternative 2: 99.7% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.02:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{expm1}\left(-2 \cdot x\right), \frac{2}{\mathsf{expm1}\left(x \cdot -4\right)}, -1\right)\\ \mathbf{elif}\;-2 \cdot x \leq 4 \cdot 10^{-11}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (* -2.0 x) -0.02)
   (fma (expm1 (* -2.0 x)) (/ 2.0 (expm1 (* x -4.0))) -1.0)
   (if (<= (* -2.0 x) 4e-11) x (+ (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) -1.0))))
double code(double x, double y) {
	double tmp;
	if ((-2.0 * x) <= -0.02) {
		tmp = fma(expm1((-2.0 * x)), (2.0 / expm1((x * -4.0))), -1.0);
	} else if ((-2.0 * x) <= 4e-11) {
		tmp = x;
	} else {
		tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if (Float64(-2.0 * x) <= -0.02)
		tmp = fma(expm1(Float64(-2.0 * x)), Float64(2.0 / expm1(Float64(x * -4.0))), -1.0);
	elseif (Float64(-2.0 * x) <= 4e-11)
		tmp = x;
	else
		tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) + -1.0);
	end
	return tmp
end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.02], N[(N[(Exp[N[(-2.0 * x), $MachinePrecision]] - 1), $MachinePrecision] * N[(2.0 / N[(Exp[N[(x * -4.0), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[N[(-2.0 * x), $MachinePrecision], 4e-11], x, N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.02:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{expm1}\left(-2 \cdot x\right), \frac{2}{\mathsf{expm1}\left(x \cdot -4\right)}, -1\right)\\

\mathbf{elif}\;-2 \cdot x \leq 4 \cdot 10^{-11}:\\
\;\;\;\;x\\

\mathbf{else}:\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 -2 x) < -0.0200000000000000004

    1. Initial program 99.9%

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Step-by-step derivation
      1. flip-+99.8%

        \[\leadsto \frac{2}{\color{blue}{\frac{1 \cdot 1 - e^{-2 \cdot x} \cdot e^{-2 \cdot x}}{1 - e^{-2 \cdot x}}}} - 1 \]
      2. associate-/r/99.8%

        \[\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)} - 1 \]
      3. fma-neg99.8%

        \[\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}, -1\right)} \]
      4. metadata-eval99.8%

        \[\leadsto \mathsf{fma}\left(\frac{2}{\color{blue}{1} - e^{-2 \cdot x} \cdot e^{-2 \cdot x}}, 1 - e^{-2 \cdot x}, -1\right) \]
      5. pow299.8%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - \color{blue}{{\left(e^{-2 \cdot x}\right)}^{2}}}, 1 - e^{-2 \cdot x}, -1\right) \]
      6. *-commutative99.8%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{\color{blue}{x \cdot -2}}\right)}^{2}}, 1 - e^{-2 \cdot x}, -1\right) \]
      7. exp-prod99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\color{blue}{\left({\left(e^{x}\right)}^{-2}\right)}}^{2}}, 1 - e^{-2 \cdot x}, -1\right) \]
      8. pow-pow99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - \color{blue}{{\left(e^{x}\right)}^{\left(-2 \cdot 2\right)}}}, 1 - e^{-2 \cdot x}, -1\right) \]
      9. metadata-eval99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{\color{blue}{-4}}}, 1 - e^{-2 \cdot x}, -1\right) \]
      10. exp-prod99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, 1 - \color{blue}{{\left(e^{-2}\right)}^{x}}, -1\right) \]
      11. metadata-eval99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, 1 - {\left(e^{-2}\right)}^{x}, \color{blue}{-1}\right) \]
    3. Applied egg-rr99.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, 1 - {\left(e^{-2}\right)}^{x}, -1\right)} \]
    4. Step-by-step derivation
      1. sub-neg99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, \color{blue}{1 + \left(-{\left(e^{-2}\right)}^{x}\right)}, -1\right) \]
      2. +-commutative99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, \color{blue}{\left(-{\left(e^{-2}\right)}^{x}\right) + 1}, -1\right) \]
      3. neg-sub099.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, \color{blue}{\left(0 - {\left(e^{-2}\right)}^{x}\right)} + 1, -1\right) \]
      4. associate-+l-99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, \color{blue}{0 - \left({\left(e^{-2}\right)}^{x} - 1\right)}, -1\right) \]
      5. exp-prod99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, 0 - \left(\color{blue}{e^{-2 \cdot x}} - 1\right), -1\right) \]
      6. expm1-def99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, 0 - \color{blue}{\mathsf{expm1}\left(-2 \cdot x\right)}, -1\right) \]
      7. sub0-neg99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, \color{blue}{-\mathsf{expm1}\left(-2 \cdot x\right)}, -1\right) \]
      8. *-commutative99.7%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, -\mathsf{expm1}\left(\color{blue}{x \cdot -2}\right), -1\right) \]
    5. Simplified99.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{2}{1 - {\left(e^{x}\right)}^{-4}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right)} \]
    6. Step-by-step derivation
      1. pow-exp99.8%

        \[\leadsto \mathsf{fma}\left(\frac{2}{1 - \color{blue}{e^{x \cdot -4}}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
    7. Applied egg-rr99.8%

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{2}{1 - e^{-4 \cdot x}}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
    9. Step-by-step derivation
      1. metadata-eval99.8%

        \[\leadsto \mathsf{fma}\left(\frac{\color{blue}{\frac{-2}{-1}}}{1 - e^{-4 \cdot x}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      2. exp-prod99.8%

        \[\leadsto \mathsf{fma}\left(\frac{\frac{-2}{-1}}{1 - \color{blue}{{\left(e^{-4}\right)}^{x}}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      3. associate-/r*99.8%

        \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{-2}{-1 \cdot \left(1 - {\left(e^{-4}\right)}^{x}\right)}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      4. neg-mul-199.8%

        \[\leadsto \mathsf{fma}\left(\frac{-2}{\color{blue}{-\left(1 - {\left(e^{-4}\right)}^{x}\right)}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      5. sub-neg99.8%

        \[\leadsto \mathsf{fma}\left(\frac{-2}{-\color{blue}{\left(1 + \left(-{\left(e^{-4}\right)}^{x}\right)\right)}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      6. +-commutative99.8%

        \[\leadsto \mathsf{fma}\left(\frac{-2}{-\color{blue}{\left(\left(-{\left(e^{-4}\right)}^{x}\right) + 1\right)}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      7. distribute-neg-in99.8%

        \[\leadsto \mathsf{fma}\left(\frac{-2}{\color{blue}{\left(-\left(-{\left(e^{-4}\right)}^{x}\right)\right) + \left(-1\right)}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      8. remove-double-neg99.8%

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

        \[\leadsto \mathsf{fma}\left(\frac{-2}{\color{blue}{{\left(e^{-4}\right)}^{x} - 1}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      10. exp-prod99.8%

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

        \[\leadsto \mathsf{fma}\left(\frac{-2}{\color{blue}{\mathsf{expm1}\left(-4 \cdot x\right)}}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
      12. *-commutative99.9%

        \[\leadsto \mathsf{fma}\left(\frac{-2}{\mathsf{expm1}\left(\color{blue}{x \cdot -4}\right)}, -\mathsf{expm1}\left(x \cdot -2\right), -1\right) \]
    10. Simplified99.9%

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

      \[\leadsto \color{blue}{-2 \cdot \frac{1 - e^{-2 \cdot x}}{e^{-4 \cdot x} - 1} - 1} \]
    12. Step-by-step derivation
      1. associate-*r/99.7%

        \[\leadsto \color{blue}{\frac{-2 \cdot \left(1 - e^{-2 \cdot x}\right)}{e^{-4 \cdot x} - 1}} - 1 \]
      2. expm1-def99.7%

        \[\leadsto \frac{-2 \cdot \left(1 - e^{-2 \cdot x}\right)}{\color{blue}{\mathsf{expm1}\left(-4 \cdot x\right)}} - 1 \]
      3. *-commutative99.7%

        \[\leadsto \frac{-2 \cdot \left(1 - e^{-2 \cdot x}\right)}{\mathsf{expm1}\left(\color{blue}{x \cdot -4}\right)} - 1 \]
      4. associate-/l*99.7%

        \[\leadsto \color{blue}{\frac{-2}{\frac{\mathsf{expm1}\left(x \cdot -4\right)}{1 - e^{-2 \cdot x}}}} - 1 \]
      5. associate-/r/99.7%

        \[\leadsto \color{blue}{\frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \left(1 - e^{-2 \cdot x}\right)} - 1 \]
      6. sub-neg99.7%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \color{blue}{\left(1 + \left(-e^{-2 \cdot x}\right)\right)} - 1 \]
      7. +-commutative99.7%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \color{blue}{\left(\left(-e^{-2 \cdot x}\right) + 1\right)} - 1 \]
      8. neg-sub099.7%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \left(\color{blue}{\left(0 - e^{-2 \cdot x}\right)} + 1\right) - 1 \]
      9. associate-+l-99.7%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \color{blue}{\left(0 - \left(e^{-2 \cdot x} - 1\right)\right)} - 1 \]
      10. expm1-def99.9%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \left(0 - \color{blue}{\mathsf{expm1}\left(-2 \cdot x\right)}\right) - 1 \]
      11. neg-sub099.9%

        \[\leadsto \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \color{blue}{\left(-\mathsf{expm1}\left(-2 \cdot x\right)\right)} - 1 \]
      12. distribute-rgt-neg-in99.9%

        \[\leadsto \color{blue}{\left(-\frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)} \cdot \mathsf{expm1}\left(-2 \cdot x\right)\right)} - 1 \]
      13. *-commutative99.9%

        \[\leadsto \left(-\color{blue}{\mathsf{expm1}\left(-2 \cdot x\right) \cdot \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)}}\right) - 1 \]
      14. distribute-rgt-neg-in99.9%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(-2 \cdot x\right) \cdot \left(-\frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)}\right)} - 1 \]
      15. mul-1-neg99.9%

        \[\leadsto \mathsf{expm1}\left(-2 \cdot x\right) \cdot \color{blue}{\left(-1 \cdot \frac{-2}{\mathsf{expm1}\left(x \cdot -4\right)}\right)} - 1 \]
    13. Simplified99.9%

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

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

    1. Initial program 6.4%

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

      \[\leadsto \color{blue}{x} \]

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

    1. Initial program 100.0%

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
  3. Recombined 3 regimes into one program.
  4. Final simplification100.0%

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

Alternative 3: 99.7% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.02 \lor \neg \left(-2 \cdot x \leq 4 \cdot 10^{-11}\right):\\ \;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= (* -2.0 x) -0.02) (not (<= (* -2.0 x) 4e-11)))
   (+ (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) -1.0)
   x))
double code(double x, double y) {
	double tmp;
	if (((-2.0 * x) <= -0.02) || !((-2.0 * x) <= 4e-11)) {
		tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
	} else {
		tmp = x;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((((-2.0d0) * x) <= (-0.02d0)) .or. (.not. (((-2.0d0) * x) <= 4d-11))) then
        tmp = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) + (-1.0d0)
    else
        tmp = x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (((-2.0 * x) <= -0.02) || !((-2.0 * x) <= 4e-11)) {
		tmp = (2.0 / (1.0 + Math.exp((-2.0 * x)))) + -1.0;
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if ((-2.0 * x) <= -0.02) or not ((-2.0 * x) <= 4e-11):
		tmp = (2.0 / (1.0 + math.exp((-2.0 * x)))) + -1.0
	else:
		tmp = x
	return tmp
function code(x, y)
	tmp = 0.0
	if ((Float64(-2.0 * x) <= -0.02) || !(Float64(-2.0 * x) <= 4e-11))
		tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) + -1.0);
	else
		tmp = x;
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (((-2.0 * x) <= -0.02) || ~(((-2.0 * x) <= 4e-11)))
		tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.02], N[Not[LessEqual[N[(-2.0 * x), $MachinePrecision], 4e-11]], $MachinePrecision]], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], x]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.02 \lor \neg \left(-2 \cdot x \leq 4 \cdot 10^{-11}\right):\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\

\mathbf{else}:\\
\;\;\;\;x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 -2 x) < -0.0200000000000000004 or 3.99999999999999976e-11 < (*.f64 -2 x)

    1. Initial program 99.9%

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

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

    1. Initial program 6.4%

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

      \[\leadsto \color{blue}{x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -0.02 \lor \neg \left(-2 \cdot x \leq 4 \cdot 10^{-11}\right):\\ \;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]

Alternative 4: 52.3% accurate, 109.0× speedup?

\[\begin{array}{l} \\ x \end{array} \]
(FPCore (x y) :precision binary64 x)
double code(double x, double y) {
	return x;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x
end function
public static double code(double x, double y) {
	return x;
}
def code(x, y):
	return x
function code(x, y)
	return x
end
function tmp = code(x, y)
	tmp = x;
end
code[x_, y_] := x
\begin{array}{l}

\\
x
\end{array}
Derivation
  1. Initial program 53.2%

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

    \[\leadsto \color{blue}{x} \]
  3. Final simplification52.9%

    \[\leadsto x \]

Reproduce

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