
(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:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (fabs x) (- s))))) (/ (/ t_0 s) (pow (+ t_0 1.0) 2.0))))
float code(float x, float s) {
float t_0 = expf((fabsf(x) / -s));
return (t_0 / s) / powf((t_0 + 1.0f), 2.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((abs(x) / -s))
code = (t_0 / s) / ((t_0 + 1.0e0) ** 2.0e0)
end function
function code(x, s) t_0 = exp(Float32(abs(x) / Float32(-s))) return Float32(Float32(t_0 / s) / (Float32(t_0 + Float32(1.0)) ^ Float32(2.0))) end
function tmp = code(x, s) t_0 = exp((abs(x) / -s)); tmp = (t_0 / s) / ((t_0 + single(1.0)) ^ single(2.0)); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{\left|x\right|}{-s}}\\
\frac{\frac{t\_0}{s}}{{\left(t\_0 + 1\right)}^{2}}
\end{array}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Taylor expanded in x around 0 99.6%
associate-/r*99.6%
mul-1-neg99.6%
distribute-neg-frac299.6%
+-commutative99.6%
mul-1-neg99.6%
distribute-neg-frac299.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ x s))))
(if (<= (fabs x) 9.999999747378752e-6)
(/ (exp (- (/ x s) (* 2.0 (log1p t_0)))) s)
(/ 0.5 (* s (+ 1.0 t_0))))))
float code(float x, float s) {
float t_0 = expf((x / s));
float tmp;
if (fabsf(x) <= 9.999999747378752e-6f) {
tmp = expf(((x / s) - (2.0f * log1pf(t_0)))) / s;
} else {
tmp = 0.5f / (s * (1.0f + t_0));
}
return tmp;
}
function code(x, s) t_0 = exp(Float32(x / s)) tmp = Float32(0.0) if (abs(x) <= Float32(9.999999747378752e-6)) tmp = Float32(exp(Float32(Float32(x / s) - Float32(Float32(2.0) * log1p(t_0)))) / s); else tmp = Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + t_0))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{x}{s}}\\
\mathbf{if}\;\left|x\right| \leq 9.999999747378752 \cdot 10^{-6}:\\
\;\;\;\;\frac{e^{\frac{x}{s} - 2 \cdot \mathsf{log1p}\left(t\_0\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{s \cdot \left(1 + t\_0\right)}\\
\end{array}
\end{array}
if (fabs.f32 x) < 9.99999975e-6Initial program 99.0%
fabs-neg99.0%
distribute-frac-neg99.0%
distribute-frac-neg299.0%
fabs-neg99.0%
*-commutative99.0%
fabs-neg99.0%
+-commutative99.0%
fabs-neg99.0%
Simplified99.0%
distribute-frac-neg299.0%
distribute-frac-neg99.0%
*-un-lft-identity99.0%
times-frac98.2%
Applied egg-rr80.4%
associate-*l/80.4%
add-exp-log80.2%
*-un-lft-identity80.2%
log-div80.2%
add-log-exp97.3%
log-pow98.8%
log1p-define98.9%
Applied egg-rr98.9%
if 9.99999975e-6 < (fabs.f32 x) Initial program 100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg2100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
Simplified100.0%
Applied egg-rr51.4%
associate-*l/51.4%
*-lft-identity51.4%
Simplified51.4%
Taylor expanded in x around 0 50.9%
Taylor expanded in s around 0 50.9%
Final simplification71.9%
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ 1.0 (pow E (/ x s))))))
float code(float x, float s) {
return 0.5f / (s * (1.0f + powf(((float) M_E), (x / s))));
}
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + (Float32(exp(1)) ^ Float32(x / s))))) end
function tmp = code(x, s) tmp = single(0.5) / (s * (single(1.0) + (single(2.71828182845904523536) ^ (x / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(1 + {e}^{\left(\frac{x}{s}\right)}\right)}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Applied egg-rr64.7%
associate-*l/64.8%
*-lft-identity64.8%
Simplified64.8%
Taylor expanded in x around 0 59.5%
Taylor expanded in s around 0 59.5%
*-un-lft-identity59.5%
exp-prod59.5%
Applied egg-rr59.5%
exp-1-e59.5%
Simplified59.5%
Final simplification59.5%
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ 1.0 (exp (/ x s))))))
float code(float x, float s) {
return 0.5f / (s * (1.0f + expf((x / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s * (1.0e0 + exp((x / s))))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = single(0.5) / (s * (single(1.0) + exp((x / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Applied egg-rr64.7%
associate-*l/64.8%
*-lft-identity64.8%
Simplified64.8%
Taylor expanded in x around 0 59.5%
Taylor expanded in s around 0 59.5%
Final simplification59.5%
(FPCore (x s) :precision binary32 (/ 0.5 (+ (* x (+ 1.0 (* (/ x s) 0.5))) (* s 2.0))))
float code(float x, float s) {
return 0.5f / ((x * (1.0f + ((x / s) * 0.5f))) + (s * 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / ((x * (1.0e0 + ((x / s) * 0.5e0))) + (s * 2.0e0))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(Float32(x * Float32(Float32(1.0) + Float32(Float32(x / s) * Float32(0.5)))) + Float32(s * Float32(2.0)))) end
function tmp = code(x, s) tmp = single(0.5) / ((x * (single(1.0) + ((x / s) * single(0.5)))) + (s * single(2.0))); end
\begin{array}{l}
\\
\frac{0.5}{x \cdot \left(1 + \frac{x}{s} \cdot 0.5\right) + s \cdot 2}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Applied egg-rr64.7%
associate-*l/64.8%
*-lft-identity64.8%
Simplified64.8%
Taylor expanded in x around 0 59.5%
Taylor expanded in s around 0 59.5%
Taylor expanded in x around 0 64.6%
Final simplification64.6%
(FPCore (x s) :precision binary32 (if (<= x 9.999999747378752e-6) (/ 0.25 s) (/ (/ 0.5 s) (/ x s))))
float code(float x, float s) {
float tmp;
if (x <= 9.999999747378752e-6f) {
tmp = 0.25f / s;
} else {
tmp = (0.5f / s) / (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 <= 9.999999747378752e-6) then
tmp = 0.25e0 / s
else
tmp = (0.5e0 / s) / (x / s)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(9.999999747378752e-6)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(0.5) / s) / Float32(x / s)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(9.999999747378752e-6)) tmp = single(0.25) / s; else tmp = (single(0.5) / s) / (x / s); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.999999747378752 \cdot 10^{-6}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{0.5}{s}}{\frac{x}{s}}\\
\end{array}
\end{array}
if x < 9.99999975e-6Initial program 99.4%
fabs-neg99.4%
distribute-frac-neg99.4%
distribute-frac-neg299.4%
fabs-neg99.4%
*-commutative99.4%
fabs-neg99.4%
+-commutative99.4%
fabs-neg99.4%
Simplified99.4%
Taylor expanded in s around inf 36.1%
if 9.99999975e-6 < x Initial program 100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg2100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
Simplified100.0%
Applied egg-rr-0.0%
associate-*l/-0.0%
*-lft-identity-0.0%
Simplified-0.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around 0 42.2%
+-commutative42.2%
Simplified42.2%
Taylor expanded in x around inf 42.2%
Final simplification37.8%
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ 2.0 (/ x s)))))
float code(float x, float s) {
return 0.5f / (s * (2.0f + (x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s * (2.0e0 + (x / s)))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(2.0) + Float32(x / s)))) end
function tmp = code(x, s) tmp = single(0.5) / (s * (single(2.0) + (x / s))); end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(2 + \frac{x}{s}\right)}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Applied egg-rr64.7%
associate-*l/64.8%
*-lft-identity64.8%
Simplified64.8%
Taylor expanded in x around 0 59.5%
Taylor expanded in s around 0 59.5%
Taylor expanded in s around inf 50.6%
+-commutative50.6%
Simplified50.6%
Final simplification50.6%
(FPCore (x s) :precision binary32 (if (<= x 9.999999747378752e-6) (/ 0.25 s) (/ 0.5 x)))
float code(float x, float s) {
float tmp;
if (x <= 9.999999747378752e-6f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / x;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 9.999999747378752e-6) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / x
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(9.999999747378752e-6)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / x); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(9.999999747378752e-6)) tmp = single(0.25) / s; else tmp = single(0.5) / x; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.999999747378752 \cdot 10^{-6}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < 9.99999975e-6Initial program 99.4%
fabs-neg99.4%
distribute-frac-neg99.4%
distribute-frac-neg299.4%
fabs-neg99.4%
*-commutative99.4%
fabs-neg99.4%
+-commutative99.4%
fabs-neg99.4%
Simplified99.4%
Taylor expanded in s around inf 36.1%
if 9.99999975e-6 < x Initial program 100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg2100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
Simplified100.0%
Applied egg-rr-0.0%
associate-*l/-0.0%
*-lft-identity-0.0%
Simplified-0.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around 0 42.2%
+-commutative42.2%
Simplified42.2%
Taylor expanded in s around 0 10.2%
Final simplification29.0%
(FPCore (x s) :precision binary32 (/ 0.5 (+ x (* s 2.0))))
float code(float x, float s) {
return 0.5f / (x + (s * 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (x + (s * 2.0e0))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(x + Float32(s * Float32(2.0)))) end
function tmp = code(x, s) tmp = single(0.5) / (x + (s * single(2.0))); end
\begin{array}{l}
\\
\frac{0.5}{x + s \cdot 2}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Applied egg-rr64.7%
associate-*l/64.8%
*-lft-identity64.8%
Simplified64.8%
Taylor expanded in x around 0 59.5%
Taylor expanded in s around 0 59.5%
Taylor expanded in x around 0 29.4%
*-commutative29.4%
Simplified29.4%
Final simplification29.4%
(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}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Taylor expanded in s around inf 27.5%
Final simplification27.5%
herbie shell --seed 2024080
(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))))))