
(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 (/ 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 -4000.0)
(/ 1.0 (* x (+ (/ 2.0 x) (/ -1.0 s))))
(if (<= x -2.000000047484456e-33)
(/ 1.0 (/ (- 4.0 (* x (/ (/ x s) s))) (+ 2.0 (/ x s))))
(/ 1.0 (+ 1.0 (/ 1.0 (+ 1.0 (/ x s))))))))
float code(float x, float s) {
float tmp;
if (x <= -4000.0f) {
tmp = 1.0f / (x * ((2.0f / x) + (-1.0f / s)));
} else if (x <= -2.000000047484456e-33f) {
tmp = 1.0f / ((4.0f - (x * ((x / s) / s))) / (2.0f + (x / s)));
} else {
tmp = 1.0f / (1.0f + (1.0f / (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 <= (-4000.0e0)) then
tmp = 1.0e0 / (x * ((2.0e0 / x) + ((-1.0e0) / s)))
else if (x <= (-2.000000047484456e-33)) then
tmp = 1.0e0 / ((4.0e0 - (x * ((x / s) / s))) / (2.0e0 + (x / s)))
else
tmp = 1.0e0 / (1.0e0 + (1.0e0 / (1.0e0 + (x / s))))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-4000.0)) tmp = Float32(Float32(1.0) / Float32(x * Float32(Float32(Float32(2.0) / x) + Float32(Float32(-1.0) / s)))); elseif (x <= Float32(-2.000000047484456e-33)) tmp = Float32(Float32(1.0) / Float32(Float32(Float32(4.0) - Float32(x * Float32(Float32(x / s) / s))) / Float32(Float32(2.0) + Float32(x / s)))); else tmp = Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(x / s))))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-4000.0)) tmp = single(1.0) / (x * ((single(2.0) / x) + (single(-1.0) / s))); elseif (x <= single(-2.000000047484456e-33)) tmp = single(1.0) / ((single(4.0) - (x * ((x / s) / s))) / (single(2.0) + (x / s))); else tmp = single(1.0) / (single(1.0) + (single(1.0) / (single(1.0) + (x / s)))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4000:\\
\;\;\;\;\frac{1}{x \cdot \left(\frac{2}{x} + \frac{-1}{s}\right)}\\
\mathbf{elif}\;x \leq -2.000000047484456 \cdot 10^{-33}:\\
\;\;\;\;\frac{1}{\frac{4 - x \cdot \frac{\frac{x}{s}}{s}}{2 + \frac{x}{s}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + \frac{1}{1 + \frac{x}{s}}}\\
\end{array}
\end{array}
if x < -4e3Initial program 100.0%
Taylor expanded in x around 0 60.5%
mul-1-neg60.5%
unsub-neg60.5%
Simplified60.5%
Taylor expanded in x around inf 60.5%
associate-*r/60.5%
metadata-eval60.5%
Simplified60.5%
if -4e3 < x < -2.00000005e-33Initial program 99.4%
Taylor expanded in x around 0 47.0%
mul-1-neg47.0%
unsub-neg47.0%
Simplified47.0%
sub-neg47.0%
flip-+58.3%
metadata-eval58.3%
distribute-neg-frac258.3%
distribute-neg-frac258.3%
distribute-neg-frac258.3%
Applied egg-rr58.3%
associate-*r/58.3%
add-sqr-sqrt-0.0%
sqrt-unprod64.2%
sqr-neg64.2%
sqrt-unprod56.6%
add-sqr-sqrt56.6%
remove-double-neg56.6%
distribute-neg-frac256.6%
associate-*r/56.6%
distribute-lft-neg-in56.6%
distribute-frac-neg256.6%
remove-double-neg56.6%
distribute-frac-neg256.6%
distribute-frac-neg56.6%
associate-*r/56.6%
Applied egg-rr56.6%
*-commutative56.6%
*-un-lft-identity56.6%
times-frac64.0%
add-sqr-sqrt64.0%
sqrt-unprod61.4%
sqr-neg61.4%
sqrt-unprod-0.0%
add-sqr-sqrt65.7%
Applied egg-rr65.7%
if -2.00000005e-33 < x Initial program 100.0%
distribute-frac-neg100.0%
exp-neg99.9%
Applied egg-rr99.9%
Taylor expanded in x around 0 93.6%
Final simplification79.3%
(FPCore (x s) :precision binary32 (if (<= (- x) -1.0000000031710769e-28) 0.5 (/ 1.0 (* x (+ (/ 2.0 x) (/ -1.0 s))))))
float code(float x, float s) {
float tmp;
if (-x <= -1.0000000031710769e-28f) {
tmp = 0.5f;
} else {
tmp = 1.0f / (x * ((2.0f / x) + (-1.0f / 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-28)) then
tmp = 0.5e0
else
tmp = 1.0e0 / (x * ((2.0e0 / x) + ((-1.0e0) / s)))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (Float32(-x) <= Float32(-1.0000000031710769e-28)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / Float32(x * Float32(Float32(Float32(2.0) / x) + Float32(Float32(-1.0) / s)))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (-x <= single(-1.0000000031710769e-28)) tmp = single(0.5); else tmp = single(1.0) / (x * ((single(2.0) / x) + (single(-1.0) / s))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-x \leq -1.0000000031710769 \cdot 10^{-28}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot \left(\frac{2}{x} + \frac{-1}{s}\right)}\\
\end{array}
\end{array}
if (neg.f32 x) < -1e-28Initial program 99.9%
Taylor expanded in x around 0 34.9%
if -1e-28 < (neg.f32 x) Initial program 99.7%
Taylor expanded in x around 0 60.1%
mul-1-neg60.1%
unsub-neg60.1%
Simplified60.1%
Taylor expanded in x around inf 60.5%
associate-*r/60.5%
metadata-eval60.5%
Simplified60.5%
Final simplification49.6%
(FPCore (x s) :precision binary32 (if (<= x -1.0000000031710769e-28) (/ 1.0 (* x (+ (/ 2.0 x) (/ -1.0 s)))) (/ 1.0 (+ 1.0 (/ 1.0 (+ 1.0 (/ x s)))))))
float code(float x, float s) {
float tmp;
if (x <= -1.0000000031710769e-28f) {
tmp = 1.0f / (x * ((2.0f / x) + (-1.0f / s)));
} else {
tmp = 1.0f / (1.0f + (1.0f / (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 <= (-1.0000000031710769e-28)) then
tmp = 1.0e0 / (x * ((2.0e0 / x) + ((-1.0e0) / s)))
else
tmp = 1.0e0 / (1.0e0 + (1.0e0 / (1.0e0 + (x / s))))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-1.0000000031710769e-28)) tmp = Float32(Float32(1.0) / Float32(x * Float32(Float32(Float32(2.0) / x) + Float32(Float32(-1.0) / s)))); else tmp = Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(x / s))))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-1.0000000031710769e-28)) tmp = single(1.0) / (x * ((single(2.0) / x) + (single(-1.0) / s))); else tmp = single(1.0) / (single(1.0) + (single(1.0) / (single(1.0) + (x / s)))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.0000000031710769 \cdot 10^{-28}:\\
\;\;\;\;\frac{1}{x \cdot \left(\frac{2}{x} + \frac{-1}{s}\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + \frac{1}{1 + \frac{x}{s}}}\\
\end{array}
\end{array}
if x < -1e-28Initial program 99.7%
Taylor expanded in x around 0 51.8%
mul-1-neg51.8%
unsub-neg51.8%
Simplified51.8%
Taylor expanded in x around inf 52.6%
associate-*r/52.6%
metadata-eval52.6%
Simplified52.6%
if -1e-28 < x Initial program 99.9%
distribute-frac-neg99.9%
exp-neg99.9%
Applied egg-rr99.9%
Taylor expanded in x around 0 92.2%
Final simplification75.2%
(FPCore (x s) :precision binary32 (if (<= (- x) -1.0000000031710769e-28) 0.5 (/ 1.0 (- 2.0 (/ x s)))))
float code(float x, float s) {
float tmp;
if (-x <= -1.0000000031710769e-28f) {
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.0000000031710769e-28)) 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.0000000031710769e-28)) 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.0000000031710769e-28)) 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.0000000031710769 \cdot 10^{-28}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\end{array}
\end{array}
if (neg.f32 x) < -1e-28Initial program 99.9%
Taylor expanded in x around 0 34.9%
if -1e-28 < (neg.f32 x) Initial program 99.7%
Taylor expanded in x around 0 60.1%
mul-1-neg60.1%
unsub-neg60.1%
Simplified60.1%
Final simplification49.4%
(FPCore (x s) :precision binary32 (if (<= (- x) 1.0) 0.5 (/ 1.0 (/ x s))))
float code(float x, float s) {
float tmp;
if (-x <= 1.0f) {
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 <= 1.0e0) 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(1.0)) 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(1.0)) 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 1:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (neg.f32 x) < 1Initial program 99.8%
Taylor expanded in x around 0 45.1%
if 1 < (neg.f32 x) Initial program 100.0%
Taylor expanded in x around 0 55.3%
mul-1-neg55.3%
unsub-neg55.3%
Simplified55.3%
Taylor expanded in x around inf 47.7%
associate-*r/47.7%
neg-mul-147.7%
Simplified47.7%
clear-num55.3%
inv-pow55.3%
add-sqr-sqrt-0.0%
sqrt-unprod62.9%
sqr-neg62.9%
sqrt-unprod55.3%
add-sqr-sqrt55.3%
Applied egg-rr55.3%
unpow-155.3%
Simplified55.3%
Final simplification47.7%
(FPCore (x s) :precision binary32 (if (<= (- x) 3.9999998989515007e-5) 0.5 (/ -1.0 (/ x s))))
float code(float x, float s) {
float tmp;
if (-x <= 3.9999998989515007e-5f) {
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 <= 3.9999998989515007e-5) 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(3.9999998989515007e-5)) 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(3.9999998989515007e-5)) 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 3.9999998989515007 \cdot 10^{-5}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\frac{x}{s}}\\
\end{array}
\end{array}
if (neg.f32 x) < 3.9999999e-5Initial program 99.8%
Taylor expanded in x around 0 46.2%
if 3.9999999e-5 < (neg.f32 x) Initial program 100.0%
Taylor expanded in x around 0 51.7%
mul-1-neg51.7%
unsub-neg51.7%
Simplified51.7%
Taylor expanded in x around inf 51.7%
mul-1-neg51.7%
distribute-frac-neg251.7%
Simplified51.7%
Final simplification47.7%
(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 / Float32(-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 51.7%
mul-1-neg51.7%
unsub-neg51.7%
Simplified51.7%
Taylor expanded in x around inf 44.7%
associate-*r/44.7%
neg-mul-144.7%
Simplified44.7%
if -1.99999995e-4 < x Initial program 99.8%
Taylor expanded in x around 0 46.2%
Final simplification45.8%
(FPCore (x s) :precision binary32 (if (<= x -1.0) (/ s x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.0f) {
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 <= (-1.0e0)) 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(-1.0)) 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(-1.0)) tmp = s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;\frac{s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1Initial program 100.0%
Taylor expanded in x around 0 55.3%
mul-1-neg55.3%
unsub-neg55.3%
Simplified55.3%
Taylor expanded in x around inf 47.7%
associate-*r/47.7%
neg-mul-147.7%
Simplified47.7%
div-inv47.7%
add-sqr-sqrt-0.0%
sqrt-unprod61.1%
sqr-neg61.1%
sqrt-unprod47.7%
add-sqr-sqrt47.7%
Applied egg-rr47.7%
associate-*r/47.7%
*-rgt-identity47.7%
Simplified47.7%
if -1 < x Initial program 99.8%
Taylor expanded in x around 0 45.1%
Final simplification45.8%
(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 35.3%
Final simplification35.3%
herbie shell --seed 2024078
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))