Logistic distribution

Percentage Accurate: 99.5% → 99.5%
Time: 13.9s
Alternatives: 10
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 10 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, 1.9× speedup?

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

\\
\begin{array}{l}
t_0 := e^{\frac{x\_m}{-s}}\\
\frac{\frac{t\_0}{t\_0 + 1}}{s + \frac{s}{e^{\frac{x\_m}{s}}}}
\end{array}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\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. *-commutative99.4%

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{e^{\frac{x}{s}}}}} \]
  8. Add Preprocessing

Alternative 2: 99.5% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
t_0 := e^{\frac{x\_m}{-s}}\\
\frac{\frac{t\_0}{s}}{{\left(t\_0 + 1\right)}^{2}}
\end{array}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\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. *-commutative99.4%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{\frac{e^{\frac{x}{-s}}}{s}}{{\left(e^{\frac{x}{-s}} + 1\right)}^{2}}} \]
  8. Add Preprocessing

Alternative 3: 97.1% accurate, 2.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \frac{{\left(e^{{\left(\frac{x\_m}{s}\right)}^{2}}\right)}^{-0.25}}{s \cdot 4} \end{array} \]
x_m = (fabs.f32 x)
(FPCore (x_m s)
 :precision binary32
 (/ (pow (exp (pow (/ x_m s) 2.0)) -0.25) (* s 4.0)))
x_m = fabs(x);
float code(float x_m, float s) {
	return powf(expf(powf((x_m / s), 2.0f)), -0.25f) / (s * 4.0f);
}
x_m = abs(x)
real(4) function code(x_m, s)
    real(4), intent (in) :: x_m
    real(4), intent (in) :: s
    code = (exp(((x_m / s) ** 2.0e0)) ** (-0.25e0)) / (s * 4.0e0)
end function
x_m = abs(x)
function code(x_m, s)
	return Float32((exp((Float32(x_m / s) ^ Float32(2.0))) ^ Float32(-0.25)) / Float32(s * Float32(4.0)))
end
x_m = abs(x);
function tmp = code(x_m, s)
	tmp = (exp(((x_m / s) ^ single(2.0))) ^ single(-0.25)) / (s * single(4.0));
end
\begin{array}{l}
x_m = \left|x\right|

\\
\frac{{\left(e^{{\left(\frac{x\_m}{s}\right)}^{2}}\right)}^{-0.25}}{s \cdot 4}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\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. *-commutative99.4%

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

    \[\leadsto \color{blue}{\frac{e^{\frac{-\left|x\right|}{s}}}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)}} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. associate-/r*99.5%

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

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

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

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

      \[\leadsto \frac{\frac{e^{\frac{-\left|x\right|}{s}}}{1 + e^{\frac{-\left|x\right|}{s}}}}{\color{blue}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}} \]
    6. add-exp-log97.8%

      \[\leadsto \color{blue}{e^{\log \left(\frac{\frac{e^{\frac{-\left|x\right|}{s}}}{1 + e^{\frac{-\left|x\right|}{s}}}}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}\right)}} \]
    7. associate-/r*97.8%

      \[\leadsto e^{\log \color{blue}{\left(\frac{e^{\frac{-\left|x\right|}{s}}}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}\right)}} \]
  6. Applied egg-rr85.1%

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

    \[\leadsto e^{\color{blue}{-0.25 \cdot \frac{{x}^{2}}{{s}^{2}} - \log \left(4 \cdot s\right)}} \]
  8. Step-by-step derivation
    1. unpow287.2%

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

      \[\leadsto e^{-0.25 \cdot \frac{x \cdot x}{\color{blue}{s \cdot s}} - \log \left(4 \cdot s\right)} \]
    3. times-frac95.0%

      \[\leadsto e^{-0.25 \cdot \color{blue}{\left(\frac{x}{s} \cdot \frac{x}{s}\right)} - \log \left(4 \cdot s\right)} \]
    4. unpow295.0%

      \[\leadsto e^{-0.25 \cdot \color{blue}{{\left(\frac{x}{s}\right)}^{2}} - \log \left(4 \cdot s\right)} \]
    5. *-commutative95.0%

      \[\leadsto e^{-0.25 \cdot {\left(\frac{x}{s}\right)}^{2} - \log \color{blue}{\left(s \cdot 4\right)}} \]
  9. Simplified95.0%

    \[\leadsto e^{\color{blue}{-0.25 \cdot {\left(\frac{x}{s}\right)}^{2} - \log \left(s \cdot 4\right)}} \]
  10. Step-by-step derivation
    1. exp-diff95.1%

      \[\leadsto \color{blue}{\frac{e^{-0.25 \cdot {\left(\frac{x}{s}\right)}^{2}}}{e^{\log \left(s \cdot 4\right)}}} \]
    2. *-commutative95.1%

      \[\leadsto \frac{e^{\color{blue}{{\left(\frac{x}{s}\right)}^{2} \cdot -0.25}}}{e^{\log \left(s \cdot 4\right)}} \]
    3. exp-prod95.1%

      \[\leadsto \frac{\color{blue}{{\left(e^{{\left(\frac{x}{s}\right)}^{2}}\right)}^{-0.25}}}{e^{\log \left(s \cdot 4\right)}} \]
    4. add-exp-log96.6%

      \[\leadsto \frac{{\left(e^{{\left(\frac{x}{s}\right)}^{2}}\right)}^{-0.25}}{\color{blue}{s \cdot 4}} \]
  11. Applied egg-rr96.6%

    \[\leadsto \color{blue}{\frac{{\left(e^{{\left(\frac{x}{s}\right)}^{2}}\right)}^{-0.25}}{s \cdot 4}} \]
  12. Add Preprocessing

