
(FPCore (x s) :precision binary32 :pre (and (<= 0.0 s) (<= s 1.0651631)) (/ 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)
use fmin_fmax_functions
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
\frac{1}{1 + e^{\frac{-x}{s}}}
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x s) :precision binary32 :pre (and (<= 0.0 s) (<= s 1.0651631)) (/ 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)
use fmin_fmax_functions
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
\frac{1}{1 + e^{\frac{-x}{s}}}
(FPCore (x s) :precision binary32 :pre (and (<= 0.0 s) (<= s 1.0651631)) (if (<= (/ (- x) s) 50.0) (/ 1.0 (+ 1.0 (/ 1.0 (+ 1.0 (/ x s))))) (/ 0.0 2.0)))
float code(float x, float s) {
float tmp;
if ((-x / s) <= 50.0f) {
tmp = 1.0f / (1.0f + (1.0f / (1.0f + (x / s))));
} else {
tmp = 0.0f / 2.0f;
}
return tmp;
}
real(4) function code(x, s)
use fmin_fmax_functions
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if ((-x / s) <= 50.0e0) then
tmp = 1.0e0 / (1.0e0 + (1.0e0 / (1.0e0 + (x / s))))
else
tmp = 0.0e0 / 2.0e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(Float32(-x) / s) <= Float32(50.0)) tmp = Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(x / s))))); else tmp = Float32(Float32(0.0) / Float32(2.0)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if ((-x / s) <= single(50.0)) tmp = single(1.0) / (single(1.0) + (single(1.0) / (single(1.0) + (x / s)))); else tmp = single(0.0) / single(2.0); end tmp_2 = tmp; end
\begin{array}{l}
\mathbf{if}\;\frac{-x}{s} \leq 50:\\
\;\;\;\;\frac{1}{1 + \frac{1}{1 + \frac{x}{s}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{0}{2}\\
\end{array}
if (/.f32 (neg.f32 x) s) < 50Initial program 99.8%
Applied rewrites99.8%
Taylor expanded in x around 0
Applied rewrites61.9%
if 50 < (/.f32 (neg.f32 x) s) Initial program 99.8%
Taylor expanded in x around 0
Applied rewrites41.1%
Taylor expanded in undef-var around zero
Applied rewrites40.0%
Taylor expanded in x around 0
Applied rewrites40.0%
(FPCore (x s) :precision binary32 :pre (and (<= 0.0 s) (<= s 1.0651631)) (if (<= (/ 1.0 (+ 1.0 (exp (/ (- x) s)))) 2.000000093402204e-34) (/ 0.0 2.0) (/ (* 0.5 s) s)))
float code(float x, float s) {
float tmp;
if ((1.0f / (1.0f + expf((-x / s)))) <= 2.000000093402204e-34f) {
tmp = 0.0f / 2.0f;
} else {
tmp = (0.5f * s) / s;
}
return tmp;
}
real(4) function code(x, s)
use fmin_fmax_functions
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if ((1.0e0 / (1.0e0 + exp((-x / s)))) <= 2.000000093402204e-34) then
tmp = 0.0e0 / 2.0e0
else
tmp = (0.5e0 * s) / s
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) <= Float32(2.000000093402204e-34)) tmp = Float32(Float32(0.0) / Float32(2.0)); else tmp = Float32(Float32(Float32(0.5) * s) / s); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if ((single(1.0) / (single(1.0) + exp((-x / s)))) <= single(2.000000093402204e-34)) tmp = single(0.0) / single(2.0); else tmp = (single(0.5) * s) / s; end tmp_2 = tmp; end
\begin{array}{l}
\mathbf{if}\;\frac{1}{1 + e^{\frac{-x}{s}}} \leq 2.000000093402204 \cdot 10^{-34}:\\
\;\;\;\;\frac{0}{2}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot s}{s}\\
\end{array}
if (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s)))) < 2.00000009e-34Initial program 99.8%
Taylor expanded in x around 0
Applied rewrites41.1%
Taylor expanded in undef-var around zero
Applied rewrites40.0%
Taylor expanded in x around 0
Applied rewrites40.0%
if 2.00000009e-34 < (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s)))) Initial program 99.8%
Taylor expanded in x around 0
Applied rewrites30.1%
Taylor expanded in s around 0
Applied rewrites30.0%
Taylor expanded in x around 0
Applied rewrites35.7%
(FPCore (x s) :precision binary32 :pre (and (<= 0.0 s) (<= s 1.0651631)) (if (<= (/ 1.0 (+ 1.0 (exp (/ (- x) s)))) 1.8755399224397163e-34) (/ 0.0 2.0) 0.5))
float code(float x, float s) {
float tmp;
if ((1.0f / (1.0f + expf((-x / s)))) <= 1.8755399224397163e-34f) {
tmp = 0.0f / 2.0f;
} else {
tmp = 0.5f;
}
return tmp;
}
real(4) function code(x, s)
use fmin_fmax_functions
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if ((1.0e0 / (1.0e0 + exp((-x / s)))) <= 1.8755399224397163e-34) then
tmp = 0.0e0 / 2.0e0
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) <= Float32(1.8755399224397163e-34)) tmp = Float32(Float32(0.0) / Float32(2.0)); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if ((single(1.0) / (single(1.0) + exp((-x / s)))) <= single(1.8755399224397163e-34)) tmp = single(0.0) / single(2.0); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\mathbf{if}\;\frac{1}{1 + e^{\frac{-x}{s}}} \leq 1.8755399224397163 \cdot 10^{-34}:\\
\;\;\;\;\frac{0}{2}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
if (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s)))) < 1.87553992e-34Initial program 99.8%
Taylor expanded in x around 0
Applied rewrites41.1%
Taylor expanded in undef-var around zero
Applied rewrites40.0%
Taylor expanded in x around 0
Applied rewrites40.0%
if 1.87553992e-34 < (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 x) s)))) Initial program 99.8%
Taylor expanded in x around 0
Applied rewrites35.7%
(FPCore (x s) :precision binary32 :pre (and (<= 0.0 s) (<= s 1.0651631)) 0.5)
float code(float x, float s) {
return 0.5f;
}
real(4) function code(x, s)
use fmin_fmax_functions
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
0.5
Initial program 99.8%
Taylor expanded in x around 0
Applied rewrites35.7%
herbie shell --seed 2026084
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))