Logistic distribution

Percentage Accurate: 99.5% → 99.5%
Time: 14.6s
Alternatives: 5
Speedup: 2.9×

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 5 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.5× speedup?

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

\\
\frac{1}{\left(1 + e^{-\frac{x_m}{s}}\right) \cdot \left(s \cdot \left(1 + {\left(\sqrt{e^{\frac{x_m}{s}}}\right)}^{2}\right)\right)}
\end{array}
Derivation
  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.7%

    \[\leadsto \color{blue}{\frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{\left|x\right|}{s}}, s\right)}} \]
  3. Add Preprocessing
  4. Step-by-step derivation
    1. frac-2neg99.7%

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

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\color{blue}{\frac{\left|x\right|}{s}}}, s\right)} \]
    3. add-sqr-sqrt99.7%

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

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

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

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

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{\color{blue}{-\left|x\right|}}{s}}, s\right)} \]
    8. fma-udef23.7%

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

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

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

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

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

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

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(e^{\log s} \cdot \color{blue}{e^{\log \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}}\right)} \]
    15. log1p-udef22.8%

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(e^{\log s} \cdot e^{\color{blue}{\mathsf{log1p}\left(e^{\frac{-\left|x\right|}{s}}\right)}}\right)} \]
    16. prod-exp22.6%

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

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

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

    \[\leadsto \frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \color{blue}{e^{\mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s}}} \]
  8. Step-by-step derivation
    1. distribute-frac-neg62.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{1}{\left(1 + \frac{1}{e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}\right) \cdot e^{\mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s}} \]
    16. fabs-sqr52.1%

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

      \[\leadsto \frac{1}{\left(1 + \frac{1}{e^{\frac{\color{blue}{x}}{s}}}\right) \cdot e^{\mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s}} \]
  9. Applied egg-rr98.0%

    \[\leadsto \frac{1}{\left(1 + \color{blue}{\frac{1}{e^{\frac{x}{s}}}}\right) \cdot e^{\mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s}} \]
  10. Step-by-step derivation
    1. rec-exp98.0%

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

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

    \[\leadsto \frac{1}{\left(1 + \color{blue}{e^{\frac{-x}{s}}}\right) \cdot e^{\mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s}} \]
  12. Step-by-step derivation
    1. exp-sum98.4%

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

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

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

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(\color{blue}{\left(e^{\frac{x}{s}} + 1\right)} \cdot e^{\log s}\right)} \]
    5. add-exp-log99.8%

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

    \[\leadsto \frac{1}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \color{blue}{\left(\left(e^{\frac{x}{s}} + 1\right) \cdot s\right)}} \]
  14. Step-by-step derivation
    1. add-sqr-sqrt99.8%

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

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(\left(\color{blue}{{\left(\sqrt{e^{\frac{x}{s}}}\right)}^{2}} + 1\right) \cdot s\right)} \]
  15. Applied egg-rr99.8%

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

    \[\leadsto \frac{1}{\left(1 + e^{-\frac{x}{s}}\right) \cdot \left(s \cdot \left(1 + {\left(\sqrt{e^{\frac{x}{s}}}\right)}^{2}\right)\right)} \]
  17. Add Preprocessing

Alternative 2: 99.5% accurate, 2.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \frac{1}{\left(1 + e^{-\frac{x_m}{s}}\right) \cdot \left(s \cdot \left(1 + e^{\frac{x_m}{s}}\right)\right)} \end{array} \]
x_m = (fabs.f32 x)
(FPCore (x_m s)
 :precision binary32
 (/ 1.0 (* (+ 1.0 (exp (- (/ x_m s)))) (* s (+ 1.0 (exp (/ x_m s)))))))
x_m = fabs(x);
float code(float x_m, float s) {
	return 1.0f / ((1.0f + expf(-(x_m / s))) * (s * (1.0f + 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
    code = 1.0e0 / ((1.0e0 + exp(-(x_m / s))) * (s * (1.0e0 + exp((x_m / s)))))
end function
x_m = abs(x)
function code(x_m, s)
	return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(-Float32(x_m / s)))) * Float32(s * Float32(Float32(1.0) + exp(Float32(x_m / s))))))
end
x_m = abs(x);
function tmp = code(x_m, s)
	tmp = single(1.0) / ((single(1.0) + exp(-(x_m / s))) * (s * (single(1.0) + exp((x_m / s)))));
end
\begin{array}{l}
x_m = \left|x\right|