Alternative 4: 96.8% accurate, 2.8× speedup?

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

\\
\begin{array}{l}
t_0 := e^{\frac{x\_m}{-s}}\\
\frac{\frac{t\_0}{t\_0 + 1}}{s + \frac{s}{1 + \frac{x\_m}{s}}}
\end{array}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\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. *-commutative99.4%

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{e^{\frac{x}{s}}}}} \]
  8. Taylor expanded in x around 0 56.3%

    \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{\color{blue}{1 + \frac{x}{s}}}} \]
  9. Step-by-step derivation
    1. +-commutative56.3%

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

    \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{\color{blue}{\frac{x}{s} + 1}}} \]
  11. Final simplification56.3%

    \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{1 + \frac{x}{s}}} \]
  12. Add Preprocessing

Alternative 5: 95.5% accurate, 2.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ e^{-0.25 \cdot \left(\frac{x\_m}{s} \cdot \frac{x\_m}{s}\right) - \log \left(s \cdot 4\right)} \end{array} \]
x_m = (fabs.f32 x)
(FPCore (x_m s)
 :precision binary32
 (exp (- (* -0.25 (* (/ x_m s) (/ x_m s))) (log (* s 4.0)))))
x_m = fabs(x);
float code(float x_m, float s) {
	return expf(((-0.25f * ((x_m / s) * (x_m / s))) - logf((s * 4.0f))));
}
x_m = abs(x)
real(4) function code(x_m, s)
    real(4), intent (in) :: x_m
    real(4), intent (in) :: s
    code = exp((((-0.25e0) * ((x_m / s) * (x_m / s))) - log((s * 4.0e0))))
end function
x_m = abs(x)
function code(x_m, s)
	return exp(Float32(Float32(Float32(-0.25) * Float32(Float32(x_m / s) * Float32(x_m / s))) - log(Float32(s * Float32(4.0)))))
end
x_m = abs(x);
function tmp = code(x_m, s)
	tmp = exp(((single(-0.25) * ((x_m / s) * (x_m / s))) - log((s * single(4.0)))));
end
\begin{array}{l}
x_m = \left|x\right|

