
(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 8 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%
(FPCore (x s) :precision binary32 (+ 1.0 (exp (/ (- x) s))))
float code(float x, float s) {
return 1.0f + expf((-x / s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 + exp((-x / s))
end function
function code(x, s) return Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) end
function tmp = code(x, s) tmp = single(1.0) + exp((-x / s)); end
\begin{array}{l}
\\
1 + e^{\frac{-x}{s}}
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites44.2%
(FPCore (x s) :precision binary32 (if (<= x 0.4000000059604645) (+ 1.0 (/ 1.0 (/ 1.0 (- x)))) (+ 1.0 (exp (- x)))))
float code(float x, float s) {
float tmp;
if (x <= 0.4000000059604645f) {
tmp = 1.0f + (1.0f / (1.0f / -x));
} else {
tmp = 1.0f + expf(-x);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 0.4000000059604645e0) then
tmp = 1.0e0 + (1.0e0 / (1.0e0 / -x))
else
tmp = 1.0e0 + exp(-x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.4000000059604645)) tmp = Float32(Float32(1.0) + Float32(Float32(1.0) / Float32(Float32(1.0) / Float32(-x)))); else tmp = Float32(Float32(1.0) + exp(Float32(-x))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.4000000059604645)) tmp = single(1.0) + (single(1.0) / (single(1.0) / -x)); else tmp = single(1.0) + exp(-x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.4000000059604645:\\
\;\;\;\;1 + \frac{1}{\frac{1}{-x}}\\
\mathbf{else}:\\
\;\;\;\;1 + e^{-x}\\
\end{array}
\end{array}
if x < 0.400000006Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites28.2%
Taylor expanded in x around 0
Applied rewrites5.2%
Taylor expanded in x around 0
Applied rewrites28.0%
if 0.400000006 < x Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites100.0%
(FPCore (x s) :precision binary32 (let* ((t_0 (/ 1.0 (- x)))) (if (<= x 0.4000000059604645) (+ 1.0 (/ 1.0 t_0)) (+ 1.0 t_0))))
float code(float x, float s) {
float t_0 = 1.0f / -x;
float tmp;
if (x <= 0.4000000059604645f) {
tmp = 1.0f + (1.0f / t_0);
} 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 = 1.0e0 / -x
if (x <= 0.4000000059604645e0) then
tmp = 1.0e0 + (1.0e0 / t_0)
else
tmp = 1.0e0 + t_0
end if
code = tmp
end function
function code(x, s) t_0 = Float32(Float32(1.0) / Float32(-x)) tmp = Float32(0.0) if (x <= Float32(0.4000000059604645)) tmp = Float32(Float32(1.0) + Float32(Float32(1.0) / t_0)); else tmp = Float32(Float32(1.0) + t_0); end return tmp end
function tmp_2 = code(x, s) t_0 = single(1.0) / -x; tmp = single(0.0); if (x <= single(0.4000000059604645)) tmp = single(1.0) + (single(1.0) / t_0); else tmp = single(1.0) + t_0; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{-x}\\
\mathbf{if}\;x \leq 0.4000000059604645:\\
\;\;\;\;1 + \frac{1}{t\_0}\\
\mathbf{else}:\\
\;\;\;\;1 + t\_0\\
\end{array}
\end{array}
if x < 0.400000006Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites28.2%
Taylor expanded in x around 0
Applied rewrites5.2%
Taylor expanded in x around 0
Applied rewrites28.0%
if 0.400000006 < x Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites96.4%
(FPCore (x s) :precision binary32 (if (<= x 1.0000000116860974e-7) (+ 1.0 (/ (- x) s)) (+ 1.0 (/ 1.0 (- x)))))
float code(float x, float s) {
float tmp;
if (x <= 1.0000000116860974e-7f) {
tmp = 1.0f + (-x / s);
} else {
tmp = 1.0f + (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 <= 1.0000000116860974e-7) then
tmp = 1.0e0 + (-x / s)
else
tmp = 1.0e0 + (1.0e0 / -x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.0000000116860974e-7)) tmp = Float32(Float32(1.0) + Float32(Float32(-x) / s)); else tmp = Float32(Float32(1.0) + Float32(Float32(1.0) / Float32(-x))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.0000000116860974e-7)) tmp = single(1.0) + (-x / s); else tmp = single(1.0) + (single(1.0) / -x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.0000000116860974 \cdot 10^{-7}:\\
\;\;\;\;1 + \frac{-x}{s}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{1}{-x}\\
\end{array}
\end{array}
if x < 1.00000001e-7Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites23.2%
Taylor expanded in x around 0
Applied rewrites12.2%
if 1.00000001e-7 < x Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites79.0%
(FPCore (x s) :precision binary32 (+ 1.0 (/ (- x) s)))
float code(float x, float s) {
return 1.0f + (-x / s);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 + (-x / s)
end function
function code(x, s) return Float32(Float32(1.0) + Float32(Float32(-x) / s)) end
function tmp = code(x, s) tmp = single(1.0) + (-x / s); end
\begin{array}{l}
\\
1 + \frac{-x}{s}
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites44.2%
Taylor expanded in x around 0
Applied rewrites9.3%
(FPCore (x s) :precision binary32 (/ 1.0 (- x)))
float code(float x, float s) {
return 1.0f / -x;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / -x
end function
function code(x, s) return Float32(Float32(1.0) / Float32(-x)) end
function tmp = code(x, s) tmp = single(1.0) / -x; end
\begin{array}{l}
\\
\frac{1}{-x}
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites6.1%
(FPCore (x s) :precision binary32 (- x))
float code(float x, float s) {
return -x;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = -x
end function
function code(x, s) return Float32(-x) end
function tmp = code(x, s) tmp = -x; end
\begin{array}{l}
\\
-x
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
Applied rewrites10.6%
Taylor expanded in x around 0
Applied rewrites5.0%
herbie shell --seed 2024321
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))