Logistic distribution

Percentage Accurate: 99.5% → 99.3%
Time: 9.4s
Alternatives: 8
Speedup: 5.6×

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 8 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.3% accurate, 1.5× speedup?

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

\\
\frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{-s}} + \left(e^{\frac{\left|x\right|}{s}} + 2\right)}
\end{array}
Derivation
  1. Initial program 99.6%

    \[\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.8%

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

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

Alternative 2: 65.3% accurate, 5.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{x \cdot x}{s}\\ \mathbf{if}\;\left|x\right| \leq 10:\\ \;\;\;\;\frac{1}{\left(2 \cdot \frac{x}{\frac{s}{x}} + s \cdot 4\right) - t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{t_0}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (let* ((t_0 (/ (* x x) s)))
   (if (<= (fabs x) 10.0)
     (/ 1.0 (- (+ (* 2.0 (/ x (/ s x))) (* s 4.0)) t_0))
     (/ 1.0 t_0))))
float code(float x, float s) {
	float t_0 = (x * x) / s;
	float tmp;
	if (fabsf(x) <= 10.0f) {
		tmp = 1.0f / (((2.0f * (x / (s / x))) + (s * 4.0f)) - t_0);
	} else {
		tmp = 1.0f / t_0;
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: t_0
    real(4) :: tmp
    t_0 = (x * x) / s
    if (abs(x) <= 10.0e0) then
        tmp = 1.0e0 / (((2.0e0 * (x / (s / x))) + (s * 4.0e0)) - t_0)
    else
        tmp = 1.0e0 / t_0
    end if
    code = tmp
end function
function code(x, s)
	t_0 = Float32(Float32(x * x) / s)
	tmp = Float32(0.0)
	if (abs(x) <= Float32(10.0))
		tmp = Float32(Float32(1.0) / Float32(Float32(Float32(Float32(2.0) * Float32(x / Float32(s / x))) + Float32(s * Float32(4.0))) - t_0));
	else
		tmp = Float32(Float32(1.0) / t_0);
	end
	return tmp
end
function tmp_2 = code(x, s)
	t_0 = (x * x) / s;
	tmp = single(0.0);
	if (abs(x) <= single(10.0))
		tmp = single(1.0) / (((single(2.0) * (x / (s / x))) + (s * single(4.0))) - t_0);
	else
		tmp = single(1.0) / t_0;
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{x \cdot x}{s}\\
\mathbf{if}\;\left|x\right| \leq 10:\\
\;\;\;\;\frac{1}{\left(2 \cdot \frac{x}{\frac{s}{x}} + s \cdot 4\right) - t_0}\\

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


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

    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. *-lft-identity99.3%

        \[\leadsto \color{blue}{1 \cdot \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. associate-*r/99.3%

        \[\leadsto \color{blue}{\frac{1 \cdot 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)}} \]
      3. associate-/l*99.2%

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

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

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

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

        \[\leadsto \frac{1}{\color{blue}{\left(\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)} \cdot e^{\frac{\left|x\right|}{s}}} \]
      8. associate-*l*99.3%

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

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

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

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(2, \frac{x \cdot x}{s}, s \cdot 4\right) - \frac{x \cdot x}{s}}} \]
    6. Step-by-step derivation
      1. fma-udef55.6%

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

        \[\leadsto \frac{1}{\left(2 \cdot \color{blue}{\frac{x}{\frac{s}{x}}} + s \cdot 4\right) - \frac{x \cdot x}{s}} \]
    7. Applied egg-rr55.7%

      \[\leadsto \frac{1}{\color{blue}{\left(2 \cdot \frac{x}{\frac{s}{x}} + s \cdot 4\right)} - \frac{x \cdot x}{s}} \]

    if 10 < (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. Step-by-step derivation
      1. *-lft-identity100.0%

        \[\leadsto \color{blue}{1 \cdot \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. associate-*r/100.0%

        \[\leadsto \color{blue}{\frac{1 \cdot 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)}} \]
      3. associate-/l*100.0%

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{1}{\color{blue}{\frac{{x}^{2}}{s}}} \]
    7. Step-by-step derivation
      1. unpow285.7%

        \[\leadsto \frac{1}{\frac{\color{blue}{x \cdot x}}{s}} \]
    8. Simplified85.7%

      \[\leadsto \frac{1}{\color{blue}{\frac{x \cdot x}{s}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification70.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left|x\right| \leq 10:\\ \;\;\;\;\frac{1}{\left(2 \cdot \frac{x}{\frac{s}{x}} + s \cdot 4\right) - \frac{x \cdot x}{s}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x}{s}}\\ \end{array} \]

Alternative 3: 80.0% accurate, 5.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 4.0000000126843074 \cdot 10^{-30}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(s, 4, \frac{x \cdot x}{s}\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{e^{\frac{x}{s}} + 3}}{s}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= x 4.0000000126843074e-30)
   (/ 1.0 (fma s 4.0 (/ (* x x) s)))
   (/ (/ 1.0 (+ (exp (/ x s)) 3.0)) s)))
