
(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}
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ (exp (/ (- x) s)) (+ (exp (/ (fabs x) s)) 2.0))))
x = abs(x);
float code(float x, float s) {
return (1.0f / s) / (expf((-x / s)) + (expf((fabsf(x) / s)) + 2.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 = (1.0e0 / s) / (exp((-x / s)) + (exp((abs(x) / s)) + 2.0e0))
end function
x = abs(x) function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(exp(Float32(Float32(-x) / s)) + Float32(exp(Float32(abs(x) / s)) + Float32(2.0)))) end
x = abs(x) function tmp = code(x, s) tmp = (single(1.0) / s) / (exp((-x / s)) + (exp((abs(x) / s)) + single(2.0))); end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{s}}{e^{\frac{-x}{s}} + \left(e^{\frac{\left|x\right|}{s}} + 2\right)}
\end{array}
Initial program 99.8%
Simplified99.9%
*-un-lft-identity99.9%
neg-mul-199.9%
times-frac99.9%
metadata-eval99.9%
metadata-eval99.9%
times-frac99.9%
neg-mul-199.9%
*-un-lft-identity99.9%
distribute-frac-neg99.9%
rec-exp99.9%
add-sqr-sqrt99.9%
sqrt-unprod98.1%
sqr-neg98.1%
sqrt-unprod-0.0%
add-sqr-sqrt95.6%
Applied egg-rr97.3%
rec-exp97.3%
distribute-neg-frac97.3%
Simplified97.3%
Final simplification97.3%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 1.0 s) (expm1 (log1p (+ 3.0 (exp (/ x s)))))))
x = abs(x);
float code(float x, float s) {
return (1.0f / s) / expm1f(log1pf((3.0f + expf((x / s)))));
}
x = abs(x) function code(x, s) return Float32(Float32(Float32(1.0) / s) / expm1(log1p(Float32(Float32(3.0) + exp(Float32(x / s)))))) end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{s}}{\mathsf{expm1}\left(\mathsf{log1p}\left(3 + e^{\frac{x}{s}}\right)\right)}
\end{array}
Initial program 99.8%
Simplified99.9%
*-un-lft-identity99.9%
neg-mul-199.9%
times-frac99.9%
metadata-eval99.9%
metadata-eval99.9%
times-frac99.9%
neg-mul-199.9%
*-un-lft-identity99.9%
distribute-frac-neg99.9%
rec-exp99.9%
add-sqr-sqrt99.9%
sqrt-unprod98.1%
sqr-neg98.1%
sqrt-unprod-0.0%
add-sqr-sqrt95.6%
Applied egg-rr97.3%
rec-exp97.3%
distribute-neg-frac97.3%
Simplified97.3%
Taylor expanded in x around 0 96.7%
associate-/r*96.7%
Simplified96.7%
expm1-log1p-u96.7%
add-sqr-sqrt49.4%
fabs-sqr49.4%
add-sqr-sqrt60.8%
Applied egg-rr60.8%
Final simplification60.8%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= (fabs x) 1.9999999996399175e-23) (/ 0.25 s) (/ 1.0 (* s (+ 3.0 (+ 1.0 (+ (/ x s) (* 0.5 (/ (* x x) (* s s))))))))))
x = abs(x);
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 1.9999999996399175e-23f) {
tmp = 0.25f / s;
} else {
tmp = 1.0f / (s * (3.0f + (1.0f + ((x / s) + (0.5f * ((x * x) / (s * s)))))));
}
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 (abs(x) <= 1.9999999996399175e-23) then
tmp = 0.25e0 / s
else
tmp = 1.0e0 / (s * (3.0e0 + (1.0e0 + ((x / s) + (0.5e0 * ((x * x) / (s * s)))))))
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(1.9999999996399175e-23)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(1.0) / Float32(s * Float32(Float32(3.0) + Float32(Float32(1.0) + Float32(Float32(x / s) + Float32(Float32(0.5) * Float32(Float32(x * x) / Float32(s * s)))))))); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (abs(x) <= single(1.9999999996399175e-23)) tmp = single(0.25) / s; else tmp = single(1.0) / (s * (single(3.0) + (single(1.0) + ((x / s) + (single(0.5) * ((x * x) / (s * s))))))); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 1.9999999996399175 \cdot 10^{-23}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{s \cdot \left(3 + \left(1 + \left(\frac{x}{s} + 0.5 \cdot \frac{x \cdot x}{s \cdot s}\right)\right)\right)}\\
\end{array}
\end{array}
if (fabs.f32 x) < 2e-23Initial program 99.3%
Taylor expanded in s around inf 76.1%
if 2e-23 < (fabs.f32 x) Initial program 99.9%
Simplified99.9%
*-un-lft-identity99.9%
neg-mul-199.9%
times-frac99.9%
metadata-eval99.9%
metadata-eval99.9%
times-frac99.9%
neg-mul-199.9%
*-un-lft-identity99.9%
distribute-frac-neg99.9%
rec-exp99.9%
add-sqr-sqrt99.9%
sqrt-unprod99.8%
sqr-neg99.8%
sqrt-unprod-0.0%
add-sqr-sqrt97.2%
Applied egg-rr98.5%
rec-exp98.5%
distribute-neg-frac98.5%
Simplified98.5%
Taylor expanded in x around 0 98.0%
distribute-lft-in98.0%
add-sqr-sqrt49.3%
fabs-sqr49.3%
add-sqr-sqrt56.4%
Applied egg-rr56.4%
distribute-lft-in56.4%
Simplified56.4%
Taylor expanded in x around 0 67.0%
unpow267.0%
unpow267.0%
Simplified67.0%
Final simplification68.7%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (* s (+ 3.0 (exp (/ x s))))))
x = abs(x);
float code(float x, float s) {
return 1.0f / (s * (3.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 * (3.0e0 + exp((x / s))))
end function
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(3.0) + exp(Float32(x / s))))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / (s * (single(3.0) + exp((x / s)))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot \left(3 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.8%
Simplified99.9%
*-un-lft-identity99.9%
neg-mul-199.9%
times-frac99.9%
metadata-eval99.9%
metadata-eval99.9%
times-frac99.9%
neg-mul-199.9%
*-un-lft-identity99.9%
distribute-frac-neg99.9%
rec-exp99.9%
add-sqr-sqrt99.9%
sqrt-unprod98.1%
sqr-neg98.1%
sqrt-unprod-0.0%
add-sqr-sqrt95.6%
Applied egg-rr97.3%
rec-exp97.3%
distribute-neg-frac97.3%
Simplified97.3%
Taylor expanded in x around 0 96.7%
distribute-lft-in96.7%
add-sqr-sqrt49.4%
fabs-sqr49.4%
add-sqr-sqrt60.8%
Applied egg-rr60.8%
distribute-lft-in60.8%
Simplified60.8%
Final simplification60.8%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 1.9999999996399175e-23) (/ 0.25 s) (/ (/ 1.0 s) (+ 4.0 (+ (/ x s) (* 0.5 (/ (* x x) (* s s))))))))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 1.9999999996399175e-23f) {
tmp = 0.25f / s;
} else {
tmp = (1.0f / s) / (4.0f + ((x / s) + (0.5f * ((x * x) / (s * s)))));
}
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.9999999996399175e-23) then
tmp = 0.25e0 / s
else
tmp = (1.0e0 / s) / (4.0e0 + ((x / s) + (0.5e0 * ((x * x) / (s * s)))))
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.9999999996399175e-23)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x / s) + Float32(Float32(0.5) * Float32(Float32(x * x) / Float32(s * s)))))); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.9999999996399175e-23)) tmp = single(0.25) / s; else tmp = (single(1.0) / s) / (single(4.0) + ((x / s) + (single(0.5) * ((x * x) / (s * s))))); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.9999999996399175 \cdot 10^{-23}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{s}}{4 + \left(\frac{x}{s} + 0.5 \cdot \frac{x \cdot x}{s \cdot s}\right)}\\
\end{array}
\end{array}
if x < 2e-23Initial program 99.7%
Taylor expanded in s around inf 34.0%
if 2e-23 < x Initial program 99.9%
Simplified99.9%
*-un-lft-identity99.9%
neg-mul-199.9%
times-frac99.9%
metadata-eval99.9%
metadata-eval99.9%
times-frac99.9%
neg-mul-199.9%
*-un-lft-identity99.9%
distribute-frac-neg99.9%
rec-exp99.9%
add-sqr-sqrt99.9%
sqrt-unprod99.6%
sqr-neg99.6%
sqrt-unprod-0.0%
add-sqr-sqrt97.4%
Applied egg-rr99.9%
rec-exp99.9%
distribute-neg-frac99.9%
Simplified99.9%
Taylor expanded in x around 0 98.2%
associate-/r*98.2%
Simplified98.2%
expm1-log1p-u98.3%
add-sqr-sqrt98.2%
fabs-sqr98.2%
add-sqr-sqrt98.3%
Applied egg-rr98.3%
Taylor expanded in x around 0 79.2%
*-commutative79.2%
unpow279.2%
unpow279.2%
Simplified79.2%
Final simplification52.5%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ 4.0 (* 2.0 (/ x s)))))
x = abs(x);
float code(float x, float s) {
return (1.0f / s) / (4.0f + (2.0f * (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) / (4.0e0 + (2.0e0 * (x / s)))
end function
x = abs(x) function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(2.0) * Float32(x / s)))) end
x = abs(x) function tmp = code(x, s) tmp = (single(1.0) / s) / (single(4.0) + (single(2.0) * (x / s))); end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{s}}{4 + 2 \cdot \frac{x}{s}}
\end{array}
Initial program 99.8%
Simplified99.9%
expm1-log1p-u97.9%
expm1-udef97.9%
Applied egg-rr59.2%
expm1-def59.2%
expm1-log1p60.3%
associate-/r*60.3%
*-lft-identity60.3%
associate-*l/60.3%
associate-*r/60.3%
*-rgt-identity60.3%
+-commutative60.3%
+-commutative60.3%
associate-+l+60.3%
count-260.3%
Simplified60.3%
Taylor expanded in x around 0 51.4%
Final simplification51.4%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ (/ x s) 4.0)))
x = abs(x);
float code(float x, float s) {
return (1.0f / s) / ((x / s) + 4.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 = (1.0e0 / s) / ((x / s) + 4.0e0)
end function
x = abs(x) function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(Float32(x / s) + Float32(4.0))) end
x = abs(x) function tmp = code(x, s) tmp = (single(1.0) / s) / ((x / s) + single(4.0)); end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{s}}{\frac{x}{s} + 4}
\end{array}
Initial program 99.8%
Simplified99.9%
*-un-lft-identity99.9%
neg-mul-199.9%
times-frac99.9%
metadata-eval99.9%
metadata-eval99.9%
times-frac99.9%
neg-mul-199.9%
*-un-lft-identity99.9%
distribute-frac-neg99.9%
rec-exp99.9%
add-sqr-sqrt99.9%
sqrt-unprod98.1%
sqr-neg98.1%
sqrt-unprod-0.0%
add-sqr-sqrt95.6%
Applied egg-rr97.3%
rec-exp97.3%
distribute-neg-frac97.3%
Simplified97.3%
Taylor expanded in x around 0 96.7%
associate-/r*96.7%
Simplified96.7%
expm1-log1p-u96.7%
add-sqr-sqrt49.4%
fabs-sqr49.4%
add-sqr-sqrt60.8%
Applied egg-rr60.8%
Taylor expanded in x around 0 51.4%
+-commutative51.4%
Simplified51.4%
Final simplification51.4%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (+ x (* s 4.0))))
x = abs(x);
float code(float x, float s) {
return 1.0f / (x + (s * 4.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 = 1.0e0 / (x + (s * 4.0e0))
end function
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(x + Float32(s * Float32(4.0)))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / (x + (s * single(4.0))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{x + s \cdot 4}
\end{array}
Initial program 99.8%
Simplified99.9%
*-un-lft-identity99.9%
neg-mul-199.9%
times-frac99.9%
metadata-eval99.9%
metadata-eval99.9%
times-frac99.9%
neg-mul-199.9%
*-un-lft-identity99.9%
distribute-frac-neg99.9%
rec-exp99.9%
add-sqr-sqrt99.9%
sqrt-unprod98.1%
sqr-neg98.1%
sqrt-unprod-0.0%
add-sqr-sqrt95.6%
Applied egg-rr97.3%
rec-exp97.3%
distribute-neg-frac97.3%
Simplified97.3%
Taylor expanded in x around 0 96.7%
distribute-lft-in96.7%
add-sqr-sqrt49.4%
fabs-sqr49.4%
add-sqr-sqrt60.8%
Applied egg-rr60.8%
distribute-lft-in60.8%
Simplified60.8%
Taylor expanded in s around inf 28.7%
Final simplification28.7%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 9.999999974752427e-7) (/ 0.25 s) (/ 0.5 x)))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 9.999999974752427e-7f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / x;
}
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 <= 9.999999974752427e-7) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / x
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(9.999999974752427e-7)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / x); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(9.999999974752427e-7)) tmp = single(0.25) / s; else tmp = single(0.5) / x; end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.999999974752427 \cdot 10^{-7}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < 9.99999997e-7Initial program 99.7%
Taylor expanded in s around inf 36.0%
if 9.99999997e-7 < x Initial program 100.0%
Simplified100.0%
expm1-log1p-u100.0%
expm1-udef100.0%
Applied egg-rr100.0%
expm1-def100.0%
expm1-log1p100.0%
associate-/r*100.0%
*-lft-identity100.0%
associate-*l/100.0%
associate-*r/100.0%
*-rgt-identity100.0%
+-commutative100.0%
+-commutative100.0%
associate-+l+100.0%
count-2100.0%
Simplified100.0%
Taylor expanded in x around 0 49.2%
Taylor expanded in s around 0 10.3%
Final simplification28.2%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 0.25 s))
x = abs(x);
float code(float x, float s) {
return 0.25f / 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.25e0 / s
end function
x = abs(x) function code(x, s) return Float32(Float32(0.25) / s) end
x = abs(x) function tmp = code(x, s) tmp = single(0.25) / s; end
\begin{array}{l}
x = |x|\\
\\
\frac{0.25}{s}
\end{array}
Initial program 99.8%
Taylor expanded in s around inf 26.4%
Final simplification26.4%
herbie shell --seed 2023228
(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))))))