\\
e^{-0.25 \cdot \left(\frac{x\_m}{s} \cdot \frac{x\_m}{s}\right) - \log \left(s \cdot 4\right)}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\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. *-commutative99.4%

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

    \[\leadsto \color{blue}{\frac{e^{\frac{-\left|x\right|}{s}}}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)}} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. associate-/r*99.5%

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

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

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

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

      \[\leadsto \frac{\frac{e^{\frac{-\left|x\right|}{s}}}{1 + e^{\frac{-\left|x\right|}{s}}}}{\color{blue}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}} \]
    6. add-exp-log97.8%

      \[\leadsto \color{blue}{e^{\log \left(\frac{\frac{e^{\frac{-\left|x\right|}{s}}}{1 + e^{\frac{-\left|x\right|}{s}}}}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}\right)}} \]
    7. associate-/r*97.8%

      \[\leadsto e^{\log \color{blue}{\left(\frac{e^{\frac{-\left|x\right|}{s}}}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}\right)}} \]
  6. Applied egg-rr85.1%

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

    \[\leadsto e^{\color{blue}{-0.25 \cdot \frac{{x}^{2}}{{s}^{2}} - \log \left(4 \cdot s\right)}} \]
  8. Step-by-step derivation
    1. unpow287.2%

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

      \[\leadsto e^{-0.25 \cdot \frac{x \cdot x}{\color{blue}{s \cdot s}} - \log \left(4 \cdot s\right)} \]
    3. times-frac95.0%

      \[\leadsto e^{-0.25 \cdot \color{blue}{\left(\frac{x}{s} \cdot \frac{x}{s}\right)} - \log \left(4 \cdot s\right)} \]
    4. unpow295.0%

      \[\leadsto e^{-0.25 \cdot \color{blue}{{\left(\frac{x}{s}\right)}^{2}} - \log \left(4 \cdot s\right)} \]
    5. *-commutative95.0%

      \[\leadsto e^{-0.25 \cdot {\left(\frac{x}{s}\right)}^{2} - \log \color{blue}{\left(s \cdot 4\right)}} \]
  9. Simplified95.0%

    \[\leadsto e^{\color{blue}{-0.25 \cdot {\left(\frac{x}{s}\right)}^{2} - \log \left(s \cdot 4\right)}} \]
  10. Step-by-step derivation
    1. unpow295.0%

      \[\leadsto e^{-0.25 \cdot \color{blue}{\left(\frac{x}{s} \cdot \frac{x}{s}\right)} - \log \left(s \cdot 4\right)} \]
  11. Applied egg-rr95.0%

    \[\leadsto e^{-0.25 \cdot \color{blue}{\left(\frac{x}{s} \cdot \frac{x}{s}\right)} - \log \left(s \cdot 4\right)} \]
  12. Add Preprocessing

Alternative 6: 94.9% accurate, 5.3× speedup?

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

\\
\frac{\frac{e^{\frac{x\_m}{-s}}}{2}}{s + \frac{s}{1 + \frac{x\_m}{s}}}
\end{array}
Derivation
  1. Initial program 99.4%

    \[\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. *-commutative99.4%

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{e^{\frac{x}{s}}}}} \]
  8. Taylor expanded in x around 0 56.3%

    \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{\color{blue}{1 + \frac{x}{s}}}} \]
  9. Step-by-step derivation
    1. +-commutative56.3%

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

    \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{\color{blue}{\frac{x}{s} + 1}}} \]
  11. Taylor expanded in x around 0 55.9%

    \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{\color{blue}{1} + 1}}{s + \frac{s}{\frac{x}{s} + 1}} \]
  12. Final simplification55.9%

    \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{2}}{s + \frac{s}{1 + \frac{x}{s}}} \]
  13. Add Preprocessing

Alternative 7: 87.5% accurate, 5.5× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 1.2000000326942414 \cdot 10^{-26}:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{\frac{x\_m}{-s}} \cdot 0.5}{s}\\ \end{array} \end{array} \]
x_m = (fabs.f32 x)
(FPCore (x_m s)
 :precision binary32
 (if (<= x_m 1.2000000326942414e-26)
   (/ 0.25 s)
   (/ (* (exp (/ x_m (- s))) 0.5) s)))
