Logistic distribution

Percentage Accurate: 99.5% → 99.5%
Time: 10.1s
Alternatives: 7
Speedup: 2.0×

Specification

?
\[0 \leq s \land s \leq 1.0651631\]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{\frac{-\left|x\right|}{s}}\\ t_1 := 1 + t_0\\ \frac{t_0}{\left(s \cdot t_1\right) \cdot t_1} \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0)))
   (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
	float t_0 = expf((-fabsf(x) / s));
	float t_1 = 1.0f + t_0;
	return t_0 / ((s * t_1) * t_1);
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: t_0
    real(4) :: t_1
    t_0 = exp((-abs(x) / s))
    t_1 = 1.0e0 + t_0
    code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s)
	t_0 = exp(Float32(Float32(-abs(x)) / s))
	t_1 = Float32(Float32(1.0) + t_0)
	return Float32(t_0 / Float32(Float32(s * t_1) * t_1))
end
function tmp = code(x, s)
	t_0 = exp((-abs(x) / s));
	t_1 = single(1.0) + t_0;
	tmp = t_0 / ((s * t_1) * t_1);
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t_0\\
\frac{t_0}{\left(s \cdot t_1\right) \cdot t_1}
\end{array}
\end{array}

Sampling outcomes in binary32 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 7 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: 99.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{\frac{-\left|x\right|}{s}}\\ t_1 := 1 + t_0\\ \frac{t_0}{\left(s \cdot t_1\right) \cdot t_1} \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0)))
   (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
	float t_0 = expf((-fabsf(x) / s));
	float t_1 = 1.0f + t_0;
	return t_0 / ((s * t_1) * t_1);
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: t_0
    real(4) :: t_1
    t_0 = exp((-abs(x) / s))
    t_1 = 1.0e0 + t_0
    code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s)
	t_0 = exp(Float32(Float32(-abs(x)) / s))
	t_1 = Float32(Float32(1.0) + t_0)
	return Float32(t_0 / Float32(Float32(s * t_1) * t_1))
end
function tmp = code(x, s)
	t_0 = exp((-abs(x) / s));
	t_1 = single(1.0) + t_0;
	tmp = t_0 / ((s * t_1) * t_1);
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t_0\\
\frac{t_0}{\left(s \cdot t_1\right) \cdot t_1}
\end{array}
\end{array}

Alternative 1: 99.5% accurate, 2.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{1}{s \cdot \left(\left(e^{\frac{x}{s}} + 2\right) + e^{\frac{\left|x\right|}{-s}}\right)} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s)
 :precision binary32
 (/ 1.0 (* s (+ (+ (exp (/ x s)) 2.0) (exp (/ (fabs x) (- s)))))))
x = abs(x);
float code(float x, float s) {
	return 1.0f / (s * ((expf((x / s)) + 2.0f) + expf((fabsf(x) / -s))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (s * ((exp((x / s)) + 2.0e0) + exp((abs(x) / -s))))
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(1.0) / Float32(s * Float32(Float32(exp(Float32(x / s)) + Float32(2.0)) + exp(Float32(abs(x) / Float32(-s))))))
end
x = abs(x)
function tmp = code(x, s)
	tmp = single(1.0) / (s * ((exp((x / s)) + single(2.0)) + exp((abs(x) / -s))));
end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot \left(\left(e^{\frac{x}{s}} + 2\right) + e^{\frac{\left|x\right|}{-s}}\right)}
\end{array}
Derivation
  1. Initial program 99.3%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{\frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}} \]
  3. Step-by-step derivation
    1. *-un-lft-identity99.0%

      \[\leadsto \frac{\frac{1}{s}}{e^{\color{blue}{1 \cdot \frac{\left|x\right|}{s}}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
    2. exp-prod99.1%

      \[\leadsto \frac{\frac{1}{s}}{\color{blue}{{\left(e^{1}\right)}^{\left(\frac{\left|x\right|}{s}\right)}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
    3. exp-1-e99.1%

      \[\leadsto \frac{\frac{1}{s}}{{\color{blue}{e}}^{\left(\frac{\left|x\right|}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
    4. add-sqr-sqrt46.8%

      \[\leadsto \frac{\frac{1}{s}}{{e}^{\left(\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
    5. fabs-sqr46.8%

      \[\leadsto \frac{\frac{1}{s}}{{e}^{\left(\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
    6. add-sqr-sqrt60.3%

      \[\leadsto \frac{\frac{1}{s}}{{e}^{\left(\frac{\color{blue}{x}}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
  4. Applied egg-rr60.3%

    \[\leadsto \frac{\frac{1}{s}}{\color{blue}{{e}^{\left(\frac{x}{s}\right)}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
  5. Step-by-step derivation
    1. expm1-log1p-u58.8%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\frac{1}{s}}{{e}^{\left(\frac{x}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)\right)} \]
    2. expm1-udef58.9%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{{e}^{\left(\frac{x}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1} \]
    3. pow-to-exp58.8%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{\color{blue}{e^{\log e \cdot \frac{x}{s}}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1 \]
    4. e-exp-158.8%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\log \color{blue}{\left(e^{1}\right)} \cdot \frac{x}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1 \]
    5. add-log-exp58.9%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\color{blue}{1} \cdot \frac{x}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1 \]
    6. *-un-lft-identity58.9%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\color{blue}{\frac{x}{s}}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1 \]
    7. +-commutative58.9%

      \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \color{blue}{\left(2 + e^{\frac{\left|x\right|}{-s}}\right)}}\right)} - 1 \]
  6. Applied egg-rr58.9%

    \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)}\right)} - 1} \]
  7. Step-by-step derivation
    1. expm1-def58.9%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)}\right)\right)} \]
    2. expm1-log1p60.3%

      \[\leadsto \color{blue}{\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)}} \]
    3. associate-/l/60.3%

      \[\leadsto \color{blue}{\frac{1}{\left(e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)\right) \cdot s}} \]
    4. *-commutative60.3%

      \[\leadsto \frac{1}{\color{blue}{s \cdot \left(e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)\right)}} \]
    5. associate-+r+60.3%

      \[\leadsto \frac{1}{s \cdot \color{blue}{\left(\left(e^{\frac{x}{s}} + 2\right) + e^{\frac{\left|x\right|}{-s}}\right)}} \]
  8. Simplified60.3%

    \[\leadsto \color{blue}{\frac{1}{s \cdot \left(\left(e^{\frac{x}{s}} + 2\right) + e^{\frac{\left|x\right|}{-s}}\right)}} \]
  9. Final simplification60.3%

    \[\leadsto \frac{1}{s \cdot \left(\left(e^{\frac{x}{s}} + 2\right) + e^{\frac{\left|x\right|}{-s}}\right)} \]

Alternative 2: 99.1% accurate, 1.5× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} t_0 := e^{\frac{x}{s}}\\ \mathbf{if}\;\left|x\right| \leq 1.9999999494757503 \cdot 10^{-5}:\\ \;\;\;\;\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(t_0\right)}}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{s}}{t_0 + 3}\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s)
 :precision binary32
 (let* ((t_0 (exp (/ x s))))
   (if (<= (fabs x) 1.9999999494757503e-5)
     (/ (exp (+ (/ x s) (* -2.0 (log1p t_0)))) s)
     (/ (/ 1.0 s) (+ t_0 3.0)))))
