Logistic distribution

Percentage Accurate: 99.5% → 99.5%
Time: 12.2s
Alternatives: 11
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 11 alternatives:

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

Initial Program: 99.5% accurate, 1.0× speedup?

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

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

Alternative 1: 99.5% accurate, 2.9× speedup?

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

\\
\frac{\frac{1}{2 + 2 \cdot \cosh \left(\frac{\left|x\right|}{s}\right)}}{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. Simplified0

    \[\leadsto expr\]
  3. Add Preprocessing
  4. Applied egg-rr0

    \[\leadsto expr\]
  5. Add Preprocessing

Alternative 2: 99.5% accurate, 2.9× speedup?

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

\\
\frac{1}{s \cdot \left(2 + \cosh \left(\frac{\left|x\right|}{s}\right) \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. Simplified0

    \[\leadsto expr\]
  3. Add Preprocessing
  4. Applied egg-rr0

    \[\leadsto expr\]
  5. Applied egg-rr0

    \[\leadsto expr\]
  6. Add Preprocessing

Alternative 3: 94.7% accurate, 3.0× speedup?

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

\\
\frac{\frac{0.25}{e^{\frac{\left|x\right|}{s}}}}{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. Add Preprocessing
  3. Taylor expanded in s around inf 0

    \[\leadsto expr\]
  4. Simplified0

    \[\leadsto expr\]
  5. Taylor expanded in x around 0 0

    \[\leadsto expr\]
  6. Simplified0

    \[\leadsto expr\]
  7. Add Preprocessing

Alternative 4: 54.6% accurate, 34.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 7.99999999855967 \cdot 10^{-23}:\\ \;\;\;\;\frac{0.25 + \frac{-0.0625}{\frac{\frac{s}{x}}{\frac{x}{s}}}}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{\frac{x \cdot x}{s \cdot s} + 4}}{s}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= x 7.99999999855967e-23)
   (/ (+ 0.25 (/ -0.0625 (/ (/ s x) (/ x s)))) s)
   (/ (/ 1.0 (+ (/ (* x x) (* s s)) 4.0)) s)))
float code(float x, float s) {
	float tmp;
	if (x <= 7.99999999855967e-23f) {
		tmp = (0.25f + (-0.0625f / ((s / x) / (x / s)))) / s;
	} else {
		tmp = (1.0f / (((x * x) / (s * s)) + 4.0f)) / s;
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 7.99999999855967e-23) then
        tmp = (0.25e0 + ((-0.0625e0) / ((s / x) / (x / s)))) / s
    else
        tmp = (1.0e0 / (((x * x) / (s * s)) + 4.0e0)) / s
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(7.99999999855967e-23))
		tmp = Float32(Float32(Float32(0.25) + Float32(Float32(-0.0625) / Float32(Float32(s / x) / Float32(x / s)))) / s);
	else
		tmp = Float32(Float32(Float32(1.0) / Float32(Float32(Float32(x * x) / Float32(s * s)) + Float32(4.0))) / s);
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(7.99999999855967e-23))
		tmp = (single(0.25) + (single(-0.0625) / ((s / x) / (x / s)))) / s;
	else
		tmp = (single(1.0) / (((x * x) / (s * s)) + single(4.0))) / s;
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 7.99999999855967 \cdot 10^{-23}:\\
\;\;\;\;\frac{0.25 + \frac{-0.0625}{\frac{\frac{s}{x}}{\frac{x}{s}}}}{s}\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{\frac{x \cdot x}{s \cdot s} + 4}}{s}\\


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

    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. Add Preprocessing
    3. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]
    5. Applied egg-rr0

      \[\leadsto expr\]

    if 8e-23 < x

    1. Initial program 99.8%

      \[\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. Simplified0

      \[\leadsto expr\]
    3. Add Preprocessing
    4. Applied egg-rr0

      \[\leadsto expr\]
    5. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    6. Simplified0

      \[\leadsto expr\]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 45.9% accurate, 44.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0.0020000000949949026:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{3 \cdot \left(x \cdot x\right)}{s}}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= x 0.0020000000949949026) (/ 0.25 s) (/ 1.0 (/ (* 3.0 (* x x)) s))))