x_m = fabs(x);
float code(float x_m, float s) {
	float tmp;
	if (x_m <= 1.2000000326942414e-26f) {
		tmp = 0.25f / s;
	} else {
		tmp = (expf((x_m / -s)) * 0.5f) / s;
	}
	return tmp;
}
x_m = abs(x)
real(4) function code(x_m, s)
    real(4), intent (in) :: x_m
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x_m <= 1.2000000326942414e-26) then
        tmp = 0.25e0 / s
    else
        tmp = (exp((x_m / -s)) * 0.5e0) / s
    end if
    code = tmp
end function
x_m = abs(x)
function code(x_m, s)
	tmp = Float32(0.0)
	if (x_m <= Float32(1.2000000326942414e-26))
		tmp = Float32(Float32(0.25) / s);
	else
		tmp = Float32(Float32(exp(Float32(x_m / Float32(-s))) * Float32(0.5)) / s);
	end
	return tmp
end
x_m = abs(x);
function tmp_2 = code(x_m, s)
	tmp = single(0.0);
	if (x_m <= single(1.2000000326942414e-26))
		tmp = single(0.25) / s;
	else
		tmp = (exp((x_m / -s)) * single(0.5)) / s;
	end
	tmp_2 = tmp;
end
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 1.2000000326942414 \cdot 10^{-26}:\\
\;\;\;\;\frac{0.25}{s}\\

\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{x\_m}{-s}} \cdot 0.5}{s}\\


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

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

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

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

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

    if 1.20000003e-26 < x

    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. *-commutative99.6%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{e^{\frac{x}{s}}}}} \]
    8. Taylor expanded in x around 0 97.1%

      \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{\color{blue}{1 + \frac{x}{s}}}} \]
    9. Step-by-step derivation
      1. +-commutative97.1%

        \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{\color{blue}{\frac{x}{s} + 1}}} \]
    10. Simplified97.1%

      \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{e^{\frac{x}{-s}} + 1}}{s + \frac{s}{\color{blue}{\frac{x}{s} + 1}}} \]
    11. Taylor expanded in x around 0 95.6%

      \[\leadsto \frac{\frac{e^{\frac{x}{-s}}}{\color{blue}{1} + 1}}{s + \frac{s}{\frac{x}{s} + 1}} \]
    12. Taylor expanded in x around inf 88.8%

      \[\leadsto \color{blue}{0.5 \cdot \frac{e^{-1 \cdot \frac{x}{s}}}{s}} \]
    13. Step-by-step derivation
      1. associate-*r/88.8%

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

        \[\leadsto \frac{0.5 \cdot e^{\color{blue}{\frac{-1 \cdot x}{s}}}}{s} \]
      3. mul-1-neg88.8%

        \[\leadsto \frac{0.5 \cdot e^{\frac{\color{blue}{-x}}{s}}}{s} \]
    14. Simplified88.8%

      \[\leadsto \color{blue}{\frac{0.5 \cdot e^{\frac{-x}{s}}}{s}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification54.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.2000000326942414 \cdot 10^{-26}:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{\frac{x}{-s}} \cdot 0.5}{s}\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 74.4% accurate, 5.6× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 1.2000000326942414 \cdot 10^{-26}:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;1 + \mathsf{fma}\left(s, 4, -1\right)\\ \end{array} \end{array} \]
x_m = (fabs.f32 x)
(FPCore (x_m s)
 :precision binary32
 (if (<= x_m 1.2000000326942414e-26) (/ 0.25 s) (+ 1.0 (fma s 4.0 -1.0))))
x_m = fabs(x);
float code(float x_m, float s) {
	float tmp;
	if (x_m <= 1.2000000326942414e-26f) {
		tmp = 0.25f / s;
	} else {
		tmp = 1.0f + fmaf(s, 4.0f, -1.0f);
	}
	return tmp;
}
x_m = abs(x)
function code(x_m, s)
	tmp = Float32(0.0)
	if (x_m <= Float32(1.2000000326942414e-26))
		tmp = Float32(Float32(0.25) / s);
	else
		tmp = Float32(Float32(1.0) + fma(s, Float32(4.0), Float32(-1.0)));
	end
	return tmp