x = abs(x);
float code(float x, float s) {
	float t_0 = expf((x / s));
	float tmp;
	if (fabsf(x) <= 1.9999999494757503e-5f) {
		tmp = expf(((x / s) + (-2.0f * log1pf(t_0)))) / s;
	} else {
		tmp = (1.0f / s) / (t_0 + 3.0f);
	}
	return tmp;
}
x = abs(x)
function code(x, s)
	t_0 = exp(Float32(x / s))
	tmp = Float32(0.0)
	if (abs(x) <= Float32(1.9999999494757503e-5))
		tmp = Float32(exp(Float32(Float32(x / s) + Float32(Float32(-2.0) * log1p(t_0)))) / s);
	else
		tmp = Float32(Float32(Float32(1.0) / s) / Float32(t_0 + Float32(3.0)));
	end
	return tmp
end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
t_0 := e^{\frac{x}{s}}\\
\mathbf{if}\;\left|x\right| \leq 1.9999999494757503 \cdot 10^{-5}:\\
\;\;\;\;\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(t_0\right)}}{s}\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{s}}{t_0 + 3}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (fabs.f32 x) < 1.99999995e-5

    1. Initial program 98.5%

      \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
    2. Step-by-step derivation
      1. associate-*l*98.5%

        \[\leadsto \frac{e^{\frac{-\left|x\right|}{s}}}{\color{blue}{s \cdot \left(\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)}} \]
      2. +-commutative98.5%

        \[\leadsto \frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\color{blue}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)} \]
      3. +-commutative98.5%

        \[\leadsto \frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \color{blue}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right)}\right)} \]
    3. Simplified98.5%

      \[\leadsto \color{blue}{\frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)}} \]
    4. Step-by-step derivation
      1. add-exp-log94.5%

        \[\leadsto \color{blue}{e^{\log \left(\frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)}\right)}} \]
      2. log-div94.5%

        \[\leadsto e^{\color{blue}{\log \left(e^{\frac{-\left|x\right|}{s}}\right) - \log \left(s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)\right)}} \]
      3. add-log-exp95.1%

        \[\leadsto e^{\color{blue}{\frac{-\left|x\right|}{s}} - \log \left(s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)\right)} \]
      4. add-sqr-sqrt-0.0%

        \[\leadsto e^{\frac{\color{blue}{\sqrt{-\left|x\right|} \cdot \sqrt{-\left|x\right|}}}{s} - \log \left(s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)\right)} \]
      5. sqrt-unprod47.4%

        \[\leadsto e^{\frac{\color{blue}{\sqrt{\left(-\left|x\right|\right) \cdot \left(-\left|x\right|\right)}}}{s} - \log \left(s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)\right)} \]
      6. sqr-neg47.4%

        \[\leadsto e^{\frac{\sqrt{\color{blue}{\left|x\right| \cdot \left|x\right|}}}{s} - \log \left(s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)\right)} \]
      7. sqrt-unprod47.1%

        \[\leadsto e^{\frac{\color{blue}{\sqrt{\left|x\right|} \cdot \sqrt{\left|x\right|}}}{s} - \log \left(s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)\right)} \]
      8. add-sqr-sqrt47.1%

        \[\leadsto e^{\frac{\color{blue}{\left|x\right|}}{s} - \log \left(s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)\right)} \]
      9. add-sqr-sqrt22.6%

        \[\leadsto e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s} - \log \left(s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)\right)} \]
      10. fabs-sqr22.6%

        \[\leadsto e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s} - \log \left(s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)\right)} \]
      11. add-sqr-sqrt72.0%

        \[\leadsto e^{\frac{\color{blue}{x}}{s} - \log \left(s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)\right)} \]
      12. *-commutative72.0%

        \[\leadsto e^{\frac{x}{s} - \log \color{blue}{\left(\left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right) \cdot s\right)}} \]
    5. Applied egg-rr94.3%

      \[\leadsto \color{blue}{e^{\frac{x}{s} - \left(2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s\right)}} \]
    6. Step-by-step derivation
      1. associate--r+94.4%

        \[\leadsto e^{\color{blue}{\left(\frac{x}{s} - 2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)\right) - \log s}} \]
      2. exp-diff94.4%

        \[\leadsto \color{blue}{\frac{e^{\frac{x}{s} - 2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{e^{\log s}}} \]
      3. cancel-sign-sub-inv94.4%

        \[\leadsto \frac{e^{\color{blue}{\frac{x}{s} + \left(-2\right) \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}}{e^{\log s}} \]
      4. metadata-eval94.4%

        \[\leadsto \frac{e^{\frac{x}{s} + \color{blue}{-2} \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{e^{\log s}} \]
      5. rem-exp-log98.8%

        \[\leadsto \frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{\color{blue}{s}} \]
    7. Simplified98.8%

      \[\leadsto \color{blue}{\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{s}} \]

    if 1.99999995e-5 < (fabs.f32 x)

    1. Initial program 100.0%

      \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}} \]
    3. Taylor expanded in s around inf 100.0%

      \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + \color{blue}{3}} \]
    4. Step-by-step derivation
      1. add-sqr-sqrt100.0%

        \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{\color{blue}{\sqrt{s} \cdot \sqrt{s}}}} + 3} \]
      2. sqrt-unprod100.0%

        \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{\color{blue}{\sqrt{s \cdot s}}}} + 3} \]
      3. sqr-neg100.0%

        \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{\sqrt{\color{blue}{\left(-s\right) \cdot \left(-s\right)}}}} + 3} \]
      4. sqrt-unprod-0.0%

        \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{\color{blue}{\sqrt{-s} \cdot \sqrt{-s}}}} + 3} \]
      5. add-sqr-sqrt4.6%

        \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{\color{blue}{-s}}} + 3} \]
      6. expm1-log1p-u4.6%

        \[\leadsto \frac{\frac{1}{s}}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(e^{\frac{\left|x\right|}{-s}}\right)\right)} + 3} \]
      7. expm1-udef4.6%

        \[\leadsto \frac{\frac{1}{s}}{\color{blue}{\left(e^{\mathsf{log1p}\left(e^{\frac{\left|x\right|}{-s}}\right)} - 1\right)} + 3} \]
    5. Applied egg-rr48.6%

      \[\leadsto \frac{\frac{1}{s}}{\color{blue}{\left(\left(e^{\frac{x}{s}} + 1\right) - 1\right)} + 3} \]
    6. Step-by-step derivation
      1. associate--l+48.6%

        \[\leadsto \frac{\frac{1}{s}}{\color{blue}{\left(e^{\frac{x}{s}} + \left(1 - 1\right)\right)} + 3} \]
      2. metadata-eval48.6%

        \[\leadsto \frac{\frac{1}{s}}{\left(e^{\frac{x}{s}} + \color{blue}{0}\right) + 3} \]
      3. +-rgt-identity48.6%

        \[\leadsto \frac{\frac{1}{s}}{\color{blue}{e^{\frac{x}{s}}} + 3} \]
    7. Simplified48.6%

      \[\leadsto \frac{\frac{1}{s}}{\color{blue}{e^{\frac{x}{s}}} + 3} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification71.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left|x\right| \leq 1.9999999494757503 \cdot 10^{-5}:\\ \;\;\;\;\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{s}}{e^{\frac{x}{s}} + 3}\\ \end{array} \]