\\
\frac{1}{\left(1 + e^{-\frac{x_m}{s}}\right) \cdot \left(s \cdot \left(1 + e^{\frac{x_m}{s}}\right)\right)}
\end{array}
Derivation
  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.7%

    \[\leadsto \color{blue}{\frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{\left|x\right|}{s}}, s\right)}} \]
  3. Add Preprocessing
  4. Step-by-step derivation
    1. frac-2neg99.7%

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

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\color{blue}{\frac{\left|x\right|}{s}}}, s\right)} \]
    3. add-sqr-sqrt99.7%

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

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

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

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

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{\color{blue}{-\left|x\right|}}{s}}, s\right)} \]
    8. fma-udef23.7%

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

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

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

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

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

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

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(e^{\log s} \cdot \color{blue}{e^{\log \left(1 + e^{\frac{-\left|x\right|}{s}}\right)}}\right)} \]
    15. log1p-udef22.8%

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \left(e^{\log s} \cdot e^{\color{blue}{\mathsf{log1p}\left(e^{\frac{-\left|x\right|}{s}}\right)}}\right)} \]
    16. prod-exp22.6%

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

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

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

    \[\leadsto \frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \color{blue}{e^{\mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s}}} \]
  8. Step-by-step derivation
    1. distribute-frac-neg62.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{1}{\left(1 + \frac{1}{e^{\frac{\left|\color{blue}{\sqrt{x} \cdot \sqrt{x}}\right|}{s}}}\right) \cdot e^{\mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s}} \]
    16. fabs-sqr52.1%

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

      \[\leadsto \frac{1}{\left(1 + \frac{1}{e^{\frac{\color{blue}{x}}{s}}}\right) \cdot e^{\mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s}} \]
  9. Applied egg-rr98.0%

    \[\leadsto \frac{1}{\left(1 + \color{blue}{\frac{1}{e^{\frac{x}{s}}}}\right) \cdot e^{\mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s}} \]
  10. Step-by-step derivation
    1. rec-exp98.0%

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

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

    \[\leadsto \frac{1}{\left(1 + \color{blue}{e^{\frac{-x}{s}}}\right) \cdot e^{\mathsf{log1p}\left(e^{\frac{x}{s}}\right) + \log s}} \]
  12. Step-by-step derivation
    1. exp-sum98.4%

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

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

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

      \[\leadsto \frac{1}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(\color{blue}{\left(e^{\frac{x}{s}} + 1\right)} \cdot e^{\log s}\right)} \]
    5. add-exp-log99.8%

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

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

    \[\leadsto \frac{1}{\left(1 + e^{-\frac{x}{s}}\right) \cdot \left(s \cdot \left(1 + e^{\frac{x}{s}}\right)\right)} \]
  15. Add Preprocessing

Alternative 3: 95.2% accurate, 5.6× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \frac{1}{2 \cdot \left(s + s \cdot e^{\frac{x_m}{s}}\right)} \end{array} \]
x_m = (fabs.f32 x)
(FPCore (x_m s)
 :precision binary32
 (/ 1.0 (* 2.0 (+ s (* s (exp (/ x_m s)))))))
x_m = fabs(x);
float code(float x_m, float s) {
	return 1.0f / (2.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
    code = 1.0e0 / (2.0e0 * (s + (s * exp((x_m / s)))))
end function
x_m = abs(x)
function code(x_m, s)
	return Float32(Float32(1.0) / Float32(Float32(2.0) * Float32(s + Float32(s * exp(Float32(x_m / s))))))
end
x_m = abs(x);
function tmp = code(x_m, s)
	tmp = single(1.0) / (single(2.0) * (s + (s * exp((x_m / s)))));
end
\begin{array}{l}
x_m = \left|x\right|

\\
\frac{1}{2 \cdot \left(s + s \cdot e^{\frac{x_m}{s}}\right)}
\end{array}
Derivation
  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.7%

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

    \[\leadsto \frac{1}{\color{blue}{2} \cdot \mathsf{fma}\left(s, e^{\frac{\left|x\right|}{s}}, s\right)} \]
  5. Step-by-step derivation
    1. fma-udef95.9%

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

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

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

Alternative 4: 29.7% accurate, 5.7× speedup?

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

\\
\frac{1}{2 \cdot \left(\left|x_m\right| + s \cdot 2\right)}
\end{array}
Derivation
  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.7%

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

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

    \[\leadsto \frac{1}{2 \cdot \color{blue}{\left(\left|x\right| + 2 \cdot s\right)}} \]
  6. Step-by-step derivation
    1. *-commutative27.8%

      \[\leadsto \frac{1}{2 \cdot \left(\left|x\right| + \color{blue}{s \cdot 2}\right)} \]
  7. Simplified27.8%

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

    \[\leadsto \frac{1}{2 \cdot \left(\left|x\right| + s \cdot 2\right)} \]
  9. Add Preprocessing

Alternative 5: 27.3% 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.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. Step-by-step derivation
    1. associate-*l*99.7%

      \[\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)}} \]
  3. Simplified99.7%

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

    \[\leadsto \color{blue}{\frac{0.25}{s}} \]
  6. Final simplification25.7%

    \[\leadsto \frac{0.25}{s} \]
  7. Add Preprocessing

Reproduce

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