float code(float x, float s) {
	float tmp;
	if (x <= 0.0020000000949949026f) {
		tmp = 0.25f / s;
	} else {
		tmp = 1.0f / ((3.0f * (x * x)) / s);
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 0.0020000000949949026e0) then
        tmp = 0.25e0 / s
    else
        tmp = 1.0e0 / ((3.0e0 * (x * x)) / s)
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(0.0020000000949949026))
		tmp = Float32(Float32(0.25) / s);
	else
		tmp = Float32(Float32(1.0) / Float32(Float32(Float32(3.0) * Float32(x * x)) / s));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(0.0020000000949949026))
		tmp = single(0.25) / s;
	else
		tmp = single(1.0) / ((single(3.0) * (x * x)) / s);
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0020000000949949026:\\
\;\;\;\;\frac{0.25}{s}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 0.00200000009

    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. Add Preprocessing
    3. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]

    if 0.00200000009 < 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. Add Preprocessing
    3. Taylor expanded in s around -inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]
    5. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    6. Simplified0

      \[\leadsto expr\]
    7. Taylor expanded in x around inf 0

      \[\leadsto expr\]
    8. Simplified0

      \[\leadsto expr\]
    9. Taylor expanded in x around inf 0

      \[\leadsto expr\]
    10. Simplified0

      \[\leadsto expr\]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 45.9% accurate, 44.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0.0020000000949949026:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{3 \cdot \frac{x \cdot x}{s}}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= x 0.0020000000949949026) (/ 0.25 s) (/ 1.0 (* 3.0 (/ (* x x) s)))))
float code(float x, float s) {
	float tmp;
	if (x <= 0.0020000000949949026f) {
		tmp = 0.25f / s;
	} else {
		tmp = 1.0f / (3.0f * ((x * x) / s));
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 0.0020000000949949026e0) then
        tmp = 0.25e0 / s
    else
        tmp = 1.0e0 / (3.0e0 * ((x * x) / s))
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(0.0020000000949949026))
		tmp = Float32(Float32(0.25) / s);
	else
		tmp = Float32(Float32(1.0) / Float32(Float32(3.0) * Float32(Float32(x * x) / s)));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(0.0020000000949949026))
		tmp = single(0.25) / s;
	else
		tmp = single(1.0) / (single(3.0) * ((x * x) / s));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0020000000949949026:\\
\;\;\;\;\frac{0.25}{s}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{3 \cdot \frac{x \cdot x}{s}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 0.00200000009

    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. Add Preprocessing
    3. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]

    if 0.00200000009 < 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. Add Preprocessing
    3. Taylor expanded in s around -inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]
    5. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    6. Simplified0

      \[\leadsto expr\]
    7. Taylor expanded in x around inf 0

      \[\leadsto expr\]
    8. Simplified0

      \[\leadsto expr\]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 45.8% accurate, 44.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0.0020000000949949026:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;0.3333333333333333 \cdot \frac{1}{\frac{x}{\frac{s}{x}}}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= x 0.0020000000949949026)
   (/ 0.25 s)
   (* 0.3333333333333333 (/ 1.0 (/ x (/ s x))))))
float code(float x, float s) {
	float tmp;
	if (x <= 0.0020000000949949026f) {
		tmp = 0.25f / s;
	} else {
		tmp = 0.3333333333333333f * (1.0f / (x / (s / x)));
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 0.0020000000949949026e0) then
        tmp = 0.25e0 / s
    else
        tmp = 0.3333333333333333e0 * (1.0e0 / (x / (s / x)))
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(0.0020000000949949026))
		tmp = Float32(Float32(0.25) / s);
	else
		tmp = Float32(Float32(0.3333333333333333) * Float32(Float32(1.0) / Float32(x / Float32(s / x))));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(0.0020000000949949026))
		tmp = single(0.25) / s;
	else
		tmp = single(0.3333333333333333) * (single(1.0) / (x / (s / x)));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0020000000949949026:\\
\;\;\;\;\frac{0.25}{s}\\

\mathbf{else}:\\
\;\;\;\;0.3333333333333333 \cdot \frac{1}{\frac{x}{\frac{s}{x}}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 0.00200000009

    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. Add Preprocessing
    3. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]

    if 0.00200000009 < 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. Add Preprocessing
    3. Taylor expanded in s around -inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]
    5. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    6. Simplified0

      \[\leadsto expr\]
    7. Taylor expanded in x around inf 0

      \[\leadsto expr\]
    8. Simplified0

      \[\leadsto expr\]
    9. Applied egg-rr0

      \[\leadsto expr\]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 8: 76.4% accurate, 47.7× speedup?

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

