
(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 9 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.9%
Final simplification99.9%
(FPCore (x s) :precision binary32 (if (<= x -9.999999796611898e-32) (/ 1.0 (+ 2.0 (* x (/ (+ (* x -0.5) (/ s (/ x x))) (* s (- s)))))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -9.999999796611898e-32f) {
tmp = 1.0f / (2.0f + (x * (((x * -0.5f) + (s / (x / x))) / (s * -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 <= (-9.999999796611898e-32)) then
tmp = 1.0e0 / (2.0e0 + (x * (((x * (-0.5e0)) + (s / (x / x))) / (s * -s))))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-9.999999796611898e-32)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(x * Float32(Float32(Float32(x * Float32(-0.5)) + Float32(s / Float32(x / x))) / Float32(s * Float32(-s)))))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-9.999999796611898e-32)) tmp = single(1.0) / (single(2.0) + (x * (((x * single(-0.5)) + (s / (x / x))) / (s * -s)))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.999999796611898 \cdot 10^{-32}:\\
\;\;\;\;\frac{1}{2 + x \cdot \frac{x \cdot -0.5 + \frac{s}{\frac{x}{x}}}{s \cdot \left(-s\right)}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -9.9999998e-32Initial program 99.9%
Taylor expanded in x around 0 73.8%
mul-1-neg73.8%
unsub-neg73.8%
unpow273.8%
unpow273.8%
times-frac68.2%
Simplified68.2%
associate-*l/68.2%
associate-/l*71.7%
Applied egg-rr71.7%
associate-*r/71.7%
associate-/r/71.7%
times-frac67.7%
clear-num67.7%
div-inv67.7%
frac-2neg67.7%
frac-sub71.7%
div-inv71.7%
clear-num71.7%
distribute-rgt-neg-in71.7%
associate-/r/71.7%
div-inv71.7%
clear-num71.7%
distribute-rgt-neg-in71.7%
distribute-neg-frac71.7%
Applied egg-rr71.7%
associate-*r/77.1%
associate-/r/78.3%
sub-neg78.3%
distribute-rgt-neg-out78.3%
*-commutative78.3%
distribute-rgt-neg-in78.3%
*-commutative78.3%
distribute-rgt-neg-in78.3%
metadata-eval78.3%
distribute-rgt-neg-out78.3%
remove-double-neg78.3%
associate-*l/78.3%
associate-/l*78.3%
Simplified78.3%
Taylor expanded in s around 0 78.3%
*-commutative78.3%
Simplified78.3%
if -9.9999998e-32 < x Initial program 99.9%
Taylor expanded in x around 0 44.7%
Final simplification60.9%
(FPCore (x s) :precision binary32 (if (<= x -9.999999796611898e-32) (/ 1.0 (+ 2.0 (* x (- (/ (* x 0.5) (* s s)) (/ 1.0 s))))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -9.999999796611898e-32f) {
tmp = 1.0f / (2.0f + (x * (((x * 0.5f) / (s * s)) - (1.0f / 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 <= (-9.999999796611898e-32)) then
tmp = 1.0e0 / (2.0e0 + (x * (((x * 0.5e0) / (s * s)) - (1.0e0 / s))))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-9.999999796611898e-32)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(x * Float32(Float32(Float32(x * Float32(0.5)) / Float32(s * s)) - Float32(Float32(1.0) / s))))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-9.999999796611898e-32)) tmp = single(1.0) / (single(2.0) + (x * (((x * single(0.5)) / (s * s)) - (single(1.0) / s)))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.999999796611898 \cdot 10^{-32}:\\
\;\;\;\;\frac{1}{2 + x \cdot \left(\frac{x \cdot 0.5}{s \cdot s} - \frac{1}{s}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -9.9999998e-32Initial program 99.9%
Taylor expanded in x around 0 73.8%
mul-1-neg73.8%
unsub-neg73.8%
unpow273.8%
unpow273.8%
times-frac68.2%
Simplified68.2%
associate-*l/68.2%
associate-/l*71.7%
Applied egg-rr71.7%
associate-*r/71.7%
associate-/r/71.7%
times-frac67.7%
clear-num67.7%
div-inv67.7%
frac-2neg67.7%
frac-sub71.7%
div-inv71.7%
clear-num71.7%
distribute-rgt-neg-in71.7%
associate-/r/71.7%
div-inv71.7%
clear-num71.7%
distribute-rgt-neg-in71.7%
distribute-neg-frac71.7%
Applied egg-rr71.7%
associate-*r/77.1%
associate-/r/78.3%
sub-neg78.3%
distribute-rgt-neg-out78.3%
*-commutative78.3%
distribute-rgt-neg-in78.3%
*-commutative78.3%
distribute-rgt-neg-in78.3%
metadata-eval78.3%
distribute-rgt-neg-out78.3%
remove-double-neg78.3%
associate-*l/78.3%
associate-/l*78.3%
Simplified78.3%
Taylor expanded in s around 0 78.3%
associate-*r/78.3%
*-commutative78.3%
unpow278.3%
Simplified78.3%
if -9.9999998e-32 < x Initial program 99.9%
Taylor expanded in x around 0 44.7%
Final simplification60.9%
(FPCore (x s) :precision binary32 (if (<= x -9.999999796611898e-32) (/ 1.0 (+ 2.0 (* 0.5 (* x (/ x (* s s)))))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -9.999999796611898e-32f) {
tmp = 1.0f / (2.0f + (0.5f * (x * (x / (s * 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 <= (-9.999999796611898e-32)) then
tmp = 1.0e0 / (2.0e0 + (0.5e0 * (x * (x / (s * s)))))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-9.999999796611898e-32)) tmp = Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(Float32(0.5) * Float32(x * Float32(x / Float32(s * s)))))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-9.999999796611898e-32)) tmp = single(1.0) / (single(2.0) + (single(0.5) * (x * (x / (s * s))))); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.999999796611898 \cdot 10^{-32}:\\
\;\;\;\;\frac{1}{2 + 0.5 \cdot \left(x \cdot \frac{x}{s \cdot s}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -9.9999998e-32Initial program 99.9%
Taylor expanded in x around 0 73.8%
mul-1-neg73.8%
unsub-neg73.8%
unpow273.8%
unpow273.8%
times-frac68.2%
Simplified68.2%
associate-*l/68.2%
associate-/l*71.7%
Applied egg-rr71.7%
Taylor expanded in x around inf 72.9%
unpow272.9%
unpow272.9%
associate-*r/77.4%
Simplified77.4%
if -9.9999998e-32 < x Initial program 99.9%
Taylor expanded in x around 0 44.7%
Final simplification60.4%
(FPCore (x s) :precision binary32 (if (<= x -5.0000000843119176e-17) (* 2.0 (* (/ s x) (/ s x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -5.0000000843119176e-17f) {
tmp = 2.0f * ((s / x) * (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 <= (-5.0000000843119176e-17)) then
tmp = 2.0e0 * ((s / x) * (s / x))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-5.0000000843119176e-17)) tmp = Float32(Float32(2.0) * Float32(Float32(s / x) * 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(-5.0000000843119176e-17)) tmp = single(2.0) * ((s / x) * (s / x)); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5.0000000843119176 \cdot 10^{-17}:\\
\;\;\;\;2 \cdot \left(\frac{s}{x} \cdot \frac{s}{x}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -5.00000008e-17Initial program 100.0%
Taylor expanded in x around 0 75.3%
mul-1-neg75.3%
unsub-neg75.3%
unpow275.3%
unpow275.3%
times-frac71.2%
Simplified71.2%
Taylor expanded in x around inf 70.6%
unpow270.6%
unpow270.6%
times-frac64.1%
Simplified64.1%
if -5.00000008e-17 < x Initial program 99.9%
Taylor expanded in x around 0 44.6%
Final simplification52.6%
(FPCore (x s) :precision binary32 (if (<= x -1.000000031374395e-22) (* 2.0 (/ (* s s) (* x x))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -1.000000031374395e-22f) {
tmp = 2.0f * ((s * s) / (x * 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.000000031374395e-22)) then
tmp = 2.0e0 * ((s * s) / (x * x))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-1.000000031374395e-22)) tmp = Float32(Float32(2.0) * Float32(Float32(s * s) / Float32(x * x))); else tmp = Float32(0.5); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-1.000000031374395e-22)) tmp = single(2.0) * ((s * s) / (x * x)); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.000000031374395 \cdot 10^{-22}:\\
\;\;\;\;2 \cdot \frac{s \cdot s}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.00000003e-22Initial program 99.9%
Taylor expanded in x around 0 75.9%
mul-1-neg75.9%
unsub-neg75.9%
unpow275.9%
unpow275.9%
times-frac68.9%
Simplified68.9%
associate-*l/68.9%
associate-/l*71.9%
Applied egg-rr71.9%
Taylor expanded in x around inf 69.7%
unpow269.7%
unpow269.7%
Simplified69.7%
if -1.00000003e-22 < x Initial program 99.9%
Taylor expanded in x around 0 45.5%
Final simplification56.2%
(FPCore (x s) :precision binary32 (if (<= x 1.000000023742228e-32) (/ 1.0 (- 2.0 (/ x s))) 0.5))
float code(float x, float s) {
float tmp;
if (x <= 1.000000023742228e-32f) {
tmp = 1.0f / (2.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 <= 1.000000023742228e-32) then
tmp = 1.0e0 / (2.0e0 - (x / s))
else
tmp = 0.5e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.000000023742228e-32)) tmp = Float32(Float32(1.0) / Float32(Float32(2.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(1.000000023742228e-32)) tmp = single(1.0) / (single(2.0) - (x / s)); else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.000000023742228 \cdot 10^{-32}:\\
\;\;\;\;\frac{1}{2 - \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < 1.00000002e-32Initial program 99.9%
Taylor expanded in x around 0 45.0%
mul-1-neg45.0%
unsub-neg45.0%
Simplified45.0%
if 1.00000002e-32 < x Initial program 99.9%
Taylor expanded in x around 0 38.1%
Final simplification41.9%
(FPCore (x s) :precision binary32 (if (<= x -2.000000026702864e-10) (/ (- s) x) 0.5))
float code(float x, float s) {
float tmp;
if (x <= -2.000000026702864e-10f) {
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.000000026702864e-10)) 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.000000026702864e-10)) 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.000000026702864e-10)) tmp = -s / x; else tmp = single(0.5); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.000000026702864 \cdot 10^{-10}:\\
\;\;\;\;\frac{-s}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -2.00000003e-10Initial program 100.0%
Taylor expanded in x around 0 38.5%
mul-1-neg38.5%
unsub-neg38.5%
Simplified38.5%
Taylor expanded in x around inf 37.6%
associate-*r/37.6%
neg-mul-137.6%
Simplified37.6%
if -2.00000003e-10 < x Initial program 99.9%
Taylor expanded in x around 0 42.6%
Final simplification40.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.9%
Taylor expanded in x around 0 29.6%
Final simplification29.6%
herbie shell --seed 2023238
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))