float code(float x, float s) {
	float tmp;
	if (x <= 4.0000000126843074e-30f) {
		tmp = 1.0f / fmaf(s, 4.0f, ((x * x) / s));
	} else {
		tmp = (1.0f / (expf((x / s)) + 3.0f)) / s;
	}
	return tmp;
}
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(4.0000000126843074e-30))
		tmp = Float32(Float32(1.0) / fma(s, Float32(4.0), Float32(Float32(x * x) / s)));
	else
		tmp = Float32(Float32(Float32(1.0) / Float32(exp(Float32(x / s)) + Float32(3.0))) / s);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.0000000126843074 \cdot 10^{-30}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(s, 4, \frac{x \cdot x}{s}\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{e^{\frac{x}{s}} + 3}}{s}\\


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

    1. Initial program 99.6%

      \[\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. *-lft-identity99.6%

        \[\leadsto \color{blue}{1 \cdot \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. associate-*r/99.6%

        \[\leadsto \color{blue}{\frac{1 \cdot 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)}} \]
      3. associate-/l*99.5%

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

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

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

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

        \[\leadsto \frac{1}{\color{blue}{\left(\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)} \cdot e^{\frac{\left|x\right|}{s}}} \]
      8. associate-*l*99.5%

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

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

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

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(2, \frac{x \cdot x}{s}, s \cdot 4\right) - \frac{x \cdot x}{s}}} \]
    6. Taylor expanded in x around 0 73.3%

      \[\leadsto \frac{1}{\color{blue}{4 \cdot s + \frac{{x}^{2}}{s}}} \]
    7. Step-by-step derivation
      1. *-commutative73.3%

        \[\leadsto \frac{1}{\color{blue}{s \cdot 4} + \frac{{x}^{2}}{s}} \]
      2. fma-def73.3%

        \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 4, \frac{{x}^{2}}{s}\right)}} \]
      3. unpow273.3%

        \[\leadsto \frac{1}{\mathsf{fma}\left(s, 4, \frac{\color{blue}{x \cdot x}}{s}\right)} \]
    8. Simplified73.3%

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

    if 4e-30 < x

    1. Initial program 99.7%

      \[\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.8%

      \[\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 97.7%

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

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

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

      \[\leadsto \color{blue}{\frac{1}{s \cdot \left(e^{\frac{\left|x\right|}{s}} + 3\right)}} \]
    7. Step-by-step derivation
      1. inv-pow97.7%

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

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

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

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

      \[\leadsto \color{blue}{{\left(e^{\frac{\left|x\right|}{s}} + 3\right)}^{-1} \cdot \frac{1}{s}} \]
    9. Step-by-step derivation
      1. un-div-inv97.7%

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

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

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

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

        \[\leadsto \frac{\frac{1}{e^{\frac{\color{blue}{x}}{s}} + 3}}{s} \]
    10. Applied egg-rr97.7%

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

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

Alternative 4: 99.5% accurate, 5.6× speedup?

\[\begin{array}{l} \\ \frac{1}{s \cdot \left(2 + 2 \cdot \cosh \left(\frac{x}{s}\right)\right)} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (* s (+ 2.0 (* 2.0 (cosh (/ x s)))))))
float code(float x, float s) {
	return 1.0f / (s * (2.0f + (2.0f * coshf((x / s)))));
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 1.0e0 / (s * (2.0e0 + (2.0e0 * cosh((x / s)))))
end function
function code(x, s)
	return Float32(Float32(1.0) / Float32(s * Float32(Float32(2.0) + Float32(Float32(2.0) * cosh(Float32(x / s))))))
end
function tmp = code(x, s)
	tmp = single(1.0) / (s * (single(2.0) + (single(2.0) * cosh((x / s)))));
end
\begin{array}{l}

\\
\frac{1}{s \cdot \left(2 + 2 \cdot \cosh \left(\frac{x}{s}\right)\right)}
\end{array}
Derivation
  1. Initial program 99.6%

    \[\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.8%

    \[\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 0 99.7%

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

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

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

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

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

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

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

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

      \[\leadsto \frac{1}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(s \cdot \left(2 + 2 \cdot \cosh \left(\frac{x}{s}\right)\right)\right)\right)}} \]
    2. expm1-log1p99.8%

      \[\leadsto \frac{1}{\color{blue}{s \cdot \left(2 + 2 \cdot \cosh \left(\frac{x}{s}\right)\right)}} \]
  9. Simplified99.8%

    \[\leadsto \frac{1}{\color{blue}{s \cdot \left(2 + 2 \cdot \cosh \left(\frac{x}{s}\right)\right)}} \]
  10. Final simplification99.8%

    \[\leadsto \frac{1}{s \cdot \left(2 + 2 \cdot \cosh \left(\frac{x}{s}\right)\right)} \]

Alternative 5: 65.3% accurate, 5.7× speedup?

\[\begin{array}{l} \\ \frac{1}{\mathsf{fma}\left(s, 4, \frac{x \cdot x}{s}\right)} \end{array} \]
(FPCore (x s) :precision binary32 (/ 1.0 (fma s 4.0 (/ (* x x) s))))
float code(float x, float s) {
	return 1.0f / fmaf(s, 4.0f, ((x * x) / s));
}
function code(x, s)
	return Float32(Float32(1.0) / fma(s, Float32(4.0), Float32(Float32(x * x) / s)))
end
\begin{array}{l}

\\
\frac{1}{\mathsf{fma}\left(s, 4, \frac{x \cdot x}{s}\right)}
\end{array}
Derivation
  1. Initial program 99.6%

    \[\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. *-lft-identity99.6%

      \[\leadsto \color{blue}{1 \cdot \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. associate-*r/99.6%

      \[\leadsto \color{blue}{\frac{1 \cdot 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)}} \]
    3. associate-/l*99.6%

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

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

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

      \[\leadsto \frac{1}{\color{blue}{\frac{\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}{1} \cdot e^{\frac{\left|x\right|}{s}}}} \]
    7. /-rgt-identity99.6%

      \[\leadsto \frac{1}{\color{blue}{\left(\left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)} \cdot e^{\frac{\left|x\right|}{s}}} \]
    8. associate-*l*99.7%

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

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

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

    \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(2, \frac{x \cdot x}{s}, s \cdot 4\right) - \frac{x \cdot x}{s}}} \]
  6. Taylor expanded in x around 0 70.2%

    \[\leadsto \frac{1}{\color{blue}{4 \cdot s + \frac{{x}^{2}}{s}}} \]
  7. Step-by-step derivation
    1. *-commutative70.2%

      \[\leadsto \frac{1}{\color{blue}{s \cdot 4} + \frac{{x}^{2}}{s}} \]
    2. fma-def70.2%

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 4, \frac{{x}^{2}}{s}\right)}} \]
    3. unpow270.2%

      \[\leadsto \frac{1}{\mathsf{fma}\left(s, 4, \frac{\color{blue}{x \cdot x}}{s}\right)} \]
  8. Simplified70.2%

    \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(s, 4, \frac{x \cdot x}{s}\right)}} \]
  9. Final simplification70.2%

    \[\leadsto \frac{1}{\mathsf{fma}\left(s, 4, \frac{x \cdot x}{s}\right)} \]