\\
\frac{1}{s \cdot \left(\frac{\frac{x \cdot x}{s}}{s} + 4\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. Simplified0

    \[\leadsto expr\]
  3. Add Preprocessing
  4. Applied egg-rr0

    \[\leadsto expr\]
  5. Applied egg-rr0

    \[\leadsto expr\]
  6. Taylor expanded in s around inf 0

    \[\leadsto expr\]
  7. Simplified0

    \[\leadsto expr\]
  8. Add Preprocessing

Alternative 9: 45.8% accurate, 51.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0.0020000000949949026:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.3333333333333333}{\frac{x \cdot x}{s}}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= x 0.0020000000949949026)
   (/ 0.25 s)
   (/ 0.3333333333333333 (/ (* x x) s))))
float code(float x, float s) {
	float tmp;
	if (x <= 0.0020000000949949026f) {
		tmp = 0.25f / s;
	} else {
		tmp = 0.3333333333333333f / ((x * x) / s);
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 0.0020000000949949026e0) then
        tmp = 0.25e0 / s
    else
        tmp = 0.3333333333333333e0 / ((x * x) / s)
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(0.0020000000949949026))
		tmp = Float32(Float32(0.25) / s);
	else
		tmp = Float32(Float32(0.3333333333333333) / Float32(Float32(x * x) / s));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(0.0020000000949949026))
		tmp = single(0.25) / s;
	else
		tmp = single(0.3333333333333333) / ((x * x) / s);
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0020000000949949026:\\
\;\;\;\;\frac{0.25}{s}\\

\mathbf{else}:\\
\;\;\;\;\frac{0.3333333333333333}{\frac{x \cdot x}{s}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 0.00200000009

    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. Add Preprocessing
    3. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]

    if 0.00200000009 < 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. Add Preprocessing
    3. Taylor expanded in s around -inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]
    5. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    6. Simplified0

      \[\leadsto expr\]
    7. Taylor expanded in x around inf 0

      \[\leadsto expr\]
    8. Simplified0

      \[\leadsto expr\]
    9. Applied egg-rr0

      \[\leadsto expr\]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 10: 45.2% accurate, 51.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0.0020000000949949026:\\ \;\;\;\;\frac{0.25}{s}\\ \mathbf{else}:\\ \;\;\;\;0.3333333333333333 \cdot \frac{s}{x \cdot x}\\ \end{array} \end{array} \]
(FPCore (x s)
 :precision binary32
 (if (<= x 0.0020000000949949026)
   (/ 0.25 s)
   (* 0.3333333333333333 (/ s (* x x)))))
float code(float x, float s) {
	float tmp;
	if (x <= 0.0020000000949949026f) {
		tmp = 0.25f / s;
	} else {
		tmp = 0.3333333333333333f * (s / (x * x));
	}
	return tmp;
}
real(4) function code(x, s)
    real(4), intent (in) :: x
    real(4), intent (in) :: s
    real(4) :: tmp
    if (x <= 0.0020000000949949026e0) then
        tmp = 0.25e0 / s
    else
        tmp = 0.3333333333333333e0 * (s / (x * x))
    end if
    code = tmp
end function
function code(x, s)
	tmp = Float32(0.0)
	if (x <= Float32(0.0020000000949949026))
		tmp = Float32(Float32(0.25) / s);
	else
		tmp = Float32(Float32(0.3333333333333333) * Float32(s / Float32(x * x)));
	end
	return tmp
end
function tmp_2 = code(x, s)
	tmp = single(0.0);
	if (x <= single(0.0020000000949949026))
		tmp = single(0.25) / s;
	else
		tmp = single(0.3333333333333333) * (s / (x * x));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0020000000949949026:\\
\;\;\;\;\frac{0.25}{s}\\

\mathbf{else}:\\
\;\;\;\;0.3333333333333333 \cdot \frac{s}{x \cdot x}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 0.00200000009

    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. Add Preprocessing
    3. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]

    if 0.00200000009 < 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. Add Preprocessing
    3. Taylor expanded in s around -inf 0

      \[\leadsto expr\]
    4. Simplified0

      \[\leadsto expr\]
    5. Taylor expanded in s around inf 0

      \[\leadsto expr\]
    6. Simplified0

      \[\leadsto expr\]
    7. Taylor expanded in x around inf 0

      \[\leadsto expr\]
    8. Simplified0

      \[\leadsto expr\]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 11: 26.7% 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.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. Add Preprocessing
  3. Taylor expanded in s around inf 0

    \[\leadsto expr\]
  4. Simplified0

    \[\leadsto expr\]
  5. Add Preprocessing

Reproduce

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