
(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 10 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_0 (* (+ t_0 1.0) (+ s (/ s (exp (/ (fabs x) s))))))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return t_0 / ((t_0 + 1.0f) * (s + (s / expf((fabsf(x) / s)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((-abs(x) / s))
code = t_0 / ((t_0 + 1.0e0) * (s + (s / exp((abs(x) / s)))))
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(t_0 / Float32(Float32(t_0 + Float32(1.0)) * Float32(s + Float32(s / exp(Float32(abs(x) / s)))))) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); tmp = t_0 / ((t_0 + single(1.0)) * (s + (s / exp((abs(x) / s))))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
\frac{t_0}{\left(t_0 + 1\right) \cdot \left(s + \frac{s}{e^{\frac{\left|x\right|}{s}}}\right)}
\end{array}
\end{array}
(FPCore (x s)
:precision binary32
(let* ((t_0 (sqrt (/ x s))))
(/
1.0
(* s (* (+ (exp (/ (- (fabs x)) s)) 1.0) (+ 1.0 (pow (exp t_0) t_0)))))))
float code(float x, float s) {
float t_0 = sqrtf((x / s));
return 1.0f / (s * ((expf((-fabsf(x) / s)) + 1.0f) * (1.0f + powf(expf(t_0), t_0))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
t_0 = sqrt((x / s))
code = 1.0e0 / (s * ((exp((-abs(x) / s)) + 1.0e0) * (1.0e0 + (exp(t_0) ** t_0))))
end function
function code(x, s) t_0 = sqrt(Float32(x / s)) return Float32(Float32(1.0) / Float32(s * Float32(Float32(exp(Float32(Float32(-abs(x)) / s)) + Float32(1.0)) * Float32(Float32(1.0) + (exp(t_0) ^ t_0))))) end
function tmp = code(x, s) t_0 = sqrt((x / s)); tmp = single(1.0) / (s * ((exp((-abs(x) / s)) + single(1.0)) * (single(1.0) + (exp(t_0) ^ t_0)))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\frac{x}{s}}\\
\frac{1}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(1 + {\left(e^{t_0}\right)}^{t_0}\right)\right)}
\end{array}
\end{array}
(FPCore (x s) :precision binary32 (/ 1.0 (* s (* (+ (exp (/ (- (fabs x)) s)) 1.0) (+ 1.0 (exp (/ x s)))))))
float code(float x, float s) {
return 1.0f / (s * ((expf((-fabsf(x) / s)) + 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 / (s * ((exp((-abs(x) / s)) + 1.0e0) * (1.0e0 + exp((x / s)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(exp(Float32(Float32(-abs(x)) / s)) + Float32(1.0)) * Float32(Float32(1.0) + exp(Float32(x / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / (s * ((exp((-abs(x) / s)) + single(1.0)) * (single(1.0) + exp((x / s))))); end
\begin{array}{l}
\\
\frac{1}{s \cdot \left(\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(1 + e^{\frac{x}{s}}\right)\right)}
\end{array}
(FPCore (x s) :precision binary32 (/ (exp (/ (- (fabs x)) s)) (+ (* (fabs x) -4.0) (* s 4.0))))
float code(float x, float s) {
return expf((-fabsf(x) / s)) / ((fabsf(x) * -4.0f) + (s * 4.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp((-abs(x) / s)) / ((abs(x) * (-4.0e0)) + (s * 4.0e0))
end function
function code(x, s) return Float32(exp(Float32(Float32(-abs(x)) / s)) / Float32(Float32(abs(x) * Float32(-4.0)) + Float32(s * Float32(4.0)))) end
function tmp = code(x, s) tmp = exp((-abs(x) / s)) / ((abs(x) * single(-4.0)) + (s * single(4.0))); end
\begin{array}{l}
\\
\frac{e^{\frac{-\left|x\right|}{s}}}{\left|x\right| \cdot -4 + s \cdot 4}
\end{array}
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (pow (+ 1.0 (exp (/ x s))) 2.0)))
float code(float x, float s) {
return (1.0f / s) / powf((1.0f + expf((x / s))), 2.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / ((1.0e0 + exp((x / s))) ** 2.0e0)
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / (Float32(Float32(1.0) + exp(Float32(x / s))) ^ Float32(2.0))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / ((single(1.0) + exp((x / s))) ^ single(2.0)); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{{\left(1 + e^{\frac{x}{s}}\right)}^{2}}
\end{array}
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (pow (+ 1.0 (+ 1.0 (/ x s))) 2.0)))
float code(float x, float s) {
return (1.0f / s) / powf((1.0f + (1.0f + (x / s))), 2.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / ((1.0e0 + (1.0e0 + (x / s))) ** 2.0e0)
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / (Float32(Float32(1.0) + Float32(Float32(1.0) + Float32(x / s))) ^ Float32(2.0))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / ((single(1.0) + (single(1.0) + (x / s))) ^ single(2.0)); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{{\left(1 + \left(1 + \frac{x}{s}\right)\right)}^{2}}
\end{array}
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ 4.0 (* (/ x s) 4.0))))
float code(float x, float s) {
return (1.0f / s) / (4.0f + ((x / s) * 4.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / (4.0e0 + ((x / s) * 4.0e0))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x / s) * Float32(4.0)))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / (single(4.0) + ((x / s) * single(4.0))); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{4 + \frac{x}{s} \cdot 4}
\end{array}
(FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (/ (* x x) s))))
float code(float x, float s) {
return 1.0f / ((s * 4.0f) + ((x * x) / s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((s * 4.0e0) + ((x * x) / s))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(Float32(x * x) / s))) end
function tmp = code(x, s) tmp = single(1.0) / ((s * single(4.0)) + ((x * x) / s)); end
\begin{array}{l}
\\
\frac{1}{s \cdot 4 + \frac{x \cdot x}{s}}
\end{array}
(FPCore (x s) :precision binary32 (if (<= x 0.0007999999797903001) (/ 0.25 s) (/ 0.3333333333333333 x)))
float code(float x, float s) {
float tmp;
if (x <= 0.0007999999797903001f) {
tmp = 0.25f / s;
} else {
tmp = 0.3333333333333333f / 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.0007999999797903001e0) then
tmp = 0.25e0 / s
else
tmp = 0.3333333333333333e0 / x
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.0007999999797903001)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.3333333333333333) / x); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.0007999999797903001)) tmp = single(0.25) / s; else tmp = single(0.3333333333333333) / x; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0007999999797903001:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.3333333333333333}{x}\\
\end{array}
\end{array}
(FPCore (x s) :precision binary32 (/ 0.25 s))
float code(float x, float s) {
return 0.25f / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.25e0 / s
end function
function code(x, s) return Float32(Float32(0.25) / s) end
function tmp = code(x, s) tmp = single(0.25) / s; end
\begin{array}{l}
\\
\frac{0.25}{s}
\end{array}
herbie shell --seed 2023350
(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))))))