Alternative 3: 96.3% accurate, 3.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{1}{s \cdot \left(3 + e^{\frac{\left|x\right|}{s}}\right)} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s) :precision binary32 (/ 1.0 (* s (+ 3.0 (exp (/ (fabs x) s))))))
x = abs(x);
float code(float x, float s) {
	return 1.0f / (s * (3.0f + expf((fabsf(x) / s))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (s * (3.0e0 + exp((abs(x) / s))))
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(1.0) / Float32(s * Float32(Float32(3.0) + exp(Float32(abs(x) / s)))))
end
x = abs(x)
function tmp = code(x, s)
	tmp = single(1.0) / (s * (single(3.0) + exp((abs(x) / s))));
end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot \left(3 + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
Derivation
  1. Initial program 99.3%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{\frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}} \]
  3. Taylor expanded in s around inf 95.1%

    \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + \color{blue}{3}} \]
  4. Taylor expanded in s around 0 95.5%

    \[\leadsto \color{blue}{\frac{1}{s \cdot \left(3 + e^{\frac{\left|x\right|}{s}}\right)}} \]
  5. Final simplification95.5%

    \[\leadsto \frac{1}{s \cdot \left(3 + e^{\frac{\left|x\right|}{s}}\right)} \]

Alternative 4: 96.1% accurate, 5.7× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{\frac{1}{s}}{e^{\frac{x}{s}} + 3} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ (exp (/ x s)) 3.0)))
x = abs(x);
float code(float x, float s) {
	return (1.0f / s) / (expf((x / s)) + 3.0f);
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = (1.0e0 / s) / (exp((x / s)) + 3.0e0)
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(Float32(1.0) / s) / Float32(exp(Float32(x / s)) + Float32(3.0)))
end
x = abs(x)
function tmp = code(x, s)
	tmp = (single(1.0) / s) / (exp((x / s)) + single(3.0));