end
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 1.2000000326942414 \cdot 10^{-26}:\\
\;\;\;\;\frac{0.25}{s}\\

\mathbf{else}:\\
\;\;\;\;1 + \mathsf{fma}\left(s, 4, -1\right)\\


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

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

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

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

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

    if 1.20000003e-26 < x

    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. *-commutative99.6%

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

      \[\leadsto \color{blue}{\frac{e^{\frac{-\left|x\right|}{s}}}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. associate-/r*99.7%

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

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

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

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

        \[\leadsto \frac{\frac{e^{\frac{-\left|x\right|}{s}}}{1 + e^{\frac{-\left|x\right|}{s}}}}{\color{blue}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}} \]
      6. add-exp-log99.0%

        \[\leadsto \color{blue}{e^{\log \left(\frac{\frac{e^{\frac{-\left|x\right|}{s}}}{1 + e^{\frac{-\left|x\right|}{s}}}}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}\right)}} \]
      7. associate-/r*99.0%

        \[\leadsto e^{\log \color{blue}{\left(\frac{e^{\frac{-\left|x\right|}{s}}}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}\right)}} \]
    6. Applied egg-rr66.9%

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

      \[\leadsto \color{blue}{e^{-\log \left(4 \cdot s\right)}} \]
    8. Step-by-step derivation
      1. *-commutative15.4%

        \[\leadsto e^{-\log \color{blue}{\left(s \cdot 4\right)}} \]
    9. Simplified15.4%

      \[\leadsto \color{blue}{e^{-\log \left(s \cdot 4\right)}} \]
    10. Step-by-step derivation
      1. add-sqr-sqrt11.1%

        \[\leadsto e^{\color{blue}{\sqrt{-\log \left(s \cdot 4\right)} \cdot \sqrt{-\log \left(s \cdot 4\right)}}} \]
      2. sqrt-unprod12.2%

        \[\leadsto e^{\color{blue}{\sqrt{\left(-\log \left(s \cdot 4\right)\right) \cdot \left(-\log \left(s \cdot 4\right)\right)}}} \]
      3. sqr-neg12.2%

        \[\leadsto e^{\sqrt{\color{blue}{\log \left(s \cdot 4\right) \cdot \log \left(s \cdot 4\right)}}} \]
      4. sqrt-unprod1.1%

        \[\leadsto e^{\color{blue}{\sqrt{\log \left(s \cdot 4\right)} \cdot \sqrt{\log \left(s \cdot 4\right)}}} \]
      5. add-sqr-sqrt11.1%

        \[\leadsto e^{\color{blue}{\log \left(s \cdot 4\right)}} \]
      6. expm1-log1p-u11.1%

        \[\leadsto e^{\log \color{blue}{\left(\mathsf{expm1}\left(\mathsf{log1p}\left(s \cdot 4\right)\right)\right)}} \]
      7. add-exp-log11.1%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(s \cdot 4\right)\right)} \]
      8. expm1-undefine72.7%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(s \cdot 4\right)} - 1} \]
    11. Applied egg-rr72.7%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(s \cdot 4\right)} - 1} \]
    12. Step-by-step derivation
      1. log1p-undefine72.7%

        \[\leadsto e^{\color{blue}{\log \left(1 + s \cdot 4\right)}} - 1 \]
      2. rem-exp-log72.7%

        \[\leadsto \color{blue}{\left(1 + s \cdot 4\right)} - 1 \]
      3. associate-+r-72.7%

        \[\leadsto \color{blue}{1 + \left(s \cdot 4 - 1\right)} \]
      4. fma-neg72.7%

        \[\leadsto 1 + \color{blue}{\mathsf{fma}\left(s, 4, -1\right)} \]
      5. metadata-eval72.7%

        \[\leadsto 1 + \mathsf{fma}\left(s, 4, \color{blue}{-1}\right) \]
    13. Simplified72.7%

      \[\leadsto \color{blue}{1 + \mathsf{fma}\left(s, 4, -1\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 9: 27.0% accurate, 206.7× speedup?

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

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

    \[\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. *-commutative99.4%

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

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

    \[\leadsto \color{blue}{\frac{0.25}{s}} \]
  6. Add Preprocessing

Alternative 10: 11.0% accurate, 206.7× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ s \cdot 4 \end{array} \]
x_m = (fabs.f32 x)
(FPCore (x_m s) :precision binary32 (* s 4.0))
x_m = fabs(x);
float code(float x_m, float s) {
	return s * 4.0f;
}
x_m = abs(x)
real(4) function code(x_m, s)
    real(4), intent (in) :: x_m
    real(4), intent (in) :: s
    code = s * 4.0e0
end function
x_m = abs(x)
function code(x_m, s)
	return Float32(s * Float32(4.0))
end
x_m = abs(x);
function tmp = code(x_m, s)
	tmp = s * single(4.0);
end
\begin{array}{l}
x_m = \left|x\right|

\\
s \cdot 4
\end{array}
Derivation
  1. Initial program 99.4%

    \[\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. *-commutative99.4%

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

    \[\leadsto \color{blue}{\frac{e^{\frac{-\left|x\right|}{s}}}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(s \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)}} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. associate-/r*99.5%

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

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

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

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

      \[\leadsto \frac{\frac{e^{\frac{-\left|x\right|}{s}}}{1 + e^{\frac{-\left|x\right|}{s}}}}{\color{blue}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}} \]
    6. add-exp-log97.8%

      \[\leadsto \color{blue}{e^{\log \left(\frac{\frac{e^{\frac{-\left|x\right|}{s}}}{1 + e^{\frac{-\left|x\right|}{s}}}}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}\right)}} \]
    7. associate-/r*97.8%

      \[\leadsto e^{\log \color{blue}{\left(\frac{e^{\frac{-\left|x\right|}{s}}}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right)}\right)}} \]
  6. Applied egg-rr85.1%

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

    \[\leadsto \color{blue}{e^{-\log \left(4 \cdot s\right)}} \]
  8. Step-by-step derivation
    1. *-commutative25.3%

      \[\leadsto e^{-\log \color{blue}{\left(s \cdot 4\right)}} \]
  9. Simplified25.3%

    \[\leadsto \color{blue}{e^{-\log \left(s \cdot 4\right)}} \]
  10. Step-by-step derivation
    1. add-sqr-sqrt22.8%

      \[\leadsto e^{\color{blue}{\sqrt{-\log \left(s \cdot 4\right)} \cdot \sqrt{-\log \left(s \cdot 4\right)}}} \]
    2. sqrt-unprod23.9%

      \[\leadsto e^{\color{blue}{\sqrt{\left(-\log \left(s \cdot 4\right)\right) \cdot \left(-\log \left(s \cdot 4\right)\right)}}} \]
    3. sqr-neg23.9%

      \[\leadsto e^{\sqrt{\color{blue}{\log \left(s \cdot 4\right) \cdot \log \left(s \cdot 4\right)}}} \]
    4. sqrt-unprod0.5%

      \[\leadsto e^{\color{blue}{\sqrt{\log \left(s \cdot 4\right)} \cdot \sqrt{\log \left(s \cdot 4\right)}}} \]
    5. add-sqr-sqrt10.9%

      \[\leadsto e^{\color{blue}{\log \left(s \cdot 4\right)}} \]
    6. add-exp-log10.9%

      \[\leadsto \color{blue}{s \cdot 4} \]
    7. *-un-lft-identity10.9%

      \[\leadsto \color{blue}{1 \cdot \left(s \cdot 4\right)} \]
  11. Applied egg-rr10.9%

    \[\leadsto \color{blue}{1 \cdot \left(s \cdot 4\right)} \]
  12. Taylor expanded in s around 0 10.9%

    \[\leadsto \color{blue}{4 \cdot s} \]
  13. Step-by-step derivation
    1. *-commutative10.9%

      \[\leadsto \color{blue}{s \cdot 4} \]
  14. Simplified10.9%

    \[\leadsto \color{blue}{s \cdot 4} \]
  15. Add Preprocessing

Reproduce

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