
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + exp((-x / s))); end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + exp((-x / s))); end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}
(FPCore (x s) :precision binary32 (let* ((t_0 (cbrt (/ x s)))) (/ 1.0 (+ 1.0 (pow (exp (pow t_0 2.0)) (- t_0))))))
float code(float x, float s) {
float t_0 = cbrtf((x / s));
return 1.0f / (1.0f + powf(expf(powf(t_0, 2.0f)), -t_0));
}
function code(x, s) t_0 = cbrt(Float32(x / s)) return Float32(Float32(1.0) / Float32(Float32(1.0) + (exp((t_0 ^ Float32(2.0))) ^ Float32(-t_0)))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\frac{x}{s}}\\
\frac{1}{1 + {\left(e^{{t_0}^{2}}\right)}^{\left(-t_0\right)}}
\end{array}
\end{array}
Initial program 99.8%
distribute-frac-neg99.8%
exp-neg99.8%
add-sqr-sqrt48.0%
sqrt-unprod60.9%
sqr-neg60.9%
sqrt-unprod14.8%
add-sqr-sqrt29.7%
add-cube-cbrt29.7%
exp-prod29.7%
pow-flip29.7%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ x s))))
(/
1.0
(+
1.0
(* (pow t_0 -0.6666666666666666) (pow (/ 1.0 t_0) 0.3333333333333333))))))
float code(float x, float s) {
float t_0 = expf((x / s));
return 1.0f / (1.0f + (powf(t_0, -0.6666666666666666f) * powf((1.0f / t_0), 0.3333333333333333f)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((x / s))
code = 1.0e0 / (1.0e0 + ((t_0 ** (-0.6666666666666666e0)) * ((1.0e0 / t_0) ** 0.3333333333333333e0)))
end function
function code(x, s) t_0 = exp(Float32(x / s)) return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32((t_0 ^ Float32(-0.6666666666666666)) * (Float32(Float32(1.0) / t_0) ^ Float32(0.3333333333333333))))) end
function tmp = code(x, s) t_0 = exp((x / s)); tmp = single(1.0) / (single(1.0) + ((t_0 ^ single(-0.6666666666666666)) * ((single(1.0) / t_0) ^ single(0.3333333333333333)))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{x}{s}}\\
\frac{1}{1 + {t_0}^{-0.6666666666666666} \cdot {\left(\frac{1}{t_0}\right)}^{0.3333333333333333}}
\end{array}
\end{array}
Initial program 99.8%
distribute-frac-neg99.8%
exp-neg99.8%
add-sqr-sqrt48.0%
sqrt-unprod60.9%
sqr-neg60.9%
sqrt-unprod14.8%
add-sqr-sqrt29.7%
add-sqr-sqrt29.7%
pow229.7%
metadata-eval29.7%
pow-flip29.7%
add-sqr-sqrt14.8%
sqrt-unprod60.8%
sqr-neg60.8%
sqrt-unprod48.0%
add-sqr-sqrt99.7%
metadata-eval99.7%
Applied egg-rr99.7%
add-cbrt-cube99.7%
pow1/399.8%
add-sqr-sqrt99.8%
pow199.8%
pow1/299.8%
pow-prod-up99.8%
metadata-eval99.8%
Applied egg-rr99.8%
pow-pow99.8%
metadata-eval99.8%
metadata-eval99.8%
pow-prod-up99.8%
pow199.8%
pow-pow99.8%
unpow-prod-down99.8%
metadata-eval99.8%
pow-pow99.8%
metadata-eval99.8%
pow1/299.8%
metadata-eval99.8%
Applied egg-rr99.8%
Taylor expanded in x around inf 99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (pow (exp -2.0) (* (/ x s) 0.5)))))
float code(float x, float s) {
return 1.0f / (1.0f + powf(expf(-2.0f), ((x / s) * 0.5f)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + (exp((-2.0e0)) ** ((x / s) * 0.5e0)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + (exp(Float32(-2.0)) ^ Float32(Float32(x / s) * Float32(0.5))))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + (exp(single(-2.0)) ^ ((x / s) * single(0.5)))); end
\begin{array}{l}
\\
\frac{1}{1 + {\left(e^{-2}\right)}^{\left(\frac{x}{s} \cdot 0.5\right)}}
\end{array}
Initial program 99.8%
distribute-frac-neg99.8%
exp-neg99.8%
add-sqr-sqrt48.0%
sqrt-unprod60.9%
sqr-neg60.9%
sqrt-unprod14.8%
add-sqr-sqrt29.7%
add-cube-cbrt29.7%
exp-prod29.7%
pow-flip29.7%
Applied egg-rr99.8%
pow-neg99.8%
inv-pow99.8%
pow-exp99.8%
unpow299.8%
add-cube-cbrt99.8%
pow-exp99.8%
*-commutative99.8%
pow-exp99.8%
add-sqr-sqrt99.7%
sqrt-unprod99.5%
pow-prod-down99.5%
prod-exp99.5%
metadata-eval99.5%
Applied egg-rr99.5%
sqrt-pow199.8%
add-log-exp99.8%
div-inv99.8%
metadata-eval99.8%
metadata-eval99.8%
pow-exp99.7%
log-pow99.8%
metadata-eval99.8%
add-log-exp99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
return 1.0f / (1.0f + expf((-x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (1.0e0 + exp((-x / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + exp((-x / s))); end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}
Initial program 99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (if (<= (/ (- x) s) -2.0) 0.5 (/ 1.0 (- 2.0 (/ x s)))))
float code(float x, float s) {
float tmp;
if ((-x / s) <= -2.0f) {
tmp = 0.5f;
} else {
tmp = 1.0f / (2.0f - (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 / s) <= (-2.0e0)) then
tmp = 0.5e0
else
tmp = 1.0e0 / (2.0e0 - (x / s))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(Float32(-x) / s) <= Float32(-2.0)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / Float32(Float32(2.0) - Float32(x / s))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if ((-x / s) <= single(-2.0)) tmp = single(0.5); else tmp = single(1.0) / (single(2.0) - (x / s)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{-x}{s} \leq -2:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < -2Initial program 100.0%
Taylor expanded in x around 0 28.1%
if -2 < (/.f32 (neg.f32 x) s) Initial program 99.7%
Taylor expanded in x around 0 63.0%
mul-1-neg63.0%
unsub-neg63.0%
Simplified63.0%
Final simplification51.5%
(FPCore (x s) :precision binary32 (let* ((t_0 (/ (- x) s))) (if (<= t_0 2.0) 0.5 (/ 1.0 t_0))))
float code(float x, float s) {
float t_0 = -x / s;
float tmp;
if (t_0 <= 2.0f) {
tmp = 0.5f;
} else {
tmp = 1.0f / t_0;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: tmp
t_0 = -x / s
if (t_0 <= 2.0e0) then
tmp = 0.5e0
else
tmp = 1.0e0 / t_0
end if
code = tmp
end function
function code(x, s) t_0 = Float32(Float32(-x) / s) tmp = Float32(0.0) if (t_0 <= Float32(2.0)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / t_0); end return tmp end
function tmp_2 = code(x, s) t_0 = -x / s; tmp = single(0.0); if (t_0 <= single(2.0)) tmp = single(0.5); else tmp = single(1.0) / t_0; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-x}{s}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{t_0}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < 2Initial program 99.9%
Taylor expanded in x around 0 55.5%
if 2 < (/.f32 (neg.f32 x) s) Initial program 99.7%
Taylor expanded in x around 0 39.2%
mul-1-neg39.2%
unsub-neg39.2%
Simplified39.2%
Taylor expanded in x around inf 39.2%
associate-*r/39.2%
neg-mul-139.2%
Simplified39.2%
Final simplification49.4%
(FPCore (x s) :precision binary32 (if (<= x -2.0000000233721948e-7) (/ 1.0 (/ x s)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -2.0000000233721948e-7f) {
tmp = 1.0f / (x / s);
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-2.0000000233721948e-7)) then
tmp = 1.0e0 / (x / s)
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-2.0000000233721948e-7)) tmp = Float32(Float32(1.0) / Float32(x / s)); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-2.0000000233721948e-7)) tmp = single(1.0) / (x / s); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -2.00000002e-7Initial program 99.8%
Taylor expanded in x around 0 47.7%
mul-1-neg47.7%
unsub-neg47.7%
Simplified47.7%
Taylor expanded in x around inf 44.2%
associate-*r/44.2%
neg-mul-144.2%
Simplified44.2%
remove-double-neg44.2%
frac-2neg44.2%
clear-num47.3%
inv-pow47.3%
add-sqr-sqrt47.3%
sqrt-unprod57.0%
sqr-neg57.0%
sqrt-unprod-0.0%
add-sqr-sqrt47.0%
Applied egg-rr47.0%
unpow-147.0%
Simplified47.0%
if -2.00000002e-7 < x Initial program 99.8%
Taylor expanded in x around 0 50.0%
Final simplification49.1%
(FPCore (x s) :precision binary32 (if (<= x -2.0000000233721948e-7) (/ (- s) x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -2.0000000233721948e-7f) {
tmp = -s / x;
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-2.0000000233721948e-7)) then
tmp = -s / x
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-2.0000000233721948e-7)) tmp = Float32(Float32(-s) / x); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-2.0000000233721948e-7)) tmp = -s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7}:\\
\;\;\;\;\frac{-s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -2.00000002e-7Initial program 99.8%
Taylor expanded in x around 0 47.7%
mul-1-neg47.7%
unsub-neg47.7%
Simplified47.7%
Taylor expanded in x around inf 44.2%
associate-*r/44.2%
neg-mul-144.2%
Simplified44.2%
if -2.00000002e-7 < x Initial program 99.8%
Taylor expanded in x around 0 50.0%
Final simplification48.2%
(FPCore (x s) :precision binary32 (if (<= x -2.0000000233721948e-7) (/ s x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -2.0000000233721948e-7f) {
tmp = s / x;
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-2.0000000233721948e-7)) then
tmp = s / x
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-2.0000000233721948e-7)) tmp = Float32(s / x); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-2.0000000233721948e-7)) tmp = s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7}:\\
\;\;\;\;\frac{s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -2.00000002e-7Initial program 99.8%
Taylor expanded in x around 0 47.7%
mul-1-neg47.7%
unsub-neg47.7%
Simplified47.7%
Taylor expanded in x around inf 44.2%
associate-*r/44.2%
neg-mul-144.2%
Simplified44.2%
remove-double-neg44.2%
frac-2neg44.2%
clear-num47.3%
expm1-log1p-u47.3%
expm1-udef94.1%
clear-num94.1%
add-sqr-sqrt94.1%
sqrt-unprod94.1%
sqr-neg94.1%
sqrt-unprod-0.0%
add-sqr-sqrt93.8%
Applied egg-rr93.8%
expm1-def43.8%
expm1-log1p43.9%
Simplified43.9%
if -2.00000002e-7 < x Initial program 99.8%
Taylor expanded in x around 0 50.0%
Final simplification48.1%
(FPCore (x s) :precision binary32 0.5)
float code(float x, float s) {
return 0.5f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0
end function
function code(x, s) return Float32(0.5) end
function tmp = code(x, s) tmp = single(0.5); end
\begin{array}{l}
\\
0.5
\end{array}
Initial program 99.8%
Taylor expanded in x around 0 37.1%
Final simplification37.1%
herbie shell --seed 2023320
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))