end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{s}}{e^{\frac{x}{s}} + 3}
\end{array}
Derivation
  1. Initial program 99.3%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Simplified99.0%

    \[\leadsto \color{blue}{\frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}} \]
  3. Taylor expanded in s around inf 95.1%

    \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + \color{blue}{3}} \]
  4. Step-by-step derivation
    1. add-sqr-sqrt95.1%

      \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{\color{blue}{\sqrt{s} \cdot \sqrt{s}}}} + 3} \]
    2. sqrt-unprod92.1%

      \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{\color{blue}{\sqrt{s \cdot s}}}} + 3} \]
    3. sqr-neg92.1%

      \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{\sqrt{\color{blue}{\left(-s\right) \cdot \left(-s\right)}}}} + 3} \]
    4. sqrt-unprod-0.0%

      \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{\color{blue}{\sqrt{-s} \cdot \sqrt{-s}}}} + 3} \]
    5. add-sqr-sqrt26.1%

      \[\leadsto \frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{\color{blue}{-s}}} + 3} \]
    6. expm1-log1p-u26.1%

      \[\leadsto \frac{\frac{1}{s}}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(e^{\frac{\left|x\right|}{-s}}\right)\right)} + 3} \]
    7. expm1-udef26.1%

      \[\leadsto \frac{\frac{1}{s}}{\color{blue}{\left(e^{\mathsf{log1p}\left(e^{\frac{\left|x\right|}{-s}}\right)} - 1\right)} + 3} \]
  5. Applied egg-rr58.7%

    \[\leadsto \frac{\frac{1}{s}}{\color{blue}{\left(\left(e^{\frac{x}{s}} + 1\right) - 1\right)} + 3} \]
  6. Step-by-step derivation
    1. associate--l+58.7%

      \[\leadsto \frac{\frac{1}{s}}{\color{blue}{\left(e^{\frac{x}{s}} + \left(1 - 1\right)\right)} + 3} \]
    2. metadata-eval58.7%

      \[\leadsto \frac{\frac{1}{s}}{\left(e^{\frac{x}{s}} + \color{blue}{0}\right) + 3} \]
    3. +-rgt-identity58.7%

      \[\leadsto \frac{\frac{1}{s}}{\color{blue}{e^{\frac{x}{s}}} + 3} \]
  7. Simplified58.7%

    \[\leadsto \frac{\frac{1}{s}}{\color{blue}{e^{\frac{x}{s}}} + 3} \]
  8. Final simplification58.7%

    \[\leadsto \frac{\frac{1}{s}}{e^{\frac{x}{s}} + 3} \]

Alternative 5: 64.4% accurate, 41.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 1.9999999494757503 \cdot 10^{-5}:\\ \;\;\;\;\frac{0.25 + \left(\frac{x}{s} \cdot \frac{x}{s}\right) \cdot -0.0625}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x \cdot \left(\frac{x}{s} \cdot 0.5\right)}\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s)
 :precision binary32
 (if (<= x 1.9999999494757503e-5)
   (/ (+ 0.25 (* (* (/ x s) (/ x s)) -0.0625)) s)
   (/ 1.0 (* x (* (/ x s) 0.5)))))
x = abs(x);
float code(float x, float s) {
	float tmp;
	if (x <= 1.9999999494757503e-5f) {
		tmp = (0.25f + (((x / s) * (x / s)) * -0.0625f)) / s;
	} else {
		tmp = 1.0f / (x * ((x / s) * 0.5f));
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 1.9999999494757503e-5) then
        tmp = (0.25e0 + (((x / s) * (x / s)) * (-0.0625e0))) / s
    else
        tmp = 1.0e0 / (x * ((x / s) * 0.5e0))
    end if
    code = tmp
end function
x = abs(x)
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(1.9999999494757503e-5))
		tmp = Float32(Float32(Float32(0.25) + Float32(Float32(Float32(x / s) * Float32(x / s)) * Float32(-0.0625))) / s);
	else
		tmp = Float32(Float32(1.0) / Float32(x * Float32(Float32(x / s) * Float32(0.5))));
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(1.9999999494757503e-5))
		tmp = (single(0.25) + (((x / s) * (x / s)) * single(-0.0625))) / s;
	else
		tmp = single(1.0) / (x * ((x / s) * single(0.5)));
	end
	tmp_2 = tmp;
