
(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}
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 1.0 s) (* (+ 1.0 (+ 1.0 (expm1 (/ (- x) s)))) (+ 1.0 (exp (/ (fabs x) s))))))
x = abs(x);
float code(float x, float s) {
return (1.0f / s) / ((1.0f + (1.0f + expm1f((-x / s)))) * (1.0f + expf((fabsf(x) / s))));
}
x = abs(x) function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(Float32(Float32(1.0) + Float32(Float32(1.0) + expm1(Float32(Float32(-x) / s)))) * Float32(Float32(1.0) + exp(Float32(abs(x) / s))))) end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{s}}{\left(1 + \left(1 + \mathsf{expm1}\left(\frac{-x}{s}\right)\right)\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
Initial program 99.7%
Simplified99.8%
Taylor expanded in s around 0 99.8%
associate-/r*99.9%
mul-1-neg99.9%
distribute-frac-neg99.9%
Simplified99.9%
expm1-log1p-u99.8%
expm1-udef99.8%
log1p-udef99.9%
add-exp-log99.9%
Applied egg-rr99.9%
associate--l+99.9%
distribute-frac-neg99.9%
mul-1-neg99.9%
expm1-def99.9%
mul-1-neg99.9%
distribute-frac-neg99.9%
Simplified99.9%
Taylor expanded in x around 0 99.9%
expm1-def99.9%
associate-*r/99.9%
neg-mul-199.9%
unpow199.9%
sqr-pow46.4%
fabs-sqr46.4%
sqr-pow97.8%
unpow197.8%
Simplified97.8%
Final simplification97.8%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 1.0 s) (* (+ 1.0 (exp (/ (fabs x) s))) (+ 1.0 (exp (/ (- x) s))))))
x = abs(x);
float code(float x, float s) {
return (1.0f / s) / ((1.0f + expf((fabsf(x) / s))) * (1.0f + expf((-x / s))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / ((1.0e0 + exp((abs(x) / s))) * (1.0e0 + exp((-x / s))))
end function
x = abs(x) function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(Float32(Float32(1.0) + exp(Float32(abs(x) / s))) * Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))) end
x = abs(x) function tmp = code(x, s) tmp = (single(1.0) / s) / ((single(1.0) + exp((abs(x) / s))) * (single(1.0) + exp((-x / s)))); end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{s}}{\left(1 + e^{\frac{\left|x\right|}{s}}\right) \cdot \left(1 + e^{\frac{-x}{s}}\right)}
\end{array}
Initial program 99.7%
Simplified99.8%
Taylor expanded in s around 0 99.8%
associate-/r*99.9%
mul-1-neg99.9%
distribute-frac-neg99.9%
Simplified99.9%
expm1-log1p-u99.8%
expm1-udef99.8%
log1p-udef99.9%
add-exp-log99.9%
Applied egg-rr99.9%
associate--l+99.9%
distribute-frac-neg99.9%
mul-1-neg99.9%
expm1-def99.9%
mul-1-neg99.9%
distribute-frac-neg99.9%
Simplified99.9%
Taylor expanded in x around 0 99.9%
associate-*r/99.9%
neg-mul-199.9%
unpow199.9%
sqr-pow46.4%
fabs-sqr46.4%
sqr-pow97.7%
unpow197.7%
Simplified97.7%
Final simplification97.7%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 0.5 (+ s (* s (pow E (/ x s))))))
x = abs(x);
float code(float x, float s) {
return 0.5f / (s + (s * powf(((float) M_E), (x / s))));
}
x = abs(x) function code(x, s) return Float32(Float32(0.5) / Float32(s + Float32(s * (Float32(exp(1)) ^ Float32(x / s))))) end
x = abs(x) function tmp = code(x, s) tmp = single(0.5) / (s + (s * (single(2.71828182845904523536) ^ (x / s)))); end
\begin{array}{l}
x = |x|\\
\\
\frac{0.5}{s + s \cdot {e}^{\left(\frac{x}{s}\right)}}
\end{array}
Initial program 99.7%
Simplified99.8%
Taylor expanded in s around inf 96.0%
Taylor expanded in x around 0 96.0%
expm1-log1p-u96.0%
expm1-udef82.0%
Applied egg-rr82.0%
expm1-def96.0%
expm1-log1p96.0%
unpow196.0%
sqr-pow44.4%
fabs-sqr44.4%
sqr-pow60.4%
unpow160.4%
*-lft-identity60.4%
*-lft-identity60.4%
Simplified60.4%
*-un-lft-identity60.4%
exp-prod60.4%
exp-1-e60.4%
Applied egg-rr60.4%
Final simplification60.4%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 0.5 s) (+ 1.0 (exp (/ (fabs x) s)))))
x = abs(x);
float code(float x, float s) {
return (0.5f / s) / (1.0f + expf((fabsf(x) / s)));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.5e0 / s) / (1.0e0 + exp((abs(x) / s)))
end function
x = abs(x) function code(x, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(1.0) + exp(Float32(abs(x) / s)))) end
x = abs(x) function tmp = code(x, s) tmp = (single(0.5) / s) / (single(1.0) + exp((abs(x) / s))); end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{0.5}{s}}{1 + e^{\frac{\left|x\right|}{s}}}
\end{array}
Initial program 99.7%
Simplified99.8%
Taylor expanded in s around inf 96.0%
Taylor expanded in s around 0 96.0%
associate-/r*96.0%
Simplified96.0%
Final simplification96.0%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 0.5 (+ s (* s (exp (/ x s))))))
x = abs(x);
float code(float x, float s) {
return 0.5f / (s + (s * expf((x / s))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s + (s * exp((x / s))))
end function
x = abs(x) function code(x, s) return Float32(Float32(0.5) / Float32(s + Float32(s * exp(Float32(x / s))))) end
x = abs(x) function tmp = code(x, s) tmp = single(0.5) / (s + (s * exp((x / s)))); end
\begin{array}{l}
x = |x|\\
\\
\frac{0.5}{s + s \cdot e^{\frac{x}{s}}}
\end{array}
Initial program 99.7%
Simplified99.8%
Taylor expanded in s around inf 96.0%
Taylor expanded in x around 0 96.0%
expm1-log1p-u96.0%
expm1-udef82.0%
Applied egg-rr82.0%
expm1-def96.0%
expm1-log1p96.0%
unpow196.0%
sqr-pow44.4%
fabs-sqr44.4%
sqr-pow60.4%
unpow160.4%
*-lft-identity60.4%
*-lft-identity60.4%
Simplified60.4%
Final simplification60.4%
NOTE: x should be positive before calling this function
(FPCore (x s)
:precision binary32
(if (<= x 5.000000156871975e-23)
(/ (/ 1.0 s) (+ 4.0 (* (/ x s) (/ x s))))
(if (<= x 1.1999999952050366e-11)
(/ (/ 1.0 s) (+ 4.0 (* (* x x) (/ 1.0 (* s s)))))
0.0)))x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 5.000000156871975e-23f) {
tmp = (1.0f / s) / (4.0f + ((x / s) * (x / s)));
} else if (x <= 1.1999999952050366e-11f) {
tmp = (1.0f / s) / (4.0f + ((x * x) * (1.0f / (s * s))));
} else {
tmp = 0.0f;
}
return tmp;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 5.000000156871975e-23) then
tmp = (1.0e0 / s) / (4.0e0 + ((x / s) * (x / s)))
else if (x <= 1.1999999952050366e-11) then
tmp = (1.0e0 / s) / (4.0e0 + ((x * x) * (1.0e0 / (s * s))))
else
tmp = 0.0e0
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(5.000000156871975e-23)) tmp = Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x / s) * Float32(x / s)))); elseif (x <= Float32(1.1999999952050366e-11)) tmp = Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x * x) * Float32(Float32(1.0) / Float32(s * s))))); else tmp = Float32(0.0); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(5.000000156871975e-23)) tmp = (single(1.0) / s) / (single(4.0) + ((x / s) * (x / s))); elseif (x <= single(1.1999999952050366e-11)) tmp = (single(1.0) / s) / (single(4.0) + ((x * x) * (single(1.0) / (s * s)))); else tmp = single(0.0); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.000000156871975 \cdot 10^{-23}:\\
\;\;\;\;\frac{\frac{1}{s}}{4 + \frac{x}{s} \cdot \frac{x}{s}}\\
\mathbf{elif}\;x \leq 1.1999999952050366 \cdot 10^{-11}:\\
\;\;\;\;\frac{\frac{1}{s}}{4 + \left(x \cdot x\right) \cdot \frac{1}{s \cdot s}}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 5.00000016e-23Initial program 99.6%
Simplified99.8%
Taylor expanded in s around 0 99.8%
associate-/r*99.9%
mul-1-neg99.9%
distribute-frac-neg99.9%
Simplified99.9%
Taylor expanded in s around -inf 28.6%
associate-+r+28.6%
Simplified72.4%
Taylor expanded in x around 0 72.4%
unpow272.4%
unpow272.4%
times-frac78.9%
Simplified78.9%
if 5.00000016e-23 < x < 1.2e-11Initial program 99.3%
Simplified99.5%
Taylor expanded in s around 0 99.8%
associate-/r*99.8%
mul-1-neg99.8%
distribute-frac-neg99.8%
Simplified99.8%
Taylor expanded in s around -inf 40.9%
associate-+r+40.9%
Simplified87.1%
div-inv92.6%
Applied egg-rr92.6%
if 1.2e-11 < x Initial program 99.9%
Simplified99.8%
Taylor expanded in s around inf 98.1%
Taylor expanded in x around 0 98.1%
add-exp-log98.1%
*-commutative98.1%
log-prod98.1%
add-log-exp98.1%
Applied egg-rr98.1%
Taylor expanded in s around inf 96.4%
Final simplification84.9%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 1.1999999952050366e-11) (/ (/ 1.0 s) (+ 4.0 (* (/ x s) (/ x s)))) 0.0))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 1.1999999952050366e-11f) {
tmp = (1.0f / s) / (4.0f + ((x / s) * (x / s)));
} else {
tmp = 0.0f;
}
return tmp;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 1.1999999952050366e-11) then
tmp = (1.0e0 / s) / (4.0e0 + ((x / s) * (x / s)))
else
tmp = 0.0e0
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.1999999952050366e-11)) tmp = Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x / s) * Float32(x / s)))); else tmp = Float32(0.0); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.1999999952050366e-11)) tmp = (single(1.0) / s) / (single(4.0) + ((x / s) * (x / s))); else tmp = single(0.0); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.1999999952050366 \cdot 10^{-11}:\\
\;\;\;\;\frac{\frac{1}{s}}{4 + \frac{x}{s} \cdot \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 1.2e-11Initial program 99.6%
Simplified99.8%
Taylor expanded in s around 0 99.8%
associate-/r*99.9%
mul-1-neg99.9%
distribute-frac-neg99.9%
Simplified99.9%
Taylor expanded in s around -inf 29.5%
associate-+r+29.5%
Simplified73.5%
Taylor expanded in x around 0 73.5%
unpow273.5%
unpow273.5%
times-frac77.4%
Simplified77.4%
if 1.2e-11 < x Initial program 99.9%
Simplified99.8%
Taylor expanded in s around inf 98.1%
Taylor expanded in x around 0 98.1%
add-exp-log98.1%
*-commutative98.1%
log-prod98.1%
add-log-exp98.1%
Applied egg-rr98.1%
Taylor expanded in s around inf 96.4%
Final simplification83.2%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 4.99999991225835e-14) (/ 0.25 s) 0.0))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 4.99999991225835e-14f) {
tmp = 0.25f / s;
} else {
tmp = 0.0f;
}
return tmp;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 4.99999991225835e-14) then
tmp = 0.25e0 / s
else
tmp = 0.0e0
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(4.99999991225835e-14)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(0.0); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(4.99999991225835e-14)) tmp = single(0.25) / s; else tmp = single(0.0); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.99999991225835 \cdot 10^{-14}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 4.99999991e-14Initial program 99.6%
Simplified99.7%
Taylor expanded in s around inf 38.5%
if 4.99999991e-14 < x Initial program 99.9%
Simplified99.8%
Taylor expanded in s around inf 98.1%
Taylor expanded in x around 0 98.2%
add-exp-log98.2%
*-commutative98.2%
log-prod98.1%
add-log-exp98.1%
Applied egg-rr98.1%
Taylor expanded in s around inf 95.4%
Final simplification56.5%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 0.0)
x = abs(x);
float code(float x, float s) {
return 0.0f;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.0e0
end function
x = abs(x) function code(x, s) return Float32(0.0) end
x = abs(x) function tmp = code(x, s) tmp = single(0.0); end
\begin{array}{l}
x = |x|\\
\\
0
\end{array}
Initial program 99.7%
Simplified99.8%
Taylor expanded in s around inf 96.0%
Taylor expanded in x around 0 96.0%
add-exp-log95.0%
*-commutative95.0%
log-prod95.0%
add-log-exp95.0%
Applied egg-rr95.0%
Taylor expanded in s around inf 72.9%
Final simplification72.9%
herbie shell --seed 2023283
(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))))))