
(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 9 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
(cast
(!
:precision
binary64
(let* ((t_0 (exp (/ (fabs x) (- s)))))
(/ t_0 (* s (pow (+ t_0 1.0) 2.0)))))))
float code(float x, float s) {
double t_0_1 = exp((fabs(x) / -((double) s)));
double tmp = t_0_1 / (((double) s) * pow((t_0_1 + 1.0), 2.0));
return (float) tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(8) :: t_0
real(8) :: t_0_1
real(8) :: tmp
t_0_1 = exp((abs(x) / -real(s, 8)))
tmp = t_0_1 / (real(s, 8) * ((t_0_1 + 1.0d0) ** 2.0d0))
code = real(tmp, 4)
end function
function code(x, s) t_0_1 = exp(Float64(abs(x) / Float64(-Float64(s)))) tmp = Float64(t_0_1 / Float64(Float64(s) * (Float64(t_0_1 + 1.0) ^ 2.0))) return Float32(tmp) end
function tmp_3 = code(x, s) t_0_2 = exp((abs(x) / -s)); tmp = t_0_2 / (double(s) * ((t_0_2 + 1.0) ^ 2.0)); tmp_3 = single(tmp); end
\begin{array}{l}
\\
\langle \left( \begin{array}{l}
t_0 := e^{\frac{\left|x\right|}{-s}}\\
\frac{t_0}{s \cdot {\left(t_0 + 1\right)}^{2}}
\end{array} \right)_{\text{binary64}} \rangle_{\text{binary32}}
\end{array}
Initial program 100.0%
clear-num100.0%
div-inv100.0%
clear-num100.0%
associate-/l/100.0%
Applied egg-rr100.0%
*-lft-identity100.0%
*-commutative100.0%
+-commutative100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (fabs x) (- s))))) (* (/ t_0 s) (exp (* -2.0 (log1p t_0))))))
float code(float x, float s) {
float t_0 = expf((fabsf(x) / -s));
return (t_0 / s) * expf((-2.0f * log1pf(t_0)));
}
function code(x, s) t_0 = exp(Float32(abs(x) / Float32(-s))) return Float32(Float32(t_0 / s) * exp(Float32(Float32(-2.0) * log1p(t_0)))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{\left|x\right|}{-s}}\\
\frac{t_0}{s} \cdot e^{-2 \cdot \mathsf{log1p}\left(t_0\right)}
\end{array}
\end{array}
Initial program 99.8%
/-rgt-identity99.8%
associate-/r*99.8%
*-lft-identity99.8%
associate-*l*99.8%
+-commutative99.8%
+-commutative99.8%
Simplified99.8%
clear-num99.8%
div-inv99.8%
clear-num99.8%
associate-/r*99.8%
div-inv99.8%
frac-2neg99.8%
remove-double-neg99.8%
pow299.8%
Applied egg-rr99.8%
*-lft-identity99.8%
Simplified99.8%
pow-to-exp99.8%
*-commutative99.8%
log1p-def99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s)))) (/ t_0 (* s (pow (+ 1.0 t_0) 2.0)))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return t_0 / (s * powf((1.0f + t_0), 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 * ((1.0e0 + t_0) ** 2.0e0))
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(t_0 / Float32(s * (Float32(Float32(1.0) + t_0) ^ Float32(2.0)))) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); tmp = t_0 / (s * ((single(1.0) + t_0) ^ single(2.0))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
\frac{t_0}{s \cdot {\left(1 + t_0\right)}^{2}}
\end{array}
\end{array}
Initial program 99.8%
/-rgt-identity99.8%
associate-/r*99.8%
*-lft-identity99.8%
associate-*l*99.8%
+-commutative99.8%
+-commutative99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
unpow299.8%
mul-1-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg99.8%
rec-exp99.8%
mul-1-neg99.8%
distribute-frac-neg99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (/ (/ (pow (+ 1.0 (exp (/ (fabs x) (- s)))) -2.0) (exp (/ (fabs x) s))) s))
float code(float x, float s) {
return (powf((1.0f + expf((fabsf(x) / -s))), -2.0f) / expf((fabsf(x) / s))) / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (((1.0e0 + exp((abs(x) / -s))) ** (-2.0e0)) / exp((abs(x) / s))) / s
end function
function code(x, s) return Float32(Float32((Float32(Float32(1.0) + exp(Float32(abs(x) / Float32(-s)))) ^ Float32(-2.0)) / exp(Float32(abs(x) / s))) / s) end
function tmp = code(x, s) tmp = (((single(1.0) + exp((abs(x) / -s))) ^ single(-2.0)) / exp((abs(x) / s))) / s; end
\begin{array}{l}
\\
\frac{\frac{{\left(1 + e^{\frac{\left|x\right|}{-s}}\right)}^{-2}}{e^{\frac{\left|x\right|}{s}}}}{s}
\end{array}
Initial program 99.8%
/-rgt-identity99.8%
associate-/r*99.8%
*-lft-identity99.8%
associate-*l*99.8%
+-commutative99.8%
+-commutative99.8%
Simplified99.8%
associate-/r*99.8%
frac-2neg99.8%
distribute-frac-neg99.8%
frac-2neg99.8%
remove-double-neg99.8%
pow299.8%
Applied egg-rr99.8%
distribute-neg-frac99.8%
Simplified99.8%
frac-2neg99.8%
div-inv99.8%
clear-num99.8%
pow-flip99.7%
metadata-eval99.7%
associate-*l/99.8%
div-inv99.8%
frac-2neg99.8%
remove-double-neg99.8%
times-frac99.4%
Applied egg-rr99.5%
add-exp-log_binary3297.7%
Applied rewrite-once97.7%
rem-exp-log99.5%
associate-*l/99.8%
*-lft-identity99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (/ 0.5 (* s (* (exp (/ (fabs x) s)) (+ 1.0 (exp (/ (- (fabs x)) s)))))))
float code(float x, float s) {
return 0.5f / (s * (expf((fabsf(x) / s)) * (1.0f + expf((-fabsf(x) / s)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s * (exp((abs(x) / s)) * (1.0e0 + exp((-abs(x) / s)))))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(exp(Float32(abs(x) / s)) * Float32(Float32(1.0) + exp(Float32(Float32(-abs(x)) / s)))))) end
function tmp = code(x, s) tmp = single(0.5) / (s * (exp((abs(x) / s)) * (single(1.0) + exp((-abs(x) / s))))); end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(e^{\frac{\left|x\right|}{s}} \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)}
\end{array}
Initial program 99.8%
associate-/r*99.8%
+-commutative99.8%
+-commutative99.8%
Simplified99.8%
Taylor expanded in s around inf 95.8%
clear-num95.8%
div-inv95.8%
associate-/r/95.4%
associate-/r*95.4%
distribute-frac-neg95.4%
exp-neg95.4%
frac-times95.8%
metadata-eval95.8%
metadata-eval95.8%
Applied egg-rr95.8%
*-lft-identity95.8%
associate-/r*95.4%
Simplified95.4%
Taylor expanded in s around 0 95.8%
Final simplification95.8%
(FPCore (x s) :precision binary32 (/ (exp (/ (- (fabs x)) s)) (+ (* (fabs x) -4.0) (* s 4.0))))
float code(float x, float s) {
return expf((-fabsf(x) / s)) / ((fabsf(x) * -4.0f) + (s * 4.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp((-abs(x) / s)) / ((abs(x) * (-4.0e0)) + (s * 4.0e0))
end function
function code(x, s) return Float32(exp(Float32(Float32(-abs(x)) / s)) / Float32(Float32(abs(x) * Float32(-4.0)) + Float32(s * Float32(4.0)))) end
function tmp = code(x, s) tmp = exp((-abs(x) / s)) / ((abs(x) * single(-4.0)) + (s * single(4.0))); end
\begin{array}{l}
\\
\frac{e^{\frac{-\left|x\right|}{s}}}{\left|x\right| \cdot -4 + s \cdot 4}
\end{array}
Initial program 99.8%
/-rgt-identity99.8%
associate-/r*99.8%
*-lft-identity99.8%
associate-*l*99.8%
+-commutative99.8%
+-commutative99.8%
Simplified99.8%
Taylor expanded in s around inf 95.7%
Final simplification95.7%
(FPCore (x s) :precision binary32 (/ (/ 0.25 s) (exp (/ (fabs x) s))))
float code(float x, float s) {
return (0.25f / s) / expf((fabsf(x) / s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.25e0 / s) / exp((abs(x) / s))
end function
function code(x, s) return Float32(Float32(Float32(0.25) / s) / exp(Float32(abs(x) / s))) end
function tmp = code(x, s) tmp = (single(0.25) / s) / exp((abs(x) / s)); end
\begin{array}{l}
\\
\frac{\frac{0.25}{s}}{e^{\frac{\left|x\right|}{s}}}
\end{array}
Initial program 99.8%
/-rgt-identity99.8%
associate-/r*99.8%
*-lft-identity99.8%
associate-*l*99.8%
+-commutative99.8%
+-commutative99.8%
Simplified99.8%
Taylor expanded in s around inf 95.5%
*-lft-identity95.5%
*-commutative95.5%
div-inv95.5%
*-commutative95.5%
*-commutative95.5%
associate-/r*95.5%
metadata-eval95.5%
distribute-frac-neg95.5%
exp-neg95.5%
un-div-inv95.5%
Applied egg-rr95.5%
*-rgt-identity95.5%
Simplified95.5%
Final simplification95.5%
(FPCore (x s) :precision binary32 (/ (/ 0.25 s) (+ 1.0 (/ (fabs x) s))))
float code(float x, float s) {
return (0.25f / s) / (1.0f + (fabsf(x) / s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.25e0 / s) / (1.0e0 + (abs(x) / s))
end function
function code(x, s) return Float32(Float32(Float32(0.25) / s) / Float32(Float32(1.0) + Float32(abs(x) / s))) end
function tmp = code(x, s) tmp = (single(0.25) / s) / (single(1.0) + (abs(x) / s)); end
\begin{array}{l}
\\
\frac{\frac{0.25}{s}}{1 + \frac{\left|x\right|}{s}}
\end{array}
Initial program 99.8%
/-rgt-identity99.8%
associate-/r*99.8%
*-lft-identity99.8%
associate-*l*99.8%
+-commutative99.8%
+-commutative99.8%
Simplified99.8%
Taylor expanded in s around inf 95.5%
*-lft-identity95.5%
*-commutative95.5%
div-inv95.5%
*-commutative95.5%
*-commutative95.5%
associate-/r*95.5%
metadata-eval95.5%
distribute-frac-neg95.5%
exp-neg95.5%
un-div-inv95.5%
Applied egg-rr95.5%
*-rgt-identity95.5%
Simplified95.5%
Taylor expanded in s around inf 51.0%
+-commutative51.0%
Simplified51.0%
Final simplification51.0%
(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.8%
/-rgt-identity99.8%
associate-/r*99.8%
*-lft-identity99.8%
associate-*l*99.8%
+-commutative99.8%
+-commutative99.8%
Simplified99.8%
Taylor expanded in s around inf 25.7%
Final simplification25.7%
herbie shell --seed 2023297
(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))))))