end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.9999999494757503 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.25 + \left(\frac{x}{s} \cdot \frac{x}{s}\right) \cdot -0.0625}{s}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot \left(\frac{x}{s} \cdot 0.5\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.99999995e-5

    1. Initial program 99.1%

      \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
    2. Step-by-step derivation
      1. associate-*l*99.1%

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

        \[\leadsto \frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\color{blue}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)} \]
      3. +-commutative99.1%

        \[\leadsto \frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \color{blue}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right)}\right)} \]
    3. Simplified99.1%

      \[\leadsto \color{blue}{\frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)}} \]
    4. Step-by-step derivation
      1. *-un-lft-identity99.1%

        \[\leadsto \frac{\color{blue}{1 \cdot e^{\frac{-\left|x\right|}{s}}}}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)} \]
      2. times-frac98.6%

        \[\leadsto \color{blue}{\frac{1}{s} \cdot \frac{e^{\frac{-\left|x\right|}{s}}}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)}} \]
      3. add-sqr-sqrt-0.0%

        \[\leadsto \frac{1}{s} \cdot \frac{e^{\frac{\color{blue}{\sqrt{-\left|x\right|} \cdot \sqrt{-\left|x\right|}}}{s}}}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \]
      4. sqrt-unprod31.5%

        \[\leadsto \frac{1}{s} \cdot \frac{e^{\frac{\color{blue}{\sqrt{\left(-\left|x\right|\right) \cdot \left(-\left|x\right|\right)}}}{s}}}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \]
      5. sqr-neg31.5%

        \[\leadsto \frac{1}{s} \cdot \frac{e^{\frac{\sqrt{\color{blue}{\left|x\right| \cdot \left|x\right|}}}{s}}}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \]
      6. sqrt-unprod31.2%

        \[\leadsto \frac{1}{s} \cdot \frac{e^{\frac{\color{blue}{\sqrt{\left|x\right|} \cdot \sqrt{\left|x\right|}}}{s}}}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \]
      7. add-sqr-sqrt31.2%

        \[\leadsto \frac{1}{s} \cdot \frac{e^{\frac{\color{blue}{\left|x\right|}}{s}}}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \]
      8. add-sqr-sqrt14.3%

        \[\leadsto \frac{1}{s} \cdot \frac{e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \]
      9. fabs-sqr14.3%

        \[\leadsto \frac{1}{s} \cdot \frac{e^{\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}}}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \]
      10. add-sqr-sqrt84.0%

        \[\leadsto \frac{1}{s} \cdot \frac{e^{\frac{\color{blue}{x}}{s}}}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \]
      11. pow284.0%

        \[\leadsto \frac{1}{s} \cdot \frac{e^{\frac{x}{s}}}{\color{blue}{{\left(e^{\frac{-\left|x\right|}{s}} + 1\right)}^{2}}} \]
    5. Applied egg-rr86.7%

      \[\leadsto \color{blue}{\frac{1}{s} \cdot \frac{e^{\frac{x}{s}}}{{\left(e^{\frac{x}{s}} + 1\right)}^{2}}} \]
    6. Taylor expanded in x around 0 31.3%

      \[\leadsto \frac{1}{s} \cdot \color{blue}{\left(0.25 + -0.0625 \cdot \frac{{x}^{2}}{{s}^{2}}\right)} \]
    7. Step-by-step derivation
      1. *-commutative31.3%

        \[\leadsto \frac{1}{s} \cdot \left(0.25 + \color{blue}{\frac{{x}^{2}}{{s}^{2}} \cdot -0.0625}\right) \]
      2. unpow231.3%

        \[\leadsto \frac{1}{s} \cdot \left(0.25 + \frac{\color{blue}{x \cdot x}}{{s}^{2}} \cdot -0.0625\right) \]
      3. unpow231.3%

        \[\leadsto \frac{1}{s} \cdot \left(0.25 + \frac{x \cdot x}{\color{blue}{s \cdot s}} \cdot -0.0625\right) \]
    8. Simplified31.3%

      \[\leadsto \frac{1}{s} \cdot \color{blue}{\left(0.25 + \frac{x \cdot x}{s \cdot s} \cdot -0.0625\right)} \]
    9. Step-by-step derivation
      1. associate-*l/31.3%

        \[\leadsto \color{blue}{\frac{1 \cdot \left(0.25 + \frac{x \cdot x}{s \cdot s} \cdot -0.0625\right)}{s}} \]
      2. *-un-lft-identity31.3%

        \[\leadsto \frac{\color{blue}{0.25 + \frac{x \cdot x}{s \cdot s} \cdot -0.0625}}{s} \]
    10. Applied egg-rr31.3%

      \[\leadsto \color{blue}{\frac{0.25 + \frac{x \cdot x}{s \cdot s} \cdot -0.0625}{s}} \]
    11. Taylor expanded in x around 0 31.3%

      \[\leadsto \frac{0.25 + \color{blue}{\frac{{x}^{2}}{{s}^{2}}} \cdot -0.0625}{s} \]
    12. Step-by-step derivation
      1. unpow231.3%

        \[\leadsto \frac{0.25 + \frac{\color{blue}{x \cdot x}}{{s}^{2}} \cdot -0.0625}{s} \]
      2. unpow231.3%

        \[\leadsto \frac{0.25 + \frac{x \cdot x}{\color{blue}{s \cdot s}} \cdot -0.0625}{s} \]
      3. times-frac35.8%

        \[\leadsto \frac{0.25 + \color{blue}{\left(\frac{x}{s} \cdot \frac{x}{s}\right)} \cdot -0.0625}{s} \]
    13. Simplified35.8%

      \[\leadsto \frac{0.25 + \color{blue}{\left(\frac{x}{s} \cdot \frac{x}{s}\right)} \cdot -0.0625}{s} \]

    if 1.99999995e-5 < x

    1. Initial program 100.0%

      \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}} \]
    3. Step-by-step derivation
      1. *-un-lft-identity100.0%

        \[\leadsto \frac{\frac{1}{s}}{e^{\color{blue}{1 \cdot \frac{\left|x\right|}{s}}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
      2. exp-prod100.0%

        \[\leadsto \frac{\frac{1}{s}}{\color{blue}{{\left(e^{1}\right)}^{\left(\frac{\left|x\right|}{s}\right)}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
      3. exp-1-e100.0%

        \[\leadsto \frac{\frac{1}{s}}{{\color{blue}{e}}^{\left(\frac{\left|x\right|}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
      4. add-sqr-sqrt100.0%

        \[\leadsto \frac{\frac{1}{s}}{{e}^{\left(\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
      5. fabs-sqr100.0%

        \[\leadsto \frac{\frac{1}{s}}{{e}^{\left(\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
      6. add-sqr-sqrt100.0%

        \[\leadsto \frac{\frac{1}{s}}{{e}^{\left(\frac{\color{blue}{x}}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
    4. Applied egg-rr100.0%

      \[\leadsto \frac{\frac{1}{s}}{\color{blue}{{e}^{\left(\frac{x}{s}\right)}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
    5. Step-by-step derivation
      1. expm1-log1p-u100.0%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\frac{1}{s}}{{e}^{\left(\frac{x}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)\right)} \]
      2. expm1-udef100.0%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{{e}^{\left(\frac{x}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1} \]
      3. pow-to-exp100.0%

        \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{\color{blue}{e^{\log e \cdot \frac{x}{s}}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1 \]
      4. e-exp-1100.0%

        \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\log \color{blue}{\left(e^{1}\right)} \cdot \frac{x}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1 \]
      5. add-log-exp100.0%

        \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\color{blue}{1} \cdot \frac{x}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1 \]
      6. *-un-lft-identity100.0%

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

        \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \color{blue}{\left(2 + e^{\frac{\left|x\right|}{-s}}\right)}}\right)} - 1 \]
    6. Applied egg-rr100.0%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)}\right)} - 1} \]
    7. Step-by-step derivation
      1. expm1-def100.0%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)}\right)\right)} \]
      2. expm1-log1p100.0%

        \[\leadsto \color{blue}{\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)}} \]
      3. associate-/l/100.0%

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

        \[\leadsto \frac{1}{\color{blue}{s \cdot \left(e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)\right)}} \]
      5. associate-+r+100.0%

        \[\leadsto \frac{1}{s \cdot \color{blue}{\left(\left(e^{\frac{x}{s}} + 2\right) + e^{\frac{\left|x\right|}{-s}}\right)}} \]
    8. Simplified100.0%

      \[\leadsto \color{blue}{\frac{1}{s \cdot \left(\left(e^{\frac{x}{s}} + 2\right) + e^{\frac{\left|x\right|}{-s}}\right)}} \]
    9. Taylor expanded in x around 0 81.4%

      \[\leadsto \frac{1}{s \cdot \left(\color{blue}{\left(3 + \left(\frac{x}{s} + 0.5 \cdot \frac{{x}^{2}}{{s}^{2}}\right)\right)} + e^{\frac{\left|x\right|}{-s}}\right)} \]
    10. Step-by-step derivation
      1. unpow281.4%

        \[\leadsto \frac{1}{s \cdot \left(\left(3 + \left(\frac{x}{s} + 0.5 \cdot \frac{\color{blue}{x \cdot x}}{{s}^{2}}\right)\right) + e^{\frac{\left|x\right|}{-s}}\right)} \]
      2. unpow281.4%

        \[\leadsto \frac{1}{s \cdot \left(\left(3 + \left(\frac{x}{s} + 0.5 \cdot \frac{x \cdot x}{\color{blue}{s \cdot s}}\right)\right) + e^{\frac{\left|x\right|}{-s}}\right)} \]
      3. times-frac81.4%

        \[\leadsto \frac{1}{s \cdot \left(\left(3 + \left(\frac{x}{s} + 0.5 \cdot \color{blue}{\left(\frac{x}{s} \cdot \frac{x}{s}\right)}\right)\right) + e^{\frac{\left|x\right|}{-s}}\right)} \]
    11. Simplified81.4%

      \[\leadsto \frac{1}{s \cdot \left(\color{blue}{\left(3 + \left(\frac{x}{s} + 0.5 \cdot \left(\frac{x}{s} \cdot \frac{x}{s}\right)\right)\right)} + e^{\frac{\left|x\right|}{-s}}\right)} \]
    12. Taylor expanded in s around 0 66.5%

      \[\leadsto \frac{1}{\color{blue}{0.5 \cdot \frac{{x}^{2}}{s}}} \]
    13. Step-by-step derivation
      1. *-commutative66.5%

        \[\leadsto \frac{1}{\color{blue}{\frac{{x}^{2}}{s} \cdot 0.5}} \]
      2. unpow266.5%

        \[\leadsto \frac{1}{\frac{\color{blue}{x \cdot x}}{s} \cdot 0.5} \]
      3. associate-*r/66.5%

        \[\leadsto \frac{1}{\color{blue}{\left(x \cdot \frac{x}{s}\right)} \cdot 0.5} \]
      4. associate-*l*66.5%

        \[\leadsto \frac{1}{\color{blue}{x \cdot \left(\frac{x}{s} \cdot 0.5\right)}} \]
      5. *-commutative66.5%

        \[\leadsto \frac{1}{x \cdot \color{blue}{\left(0.5 \cdot \frac{x}{s}\right)}} \]
    14. Simplified66.5%

      \[\leadsto \frac{1}{\color{blue}{x \cdot \left(0.5 \cdot \frac{x}{s}\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification43.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.9999999494757503 \cdot 10^{-5}:\\ \;\;\;\;\frac{0.25 + \left(\frac{x}{s} \cdot \frac{x}{s}\right) \cdot -0.0625}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x \cdot \left(\frac{x}{s} \cdot 0.5\right)}\\ \end{array} \]

Alternative 6: 63.8% accurate, 55.7× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 1.9999999494757503 \cdot 10^{-5}:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x \cdot \left(\frac{x}{s} \cdot 0.5\right)}\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s)
 :precision binary32
 (if (<= x 1.9999999494757503e-5) (/ 0.25 s) (/ 1.0 (* x (* (/ x s) 0.5)))))
x = abs(x);
float code(float x, float s) {
	float tmp;
	if (x <= 1.9999999494757503e-5f) {
		tmp = 0.25f / s;
	} else {
		tmp = 1.0f / (x * ((x / s) * 0.5f));
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 1.9999999494757503e-5) then
        tmp = 0.25e0 / s
    else
        tmp = 1.0e0 / (x * ((x / s) * 0.5e0))
    end if
    code = tmp
end function
x = abs(x)
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(1.9999999494757503e-5))
		tmp = Float32(Float32(0.25) / s);
	else
		tmp = Float32(Float32(1.0) / Float32(x * Float32(Float32(x / s) * Float32(0.5))));
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(1.9999999494757503e-5))
		tmp = single(0.25) / s;
	else
		tmp = single(1.0) / (x * ((x / s) * single(0.5)));
	end
	tmp_2 = tmp;
end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.9999999494757503 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.25}{s}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot \left(\frac{x}{s} \cdot 0.5\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.99999995e-5

    1. Initial program 99.1%

      \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
    2. Step-by-step derivation
      1. associate-*l*99.1%

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

        \[\leadsto \frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\color{blue}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)} \]
      3. +-commutative99.1%

        \[\leadsto \frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \color{blue}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right)}\right)} \]
    3. Simplified99.1%

      \[\leadsto \color{blue}{\frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)}} \]
    4. Taylor expanded in s around inf 35.8%

      \[\leadsto \color{blue}{\frac{0.25}{s}} \]

    if 1.99999995e-5 < x

    1. Initial program 100.0%

      \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}} \]
    3. Step-by-step derivation
      1. *-un-lft-identity100.0%

        \[\leadsto \frac{\frac{1}{s}}{e^{\color{blue}{1 \cdot \frac{\left|x\right|}{s}}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
      2. exp-prod100.0%

        \[\leadsto \frac{\frac{1}{s}}{\color{blue}{{\left(e^{1}\right)}^{\left(\frac{\left|x\right|}{s}\right)}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
      3. exp-1-e100.0%

        \[\leadsto \frac{\frac{1}{s}}{{\color{blue}{e}}^{\left(\frac{\left|x\right|}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
      4. add-sqr-sqrt100.0%

        \[\leadsto \frac{\frac{1}{s}}{{e}^{\left(\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
      5. fabs-sqr100.0%

        \[\leadsto \frac{\frac{1}{s}}{{e}^{\left(\frac{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
      6. add-sqr-sqrt100.0%

        \[\leadsto \frac{\frac{1}{s}}{{e}^{\left(\frac{\color{blue}{x}}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
    4. Applied egg-rr100.0%

      \[\leadsto \frac{\frac{1}{s}}{\color{blue}{{e}^{\left(\frac{x}{s}\right)}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)} \]
    5. Step-by-step derivation
      1. expm1-log1p-u100.0%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\frac{1}{s}}{{e}^{\left(\frac{x}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)\right)} \]
      2. expm1-udef100.0%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{{e}^{\left(\frac{x}{s}\right)} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1} \]
      3. pow-to-exp100.0%

        \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{\color{blue}{e^{\log e \cdot \frac{x}{s}}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1 \]
      4. e-exp-1100.0%

        \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\log \color{blue}{\left(e^{1}\right)} \cdot \frac{x}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1 \]
      5. add-log-exp100.0%

        \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\color{blue}{1} \cdot \frac{x}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}\right)} - 1 \]
      6. *-un-lft-identity100.0%

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

        \[\leadsto e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \color{blue}{\left(2 + e^{\frac{\left|x\right|}{-s}}\right)}}\right)} - 1 \]
    6. Applied egg-rr100.0%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)}\right)} - 1} \]
    7. Step-by-step derivation
      1. expm1-def100.0%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)}\right)\right)} \]
      2. expm1-log1p100.0%

        \[\leadsto \color{blue}{\frac{\frac{1}{s}}{e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)}} \]
      3. associate-/l/100.0%

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

        \[\leadsto \frac{1}{\color{blue}{s \cdot \left(e^{\frac{x}{s}} + \left(2 + e^{\frac{\left|x\right|}{-s}}\right)\right)}} \]
      5. associate-+r+100.0%

        \[\leadsto \frac{1}{s \cdot \color{blue}{\left(\left(e^{\frac{x}{s}} + 2\right) + e^{\frac{\left|x\right|}{-s}}\right)}} \]
    8. Simplified100.0%

      \[\leadsto \color{blue}{\frac{1}{s \cdot \left(\left(e^{\frac{x}{s}} + 2\right) + e^{\frac{\left|x\right|}{-s}}\right)}} \]
    9. Taylor expanded in x around 0 81.4%

      \[\leadsto \frac{1}{s \cdot \left(\color{blue}{\left(3 + \left(\frac{x}{s} + 0.5 \cdot \frac{{x}^{2}}{{s}^{2}}\right)\right)} + e^{\frac{\left|x\right|}{-s}}\right)} \]
    10. Step-by-step derivation
      1. unpow281.4%

        \[\leadsto \frac{1}{s \cdot \left(\left(3 + \left(\frac{x}{s} + 0.5 \cdot \frac{\color{blue}{x \cdot x}}{{s}^{2}}\right)\right) + e^{\frac{\left|x\right|}{-s}}\right)} \]
      2. unpow281.4%

        \[\leadsto \frac{1}{s \cdot \left(\left(3 + \left(\frac{x}{s} + 0.5 \cdot \frac{x \cdot x}{\color{blue}{s \cdot s}}\right)\right) + e^{\frac{\left|x\right|}{-s}}\right)} \]
      3. times-frac81.4%

        \[\leadsto \frac{1}{s \cdot \left(\left(3 + \left(\frac{x}{s} + 0.5 \cdot \color{blue}{\left(\frac{x}{s} \cdot \frac{x}{s}\right)}\right)\right) + e^{\frac{\left|x\right|}{-s}}\right)} \]
    11. Simplified81.4%

      \[\leadsto \frac{1}{s \cdot \left(\color{blue}{\left(3 + \left(\frac{x}{s} + 0.5 \cdot \left(\frac{x}{s} \cdot \frac{x}{s}\right)\right)\right)} + e^{\frac{\left|x\right|}{-s}}\right)} \]
    12. Taylor expanded in s around 0 66.5%

      \[\leadsto \frac{1}{\color{blue}{0.5 \cdot \frac{{x}^{2}}{s}}} \]
    13. Step-by-step derivation
      1. *-commutative66.5%

        \[\leadsto \frac{1}{\color{blue}{\frac{{x}^{2}}{s} \cdot 0.5}} \]
      2. unpow266.5%

        \[\leadsto \frac{1}{\frac{\color{blue}{x \cdot x}}{s} \cdot 0.5} \]
      3. associate-*r/66.5%

        \[\leadsto \frac{1}{\color{blue}{\left(x \cdot \frac{x}{s}\right)} \cdot 0.5} \]
      4. associate-*l*66.5%

        \[\leadsto \frac{1}{\color{blue}{x \cdot \left(\frac{x}{s} \cdot 0.5\right)}} \]
      5. *-commutative66.5%

        \[\leadsto \frac{1}{x \cdot \color{blue}{\left(0.5 \cdot \frac{x}{s}\right)}} \]
    14. Simplified66.5%

      \[\leadsto \frac{1}{\color{blue}{x \cdot \left(0.5 \cdot \frac{x}{s}\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification43.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.9999999494757503 \cdot 10^{-5}:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x \cdot \left(\frac{x}{s} \cdot 0.5\right)}\\ \end{array} \]

Alternative 7: 27.1% accurate, 206.7× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{0.25}{s} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x s) :precision binary32 (/ 0.25 s))
x = abs(x);
float code(float x, float s) {
	return 0.25f / s;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 0.25e0 / s
end function
x = abs(x)
function code(x, s)
	return Float32(Float32(0.25) / s)
end
x = abs(x)
function tmp = code(x, s)
	tmp = single(0.25) / s;
end
\begin{array}{l}
x = |x|\\
\\
\frac{0.25}{s}
\end{array}
Derivation
  1. Initial program 99.3%

    \[\frac{e^{\frac{-\left|x\right|}{s}}}{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)} \]
  2. Step-by-step derivation
    1. associate-*l*99.3%

      \[\leadsto \frac{e^{\frac{-\left|x\right|}{s}}}{\color{blue}{s \cdot \left(\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)}} \]
    2. +-commutative99.3%

      \[\leadsto \frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\color{blue}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right)} \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)} \]
    3. +-commutative99.3%

      \[\leadsto \frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \color{blue}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right)}\right)} \]
  3. Simplified99.3%

    \[\leadsto \color{blue}{\frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(e^{\frac{-\left|x\right|}{s}} + 1\right)\right)}} \]
  4. Taylor expanded in s around inf 28.0%

    \[\leadsto \color{blue}{\frac{0.25}{s}} \]
  5. Final simplification28.0%

    \[\leadsto \frac{0.25}{s} \]

Reproduce

?
herbie shell --seed 2023224 
(FPCore (x s)
  :name "Logistic distribution"
  :precision binary32
  :pre (and (<= 0.0 s) (<= s 1.0651631))
  (/ (exp (/ (- (fabs x)) s)) (* (* s (+ 1.0 (exp (/ (- (fabs x)) s)))) (+ 1.0 (exp (/ (- (fabs x)) s))))))