Alternative 6: 64.0% accurate, 55.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7} \lor \neg \left(x \leq 1.999999987845058 \cdot 10^{-8}\right):\\ \;\;\;\;\frac{1}{\frac{x \cdot x}{s}}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.25}{s}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (or (<= x -2.0000000233721948e-7) (not (<= x 1.999999987845058e-8)))
   (/ 1.0 (/ (* x x) s))
   (/ 0.25 s)))
float code(float x, float s) {
	float tmp;
	if ((x <= -2.0000000233721948e-7f) || !(x <= 1.999999987845058e-8f)) {
		tmp = 1.0f / ((x * x) / s);
	} else {
		tmp = 0.25f / s;
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if ((x <= (-2.0000000233721948e-7)) .or. (.not. (x <= 1.999999987845058e-8))) then
        tmp = 1.0e0 / ((x * x) / s)
    else
        tmp = 0.25e0 / s
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if ((x <= Float32(-2.0000000233721948e-7)) || !(x <= Float32(1.999999987845058e-8)))
		tmp = Float32(Float32(1.0) / Float32(Float32(x * x) / s));
	else
		tmp = Float32(Float32(0.25) / s);
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if ((x <= single(-2.0000000233721948e-7)) || ~((x <= single(1.999999987845058e-8))))
		tmp = single(1.0) / ((x * x) / s);
	else
		tmp = single(0.25) / s;
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7} \lor \neg \left(x \leq 1.999999987845058 \cdot 10^{-8}\right):\\
\;\;\;\;\frac{1}{\frac{x \cdot x}{s}}\\

\mathbf{else}:\\
\;\;\;\;\frac{0.25}{s}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -2.00000002e-7 or 1.99999999e-8 < x

    1. Initial program 99.9%

      \[\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. *-lft-identity99.9%

        \[\leadsto \color{blue}{1 \cdot \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. associate-*r/99.9%

        \[\leadsto \color{blue}{\frac{1 \cdot 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)}} \]
      3. associate-/l*100.0%

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{1}{\color{blue}{\frac{{x}^{2}}{s}}} \]
    7. Step-by-step derivation
      1. unpow275.2%

        \[\leadsto \frac{1}{\frac{\color{blue}{x \cdot x}}{s}} \]
    8. Simplified75.2%

      \[\leadsto \frac{1}{\color{blue}{\frac{x \cdot x}{s}}} \]

    if -2.00000002e-7 < x < 1.99999999e-8

    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.5%

      \[\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 62.0%

      \[\leadsto \color{blue}{\frac{0.25}{s}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification69.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7} \lor \neg \left(x \leq 1.999999987845058 \cdot 10^{-8}\right):\\ \;\;\;\;\frac{1}{\frac{x \cdot x}{s}}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.25}{s}\\ \end{array} \]

Alternative 7: 62.4% accurate, 66.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7} \lor \neg \left(x \leq 1.999999987845058 \cdot 10^{-8}\right):\\ \;\;\;\;\frac{s}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.25}{s}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (or (<= x -2.0000000233721948e-7) (not (<= x 1.999999987845058e-8)))
   (/ s (* x x))
   (/ 0.25 s)))
