
(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 11 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 (/ 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) 1.5999999674579037e-30)
0.5
(if (<= (- x) 9.0)
(/ 1.0 (* x (* (- (* s 2.0) x) (/ 1.0 (* x s)))))
(/ 1.0 (/ (* x (- 2.0 (/ x s))) x)))))
float code(float x, float s) {
float tmp;
if (-x <= 1.5999999674579037e-30f) {
tmp = 0.5f;
} else if (-x <= 9.0f) {
tmp = 1.0f / (x * (((s * 2.0f) - x) * (1.0f / (x * s))));
} else {
tmp = 1.0f / ((x * (2.0f - (x / s))) / x);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (-x <= 1.5999999674579037e-30) then
tmp = 0.5e0
else if (-x <= 9.0e0) then
tmp = 1.0e0 / (x * (((s * 2.0e0) - x) * (1.0e0 / (x * s))))
else
tmp = 1.0e0 / ((x * (2.0e0 - (x / s))) / x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(1.5999999674579037e-30)) tmp = Float32(0.5); elseif (Float32(-x) <= Float32(9.0)) tmp = Float32(Float32(1.0) / Float32(x * Float32(Float32(Float32(s * Float32(2.0)) - x) * Float32(Float32(1.0) / Float32(x * s))))); else tmp = Float32(Float32(1.0) / Float32(Float32(x * Float32(Float32(2.0) - Float32(x / s))) / x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(1.5999999674579037e-30)) tmp = single(0.5); elseif (-x <= single(9.0)) tmp = single(1.0) / (x * (((s * single(2.0)) - x) * (single(1.0) / (x * s)))); else tmp = single(1.0) / ((x * (single(2.0) - (x / s))) / x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq 1.5999999674579037 \cdot 10^{-30}:\\
\;\;\;\;0.5\\
\mathbf{elif}\;-x \leq 9:\\
\;\;\;\;\frac{1}{x \cdot \left(\left(s \cdot 2 - x\right) \cdot \frac{1}{x \cdot s}\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x \cdot \left(2 - \frac{x}{s}\right)}{x}}\\
\end{array}
\end{array}
if (neg.f32 x) < 1.59999997e-30Initial program 99.8%
Taylor expanded in x around 0 49.0%
if 1.59999997e-30 < (neg.f32 x) < 9Initial program 99.5%
Taylor expanded in x around 0 41.9%
mul-1-neg41.9%
unsub-neg41.9%
Simplified41.9%
Taylor expanded in x around inf 41.8%
associate-*r/41.8%
metadata-eval41.8%
Simplified41.8%
frac-sub57.1%
div-inv64.0%
*-rgt-identity64.0%
Applied egg-rr64.0%
if 9 < (neg.f32 x) Initial program 100.0%
Taylor expanded in x around 0 52.4%
mul-1-neg52.4%
unsub-neg52.4%
Simplified52.4%
Taylor expanded in x around inf 52.4%
associate-*r/52.4%
metadata-eval52.4%
Simplified52.4%
Taylor expanded in x around 0 52.4%
mul-1-neg52.4%
unsub-neg52.4%
Simplified52.4%
associate-*r/72.3%
Applied egg-rr72.3%
Final simplification57.2%
(FPCore (x s) :precision binary32 (if (<= (- x) 4.999999999099794e-24) 0.5 (* s (/ x (* x (- (* s 2.0) x))))))
float code(float x, float s) {
float tmp;
if (-x <= 4.999999999099794e-24f) {
tmp = 0.5f;
} else {
tmp = s * (x / (x * ((s * 2.0f) - x)));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (-x <= 4.999999999099794e-24) then
tmp = 0.5e0
else
tmp = s * (x / (x * ((s * 2.0e0) - x)))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(4.999999999099794e-24)) tmp = Float32(0.5); else tmp = Float32(s * Float32(x / Float32(x * Float32(Float32(s * Float32(2.0)) - x)))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(4.999999999099794e-24)) tmp = single(0.5); else tmp = s * (x / (x * ((s * single(2.0)) - x))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq 4.999999999099794 \cdot 10^{-24}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;s \cdot \frac{x}{x \cdot \left(s \cdot 2 - x\right)}\\
\end{array}
\end{array}
if (neg.f32 x) < 5e-24Initial program 99.8%
Taylor expanded in x around 0 49.0%
if 5e-24 < (neg.f32 x) Initial program 99.8%
Taylor expanded in x around 0 47.8%
mul-1-neg47.8%
unsub-neg47.8%
Simplified47.8%
Taylor expanded in x around inf 47.8%
associate-*r/47.8%
metadata-eval47.8%
Simplified47.8%
associate-/r*44.4%
frac-sub47.1%
associate-/r/49.2%
*-rgt-identity49.2%
Applied egg-rr49.2%
associate-*r*47.3%
*-commutative47.3%
associate-/l/50.6%
associate-*l/51.5%
*-commutative51.5%
*-rgt-identity51.5%
*-commutative51.5%
Simplified51.5%
Final simplification49.9%
(FPCore (x s) :precision binary32 (if (<= (- x) -1.9999999593223797e-31) 0.5 (* x (/ 1.0 (* x (- 2.0 (/ x s)))))))
float code(float x, float s) {
float tmp;
if (-x <= -1.9999999593223797e-31f) {
tmp = 0.5f;
} else {
tmp = x * (1.0f / (x * (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 <= (-1.9999999593223797e-31)) then
tmp = 0.5e0
else
tmp = x * (1.0e0 / (x * (2.0e0 - (x / s))))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(-1.9999999593223797e-31)) tmp = Float32(0.5); else tmp = Float32(x * Float32(Float32(1.0) / Float32(x * Float32(Float32(2.0) - Float32(x / s))))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(-1.9999999593223797e-31)) tmp = single(0.5); else tmp = x * (single(1.0) / (x * (single(2.0) - (x / s)))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq -1.9999999593223797 \cdot 10^{-31}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;x \cdot \frac{1}{x \cdot \left(2 - \frac{x}{s}\right)}\\
\end{array}
\end{array}
if (neg.f32 x) < -1.99999996e-31Initial program 99.9%
Taylor expanded in x around 0 38.7%
if -1.99999996e-31 < (neg.f32 x) Initial program 99.7%
Taylor expanded in x around 0 57.8%
mul-1-neg57.8%
unsub-neg57.8%
Simplified57.8%
Taylor expanded in x around inf 57.6%
associate-*r/57.6%
metadata-eval57.6%
Simplified57.6%
Taylor expanded in x around 0 57.6%
mul-1-neg57.6%
unsub-neg57.6%
Simplified57.6%
associate-*r/66.7%
associate-/r/66.5%
Applied egg-rr66.5%
Final simplification53.6%
(FPCore (x s) :precision binary32 (if (<= (- x) -1.9999999593223797e-31) 0.5 (/ 1.0 (/ (* x (- 2.0 (/ x s))) x))))
float code(float x, float s) {
float tmp;
if (-x <= -1.9999999593223797e-31f) {
tmp = 0.5f;
} else {
tmp = 1.0f / ((x * (2.0f - (x / s))) / x);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (-x <= (-1.9999999593223797e-31)) then
tmp = 0.5e0
else
tmp = 1.0e0 / ((x * (2.0e0 - (x / s))) / x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(-1.9999999593223797e-31)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / Float32(Float32(x * Float32(Float32(2.0) - Float32(x / s))) / x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(-1.9999999593223797e-31)) tmp = single(0.5); else tmp = single(1.0) / ((x * (single(2.0) - (x / s))) / x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq -1.9999999593223797 \cdot 10^{-31}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x \cdot \left(2 - \frac{x}{s}\right)}{x}}\\
\end{array}
\end{array}
if (neg.f32 x) < -1.99999996e-31Initial program 99.9%
Taylor expanded in x around 0 38.7%
if -1.99999996e-31 < (neg.f32 x) Initial program 99.7%
Taylor expanded in x around 0 57.8%
mul-1-neg57.8%
unsub-neg57.8%
Simplified57.8%
Taylor expanded in x around inf 57.6%
associate-*r/57.6%
metadata-eval57.6%
Simplified57.6%
Taylor expanded in x around 0 57.6%
mul-1-neg57.6%
unsub-neg57.6%
Simplified57.6%
associate-*r/66.7%
Applied egg-rr66.7%
Final simplification53.7%
(FPCore (x s) :precision binary32 (if (<= (- x) -2.0000000063421537e-28) 0.5 (/ 1.0 (/ (- (* s 2.0) x) s))))
float code(float x, float s) {
float tmp;
if (-x <= -2.0000000063421537e-28f) {
tmp = 0.5f;
} else {
tmp = 1.0f / (((s * 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 <= (-2.0000000063421537e-28)) then
tmp = 0.5e0
else
tmp = 1.0e0 / (((s * 2.0e0) - x) / s)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(-2.0000000063421537e-28)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / Float32(Float32(Float32(s * Float32(2.0)) - x) / s)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(-2.0000000063421537e-28)) tmp = single(0.5); else tmp = single(1.0) / (((s * single(2.0)) - x) / s); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq -2.0000000063421537 \cdot 10^{-28}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{s \cdot 2 - x}{s}}\\
\end{array}
\end{array}
if (neg.f32 x) < -2.00000001e-28Initial program 99.9%
Taylor expanded in x around 0 37.1%
if -2.00000001e-28 < (neg.f32 x) Initial program 99.8%
Taylor expanded in x around 0 58.7%
mul-1-neg58.7%
unsub-neg58.7%
Simplified58.7%
Taylor expanded in s around 0 58.7%
*-commutative58.7%
Simplified58.7%
Final simplification48.9%
(FPCore (x s) :precision binary32 (if (<= (- x) -1.9999999593223797e-31) 0.5 (/ 1.0 (- 2.0 (/ x s)))))
float code(float x, float s) {
float tmp;
if (-x <= -1.9999999593223797e-31f) {
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 <= (-1.9999999593223797e-31)) 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(-x) <= Float32(-1.9999999593223797e-31)) 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 <= single(-1.9999999593223797e-31)) 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}\;-x \leq -1.9999999593223797 \cdot 10^{-31}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\end{array}
\end{array}
if (neg.f32 x) < -1.99999996e-31Initial program 99.9%
Taylor expanded in x around 0 38.7%
if -1.99999996e-31 < (neg.f32 x) Initial program 99.7%
Taylor expanded in x around 0 57.8%
mul-1-neg57.8%
unsub-neg57.8%
Simplified57.8%
Final simplification48.9%
(FPCore (x s) :precision binary32 (if (<= (- x) 9.999999747378752e-6) 0.5 (* s (/ 1.0 x))))
float code(float x, float s) {
float tmp;
if (-x <= 9.999999747378752e-6f) {
tmp = 0.5f;
} else {
tmp = s * (1.0f / x);
}
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-6) then
tmp = 0.5e0
else
tmp = s * (1.0e0 / x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(9.999999747378752e-6)) tmp = Float32(0.5); else tmp = Float32(s * Float32(Float32(1.0) / x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(9.999999747378752e-6)) tmp = single(0.5); else tmp = s * (single(1.0) / x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq 9.999999747378752 \cdot 10^{-6}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;s \cdot \frac{1}{x}\\
\end{array}
\end{array}
if (neg.f32 x) < 9.99999975e-6Initial program 99.7%
Taylor expanded in x around 0 47.4%
if 9.99999975e-6 < (neg.f32 x) Initial program 100.0%
Taylor expanded in x around 0 49.3%
mul-1-neg49.3%
unsub-neg49.3%
Simplified49.3%
Taylor expanded in x around inf 49.3%
neg-mul-149.3%
distribute-neg-frac249.3%
Simplified49.3%
associate-/r/44.6%
add-sqr-sqrt-0.0%
sqrt-unprod57.8%
sqr-neg57.8%
sqrt-unprod44.6%
add-sqr-sqrt44.6%
Applied egg-rr44.6%
Final simplification46.7%
(FPCore (x s) :precision binary32 (if (<= (- x) 9.999999747378752e-6) 0.5 (/ -1.0 (/ x s))))
float code(float x, float s) {
float tmp;
if (-x <= 9.999999747378752e-6f) {
tmp = 0.5f;
} else {
tmp = -1.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 <= 9.999999747378752e-6) then
tmp = 0.5e0
else
tmp = (-1.0e0) / (x / s)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(9.999999747378752e-6)) tmp = Float32(0.5); else tmp = Float32(Float32(-1.0) / Float32(x / s)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(9.999999747378752e-6)) tmp = single(0.5); else tmp = single(-1.0) / (x / s); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq 9.999999747378752 \cdot 10^{-6}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (neg.f32 x) < 9.99999975e-6Initial program 99.7%
Taylor expanded in x around 0 47.4%
if 9.99999975e-6 < (neg.f32 x) Initial program 100.0%
Taylor expanded in x around 0 49.3%
mul-1-neg49.3%
unsub-neg49.3%
Simplified49.3%
Taylor expanded in x around inf 49.3%
neg-mul-149.3%
distribute-neg-frac249.3%
Simplified49.3%
Final simplification47.9%
(FPCore (x s) :precision binary32 (if (<= x -0.00019999999494757503) (/ s x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -0.00019999999494757503f) {
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 <= (-0.00019999999494757503e0)) 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(-0.00019999999494757503)) 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(-0.00019999999494757503)) tmp = s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.00019999999494757503:\\
\;\;\;\;\frac{s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.99999995e-4Initial program 100.0%
Taylor expanded in x around 0 49.3%
mul-1-neg49.3%
unsub-neg49.3%
Simplified49.3%
Taylor expanded in x around inf 49.3%
neg-mul-149.3%
distribute-neg-frac249.3%
Simplified49.3%
clear-num44.6%
add-sqr-sqrt-0.0%
sqrt-unprod57.8%
sqr-neg57.8%
sqrt-unprod44.6%
add-sqr-sqrt44.6%
*-un-lft-identity44.6%
Applied egg-rr44.6%
*-lft-identity44.6%
Simplified44.6%
if -1.99999995e-4 < x Initial program 99.7%
Taylor expanded in x around 0 47.4%
Final simplification46.7%
(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 36.7%
Final simplification36.7%
herbie shell --seed 2024112
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))