
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0))) (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = 1.0f + t_0;
return t_0 / ((s * t_1) * t_1);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: t_1
t_0 = exp((-abs(x) / s))
t_1 = 1.0e0 + t_0
code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) t_1 = Float32(Float32(1.0) + t_0) return Float32(t_0 / Float32(Float32(s * t_1) * t_1)) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); t_1 = single(1.0) + t_0; tmp = t_0 / ((s * t_1) * t_1); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t_0\\
\frac{t_0}{\left(s \cdot t_1\right) \cdot t_1}
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0))) (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = 1.0f + t_0;
return t_0 / ((s * t_1) * t_1);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: t_1
t_0 = exp((-abs(x) / s))
t_1 = 1.0e0 + t_0
code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) t_1 = Float32(Float32(1.0) + t_0) return Float32(t_0 / Float32(Float32(s * t_1) * t_1)) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); t_1 = single(1.0) + t_0; tmp = t_0 / ((s * t_1) * t_1); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t_0\\
\frac{t_0}{\left(s \cdot t_1\right) \cdot t_1}
\end{array}
\end{array}
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ t_0 1.0))) (/ t_0 (* t_1 (* s t_1)))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = t_0 + 1.0f;
return t_0 / (t_1 * (s * t_1));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: t_1
t_0 = exp((-abs(x) / s))
t_1 = t_0 + 1.0e0
code = t_0 / (t_1 * (s * t_1))
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) t_1 = Float32(t_0 + Float32(1.0)) return Float32(t_0 / Float32(t_1 * Float32(s * t_1))) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); t_1 = t_0 + single(1.0); tmp = t_0 / (t_1 * (s * t_1)); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := t_0 + 1\\
\frac{t_0}{t_1 \cdot \left(s \cdot t_1\right)}
\end{array}
\end{array}
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- x) s)))) (/ (/ t_0 s) (pow (+ 1.0 t_0) 2.0))))
float code(float x, float s) {
float t_0 = expf((-x / s));
return (t_0 / s) / powf((1.0f + t_0), 2.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((-x / s))
code = (t_0 / s) / ((1.0e0 + t_0) ** 2.0e0)
end function
function code(x, s) t_0 = exp(Float32(Float32(-x) / s)) return Float32(Float32(t_0 / s) / (Float32(Float32(1.0) + t_0) ^ Float32(2.0))) end
function tmp = code(x, s) t_0 = exp((-x / s)); tmp = (t_0 / s) / ((single(1.0) + t_0) ^ single(2.0)); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-x}{s}}\\
\frac{\frac{t_0}{s}}{{\left(1 + t_0\right)}^{2}}
\end{array}
\end{array}
(FPCore (x s) :precision binary32 (/ (/ 1.0 (- (* s 2.0) x)) (+ 1.0 (exp (/ (fabs x) s)))))
float code(float x, float s) {
return (1.0f / ((s * 2.0f) - x)) / (1.0f + expf((fabsf(x) / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / ((s * 2.0e0) - x)) / (1.0e0 + exp((abs(x) / s)))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(Float32(s * Float32(2.0)) - x)) / Float32(Float32(1.0) + exp(Float32(abs(x) / s)))) end
function tmp = code(x, s) tmp = (single(1.0) / ((s * single(2.0)) - x)) / (single(1.0) + exp((abs(x) / s))); end
\begin{array}{l}
\\
\frac{\frac{1}{s \cdot 2 - x}}{1 + e^{\frac{\left|x\right|}{s}}}
\end{array}
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ 1.0 (exp (/ (fabs x) s))))))
float code(float x, float s) {
return 0.5f / (s * (1.0f + expf((fabsf(x) / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s * (1.0e0 + exp((abs(x) / s))))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + exp(Float32(abs(x) / s))))) end
function tmp = code(x, s) tmp = single(0.5) / (s * (single(1.0) + exp((abs(x) / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
(FPCore (x s) :precision binary32 (/ (/ 1.0 (* s (exp (/ x s)))) 4.0))
float code(float x, float s) {
return (1.0f / (s * expf((x / s)))) / 4.0f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / (s * exp((x / s)))) / 4.0e0
end function
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(s * exp(Float32(x / s)))) / Float32(4.0)) end
function tmp = code(x, s) tmp = (single(1.0) / (s * exp((x / s)))) / single(4.0); end
\begin{array}{l}
\\
\frac{\frac{1}{s \cdot e^{\frac{x}{s}}}}{4}
\end{array}
(FPCore (x s) :precision binary32 (/ 1.0 (* 2.0 (- (* s 2.0) x))))
float code(float x, float s) {
return 1.0f / (2.0f * ((s * 2.0f) - x));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (2.0e0 * ((s * 2.0e0) - x))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(2.0) * Float32(Float32(s * Float32(2.0)) - x))) end
function tmp = code(x, s) tmp = single(1.0) / (single(2.0) * ((s * single(2.0)) - x)); end
\begin{array}{l}
\\
\frac{1}{2 \cdot \left(s \cdot 2 - x\right)}
\end{array}
(FPCore (x s) :precision binary32 (if (<= x 4.999999987376214e-7) (/ 0.25 s) (/ -0.5 x)))
float code(float x, float s) {
float tmp;
if (x <= 4.999999987376214e-7f) {
tmp = 0.25f / s;
} else {
tmp = -0.5f / 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.999999987376214e-7) then
tmp = 0.25e0 / s
else
tmp = (-0.5e0) / x
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(4.999999987376214e-7)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(-0.5) / x); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(4.999999987376214e-7)) tmp = single(0.25) / s; else tmp = single(-0.5) / x; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.999999987376214 \cdot 10^{-7}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{-0.5}{x}\\
\end{array}
\end{array}
(FPCore (x s) :precision binary32 (/ -0.5 x))
float code(float x, float s) {
return -0.5f / x;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (-0.5e0) / x
end function
function code(x, s) return Float32(Float32(-0.5) / x) end
function tmp = code(x, s) tmp = single(-0.5) / x; end
\begin{array}{l}
\\
\frac{-0.5}{x}
\end{array}
herbie shell --seed 2023343
(FPCore (x s)
:name "Logistic distribution"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ (exp (/ (- (fabs x)) s)) (* (* s (+ 1.0 (exp (/ (- (fabs x)) s)))) (+ 1.0 (exp (/ (- (fabs x)) s))))))