float code(float x, float s) {
	float tmp;
	if ((x <= -2.0000000233721948e-7f) || !(x <= 1.999999987845058e-8f)) {
		tmp = s / (x * x);
	} else {
		tmp = 0.25f / s;
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if ((x <= (-2.0000000233721948e-7)) .or. (.not. (x <= 1.999999987845058e-8))) then
        tmp = s / (x * x)
    else
        tmp = 0.25e0 / s
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if ((x <= Float32(-2.0000000233721948e-7)) || !(x <= Float32(1.999999987845058e-8)))
		tmp = Float32(s / Float32(x * x));
	else
		tmp = Float32(Float32(0.25) / s);
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if ((x <= single(-2.0000000233721948e-7)) || ~((x <= single(1.999999987845058e-8))))
		tmp = s / (x * x);
	else
		tmp = single(0.25) / s;
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7} \lor \neg \left(x \leq 1.999999987845058 \cdot 10^{-8}\right):\\
\;\;\;\;\frac{s}{x \cdot x}\\

\mathbf{else}:\\
\;\;\;\;\frac{0.25}{s}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -2.00000002e-7 or 1.99999999e-8 < x

    1. Initial program 99.9%

      \[\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. *-lft-identity99.9%

        \[\leadsto \color{blue}{1 \cdot \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. associate-*r/99.9%

        \[\leadsto \color{blue}{\frac{1 \cdot 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)}} \]
      3. associate-/l*100.0%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\frac{s}{{x}^{2}}} \]
    7. Step-by-step derivation
      1. unpow273.4%

        \[\leadsto \frac{s}{\color{blue}{x \cdot x}} \]
    8. Simplified73.4%

      \[\leadsto \color{blue}{\frac{s}{x \cdot x}} \]

    if -2.00000002e-7 < x < 1.99999999e-8

    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.5%

      \[\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 62.0%

      \[\leadsto \color{blue}{\frac{0.25}{s}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification68.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7} \lor \neg \left(x \leq 1.999999987845058 \cdot 10^{-8}\right):\\ \;\;\;\;\frac{s}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.25}{s}\\ \end{array} \]

Alternative 8: 26.5% accurate, 206.7× speedup?

\[\begin{array}{l} \\ \frac{0.25}{s} \end{array} \]
(FPCore (x s) :precision binary32 (/ 0.25 s))
float code(float x, float s) {
	return 0.25f / s;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    code = 0.25e0 / s
end function
function code(x, s)
	return Float32(Float32(0.25) / s)
end
function tmp = code(x, s)
	tmp = single(0.25) / s;
end
\begin{array}{l}

\\
\frac{0.25}{s}
\end{array}
Derivation
  1. Initial program 99.6%

    \[\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.8%

    \[\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 29.9%

    \[\leadsto \color{blue}{\frac{0.25}{s}} \]
  4. Final simplification29.9%

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

Reproduce

?
herbie shell --seed 2023194 
(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))))))