
(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 12 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 (exp (- (log1p (exp (/ (- x) s))))))
float code(float x, float s) {
return expf(-log1pf(expf((-x / s))));
}
function code(x, s) return exp(Float32(-log1p(exp(Float32(Float32(-x) / s))))) end
\begin{array}{l}
\\
e^{-\mathsf{log1p}\left(e^{\frac{-x}{s}}\right)}
\end{array}
Initial program 99.8%
div-inv99.8%
exp-prod81.1%
neg-mul-181.1%
exp-prod81.1%
pow-pow99.8%
div-inv99.8%
Applied egg-rr99.8%
add-exp-log99.7%
log-rec99.8%
log1p-udef99.9%
Applied egg-rr99.9%
exp-prod99.9%
neg-mul-199.9%
distribute-neg-frac99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (exp (/ (- x) s)) 1.0)))
float code(float x, float s) {
return 1.0f / (expf((-x / s)) + 1.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (exp((-x / s)) + 1.0e0)
end function
function code(x, s) return Float32(Float32(1.0) / Float32(exp(Float32(Float32(-x) / s)) + Float32(1.0))) end
function tmp = code(x, s) tmp = single(1.0) / (exp((-x / s)) + single(1.0)); end
\begin{array}{l}
\\
\frac{1}{e^{\frac{-x}{s}} + 1}
\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 (- (* 0.5 (* (/ x s) (/ x s))) (/ x s))))))
float code(float x, float s) {
float tmp;
if ((-x / s) <= -2.0f) {
tmp = 0.5f;
} else {
tmp = 1.0f / (2.0f + ((0.5f * ((x / s) * (x / 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 / s) <= (-2.0e0)) then
tmp = 0.5e0
else
tmp = 1.0e0 / (2.0e0 + ((0.5e0 * ((x / s) * (x / s))) - (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(Float32(Float32(0.5) * Float32(Float32(x / s) * Float32(x / s))) - 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) + ((single(0.5) * ((x / s) * (x / s))) - (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 + \left(0.5 \cdot \left(\frac{x}{s} \cdot \frac{x}{s}\right) - \frac{x}{s}\right)}\\
\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 78.2%
mul-1-neg78.2%
unsub-neg78.2%
unpow278.2%
unpow278.2%
times-frac79.7%
Simplified79.7%
Final simplification62.0%
(FPCore (x s) :precision binary32 (if (<= (/ (- x) s) -2.0) 0.5 (/ 1.0 (+ 2.0 (* (/ x s) (+ (* 0.5 (/ x s)) -1.0))))))
float code(float x, float s) {
float tmp;
if ((-x / s) <= -2.0f) {
tmp = 0.5f;
} else {
tmp = 1.0f / (2.0f + ((x / s) * ((0.5f * (x / s)) + -1.0f)));
}
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) * ((0.5e0 * (x / s)) + (-1.0e0))))
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(Float32(x / s) * Float32(Float32(Float32(0.5) * Float32(x / s)) + Float32(-1.0))))); 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) * ((single(0.5) * (x / s)) + single(-1.0)))); 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} \cdot \left(0.5 \cdot \frac{x}{s} + -1\right)}\\
\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%
div-inv99.6%
exp-prod80.2%
neg-mul-180.2%
exp-prod80.2%
pow-pow99.6%
div-inv99.7%
Applied egg-rr99.7%
Taylor expanded in x around 0 78.2%
unpow278.2%
unpow278.2%
times-frac79.7%
associate-*r*79.7%
distribute-rgt-out79.7%
*-commutative79.7%
Simplified79.7%
Final simplification62.0%
(FPCore (x s) :precision binary32 (if (<= (- x) 1.0000000031710769e-29) 0.5 (/ 1.0 (+ 2.0 (- (* 0.5 (* x (/ x (* s s)))) (/ x s))))))
float code(float x, float s) {
float tmp;
if (-x <= 1.0000000031710769e-29f) {
tmp = 0.5f;
} else {
tmp = 1.0f / (2.0f + ((0.5f * (x * (x / (s * 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 <= 1.0000000031710769e-29) then
tmp = 0.5e0
else
tmp = 1.0e0 / (2.0e0 + ((0.5e0 * (x * (x / (s * s)))) - (x / s)))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(1.0000000031710769e-29)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(Float32(0.5) * Float32(x * Float32(x / Float32(s * s)))) - Float32(x / s)))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(1.0000000031710769e-29)) tmp = single(0.5); else tmp = single(1.0) / (single(2.0) + ((single(0.5) * (x * (x / (s * s)))) - (x / s))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq 1.0000000031710769 \cdot 10^{-29}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + \left(0.5 \cdot \left(x \cdot \frac{x}{s \cdot s}\right) - \frac{x}{s}\right)}\\
\end{array}
\end{array}
if (neg.f32 x) < 1e-29Initial program 99.7%
Taylor expanded in x around 0 48.6%
if 1e-29 < (neg.f32 x) Initial program 99.8%
Taylor expanded in x around 0 79.8%
mul-1-neg79.8%
unsub-neg79.8%
unpow279.8%
unpow279.8%
times-frac74.8%
Simplified74.8%
associate-*r/74.8%
Applied egg-rr74.8%
Taylor expanded in x around 0 74.8%
unpow274.8%
associate-/l*74.8%
Simplified74.8%
expm1-log1p-u74.8%
expm1-udef74.8%
associate-/l/77.0%
associate-*r/82.4%
Applied egg-rr82.4%
expm1-def82.4%
expm1-log1p82.4%
unpow282.4%
associate-/r/82.4%
unpow282.4%
Simplified82.4%
Final simplification64.0%
(FPCore (x s) :precision binary32 (if (<= (- x) 1.9999999996399175e-23) 0.5 (/ 1.0 (+ 2.0 (* 0.5 (/ (* x x) (* s s)))))))
float code(float x, float s) {
float tmp;
if (-x <= 1.9999999996399175e-23f) {
tmp = 0.5f;
} else {
tmp = 1.0f / (2.0f + (0.5f * ((x * x) / (s * s))));
}
return tmp;
}
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.5e0
else
tmp = 1.0e0 / (2.0e0 + (0.5e0 * ((x * x) / (s * s))))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(1.9999999996399175e-23)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(0.5) * Float32(Float32(x * x) / Float32(s * s))))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(1.9999999996399175e-23)) tmp = single(0.5); else tmp = single(1.0) / (single(2.0) + (single(0.5) * ((x * x) / (s * s)))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq 1.9999999996399175 \cdot 10^{-23}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + 0.5 \cdot \frac{x \cdot x}{s \cdot s}}\\
\end{array}
\end{array}
if (neg.f32 x) < 2e-23Initial program 99.7%
Taylor expanded in x around 0 50.4%
if 2e-23 < (neg.f32 x) Initial program 99.9%
div-inv99.8%
exp-prod79.4%
neg-mul-179.4%
exp-prod79.4%
pow-pow99.8%
div-inv99.8%
Applied egg-rr99.8%
Taylor expanded in x around 0 80.5%
unpow280.5%
unpow280.5%
times-frac74.7%
associate-*r*74.7%
distribute-rgt-out74.7%
*-commutative74.7%
Simplified74.7%
Taylor expanded in x around inf 78.3%
*-commutative78.3%
unpow278.3%
unpow278.3%
Simplified78.3%
Final simplification61.7%
(FPCore (x s) :precision binary32 (if (<= (/ (- x) s) 2000000.0) 0.5 (* 2.0 (/ (* s s) (* x x)))))
float code(float x, float s) {
float tmp;
if ((-x / s) <= 2000000.0f) {
tmp = 0.5f;
} else {
tmp = 2.0f * ((s * s) / (x * x));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if ((-x / s) <= 2000000.0e0) then
tmp = 0.5e0
else
tmp = 2.0e0 * ((s * s) / (x * x))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(Float32(-x) / s) <= Float32(2000000.0)) tmp = Float32(0.5); else tmp = Float32(Float32(2.0) * Float32(Float32(s * s) / Float32(x * x))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if ((-x / s) <= single(2000000.0)) tmp = single(0.5); else tmp = single(2.0) * ((s * s) / (x * x)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{-x}{s} \leq 2000000:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \frac{s \cdot s}{x \cdot x}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < 2e6Initial program 99.7%
Taylor expanded in x around 0 49.2%
if 2e6 < (/.f32 (neg.f32 x) s) Initial program 100.0%
Taylor expanded in x around 0 85.8%
mul-1-neg85.8%
unsub-neg85.8%
unpow285.8%
unpow285.8%
times-frac78.9%
Simplified78.9%
Taylor expanded in x around inf 83.5%
unpow283.5%
unpow283.5%
Simplified83.5%
Final simplification60.9%
(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 59.3%
mul-1-neg59.3%
unsub-neg59.3%
Simplified59.3%
Final simplification48.6%
(FPCore (x s) :precision binary32 (let* ((t_0 (/ (- x) s))) (if (<= t_0 1.0) 0.5 (/ 1.0 t_0))))
float code(float x, float s) {
float t_0 = -x / s;
float tmp;
if (t_0 <= 1.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 <= 1.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(1.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(1.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 1:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{t_0}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < 1Initial program 99.8%
Taylor expanded in x around 0 52.9%
if 1 < (/.f32 (neg.f32 x) s) Initial program 99.8%
Taylor expanded in x around 0 36.8%
mul-1-neg36.8%
unsub-neg36.8%
Simplified36.8%
Taylor expanded in x around inf 36.8%
neg-mul-136.8%
distribute-neg-frac36.8%
Simplified36.8%
Final simplification46.5%
(FPCore (x s) :precision binary32 (if (<= x -9.999999747378752e-5) (* s (/ 1.0 x)) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -9.999999747378752e-5f) {
tmp = s * (1.0f / 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 <= (-9.999999747378752e-5)) then
tmp = s * (1.0e0 / x)
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-9.999999747378752e-5)) tmp = Float32(s * Float32(Float32(1.0) / x)); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-9.999999747378752e-5)) tmp = s * (single(1.0) / x); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.999999747378752 \cdot 10^{-5}:\\
\;\;\;\;s \cdot \frac{1}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -9.99999975e-5Initial program 100.0%
Taylor expanded in x around 0 46.3%
mul-1-neg46.3%
unsub-neg46.3%
Simplified46.3%
Taylor expanded in x around inf 46.3%
neg-mul-146.3%
distribute-neg-frac46.3%
Simplified46.3%
associate-/r/43.6%
add-sqr-sqrt43.6%
sqrt-unprod57.4%
sqr-neg57.4%
sqrt-unprod-0.0%
add-sqr-sqrt43.6%
Applied egg-rr43.6%
if -9.99999975e-5 < x Initial program 99.7%
Taylor expanded in x around 0 46.4%
Final simplification45.6%
(FPCore (x s) :precision binary32 (if (<= x -9.999999747378752e-5) (/ s x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -9.999999747378752e-5f) {
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 <= (-9.999999747378752e-5)) 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(-9.999999747378752e-5)) 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(-9.999999747378752e-5)) tmp = s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.999999747378752 \cdot 10^{-5}:\\
\;\;\;\;\frac{s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -9.99999975e-5Initial program 100.0%
Taylor expanded in x around 0 46.3%
mul-1-neg46.3%
unsub-neg46.3%
Simplified46.3%
Taylor expanded in x around inf 46.3%
neg-mul-146.3%
distribute-neg-frac46.3%
Simplified46.3%
expm1-log1p-u46.3%
expm1-udef96.3%
clear-num96.3%
add-sqr-sqrt96.3%
sqrt-unprod96.3%
sqr-neg96.3%
sqrt-unprod-0.0%
add-sqr-sqrt96.3%
Applied egg-rr96.3%
expm1-def43.6%
expm1-log1p43.6%
Simplified43.6%
if -9.99999975e-5 < x Initial program 99.7%
Taylor expanded in x around 0 46.4%
Final simplification45.6%
(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 34.6%
Final simplification34.6%
herbie shell --seed 2023224
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))