
(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 12 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(abs(x) / Float32(-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}
Initial program 99.6%
Final simplification99.6%
(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(abs(x) / Float32(-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}
Initial program 99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
distribute-lft-in99.6%
*-rgt-identity99.6%
+-commutative99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x s) :precision binary32 (/ (exp (/ (+ x (* -2.0 (* s (log1p (exp (/ x s)))))) s)) s))
float code(float x, float s) {
return expf(((x + (-2.0f * (s * log1pf(expf((x / s)))))) / s)) / s;
}
function code(x, s) return Float32(exp(Float32(Float32(x + Float32(Float32(-2.0) * Float32(s * log1p(exp(Float32(x / s)))))) / s)) / s) end
\begin{array}{l}
\\
\frac{e^{\frac{x + -2 \cdot \left(s \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)\right)}{s}}}{s}
\end{array}
Initial program 99.6%
*-commutative99.6%
Simplified99.6%
*-un-lft-identity99.6%
*-commutative99.6%
associate-*r*99.6%
pow299.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
pow299.6%
associate-/r*99.6%
Applied egg-rr61.5%
*-lft-identity61.5%
associate-/l/61.8%
exp-to-pow61.9%
log1p-undefine61.9%
*-commutative61.9%
rem-exp-log60.2%
exp-sum60.2%
exp-diff88.6%
associate--r+88.7%
exp-diff88.9%
Simplified90.6%
Taylor expanded in s around 0 99.5%
log1p-define99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x s) :precision binary32 (/ (exp (/ (+ x (* -2.0 (+ (* s (log 2.0)) (* x (+ 0.5 (* (/ x s) 0.125)))))) s)) s))
float code(float x, float s) {
return expf(((x + (-2.0f * ((s * logf(2.0f)) + (x * (0.5f + ((x / s) * 0.125f)))))) / s)) / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp(((x + ((-2.0e0) * ((s * log(2.0e0)) + (x * (0.5e0 + ((x / s) * 0.125e0)))))) / s)) / s
end function
function code(x, s) return Float32(exp(Float32(Float32(x + Float32(Float32(-2.0) * Float32(Float32(s * log(Float32(2.0))) + Float32(x * Float32(Float32(0.5) + Float32(Float32(x / s) * Float32(0.125))))))) / s)) / s) end
function tmp = code(x, s) tmp = exp(((x + (single(-2.0) * ((s * log(single(2.0))) + (x * (single(0.5) + ((x / s) * single(0.125))))))) / s)) / s; end
\begin{array}{l}
\\
\frac{e^{\frac{x + -2 \cdot \left(s \cdot \log 2 + x \cdot \left(0.5 + \frac{x}{s} \cdot 0.125\right)\right)}{s}}}{s}
\end{array}
Initial program 99.6%
*-commutative99.6%
Simplified99.6%
*-un-lft-identity99.6%
*-commutative99.6%
associate-*r*99.6%
pow299.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
pow299.6%
associate-/r*99.6%
Applied egg-rr61.5%
*-lft-identity61.5%
associate-/l/61.8%
exp-to-pow61.9%
log1p-undefine61.9%
*-commutative61.9%
rem-exp-log60.2%
exp-sum60.2%
exp-diff88.6%
associate--r+88.7%
exp-diff88.9%
Simplified90.6%
Taylor expanded in s around 0 99.5%
log1p-define99.5%
Simplified99.5%
Taylor expanded in x around 0 96.7%
Final simplification96.7%
(FPCore (x s) :precision binary32 (* (/ -1.0 (- -1.0 (pow E (/ x s)))) (/ 0.5 s)))
float code(float x, float s) {
return (-1.0f / (-1.0f - powf(((float) M_E), (x / s)))) * (0.5f / s);
}
function code(x, s) return Float32(Float32(Float32(-1.0) / Float32(Float32(-1.0) - (Float32(exp(1)) ^ Float32(x / s)))) * Float32(Float32(0.5) / s)) end
function tmp = code(x, s) tmp = (single(-1.0) / (single(-1.0) - (single(2.71828182845904523536) ^ (x / s)))) * (single(0.5) / s); end
\begin{array}{l}
\\
\frac{-1}{-1 - {e}^{\left(\frac{x}{s}\right)}} \cdot \frac{0.5}{s}
\end{array}
Initial program 99.6%
*-commutative99.6%
Simplified99.6%
Applied egg-rr62.5%
Taylor expanded in x around 0 58.4%
*-un-lft-identity58.4%
exp-prod58.4%
Applied egg-rr58.4%
exp-1-e58.4%
Simplified58.4%
Final simplification58.4%
(FPCore (x s) :precision binary32 (* (/ 0.5 s) (/ 1.0 (+ (exp (/ x s)) 1.0))))
float code(float x, float s) {
return (0.5f / s) * (1.0f / (expf((x / s)) + 1.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.5e0 / s) * (1.0e0 / (exp((x / s)) + 1.0e0))
end function
function code(x, s) return Float32(Float32(Float32(0.5) / s) * Float32(Float32(1.0) / Float32(exp(Float32(x / s)) + Float32(1.0)))) end
function tmp = code(x, s) tmp = (single(0.5) / s) * (single(1.0) / (exp((x / s)) + single(1.0))); end
\begin{array}{l}
\\
\frac{0.5}{s} \cdot \frac{1}{e^{\frac{x}{s}} + 1}
\end{array}
Initial program 99.6%
*-commutative99.6%
Simplified99.6%
Applied egg-rr62.5%
Taylor expanded in x around 0 58.4%
Final simplification58.4%
(FPCore (x s) :precision binary32 (/ (/ 0.5 s) (+ (exp (/ x s)) 1.0)))
float code(float x, float s) {
return (0.5f / s) / (expf((x / s)) + 1.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.5e0 / s) / (exp((x / s)) + 1.0e0)
end function
function code(x, s) return Float32(Float32(Float32(0.5) / s) / Float32(exp(Float32(x / s)) + Float32(1.0))) end
function tmp = code(x, s) tmp = (single(0.5) / s) / (exp((x / s)) + single(1.0)); end
\begin{array}{l}
\\
\frac{\frac{0.5}{s}}{e^{\frac{x}{s}} + 1}
\end{array}
Initial program 99.6%
*-commutative99.6%
Simplified99.6%
Applied egg-rr62.5%
Taylor expanded in x around 0 58.4%
Taylor expanded in x around inf 58.4%
associate-/r*58.4%
Simplified58.4%
Final simplification58.4%
(FPCore (x s) :precision binary32 (/ (exp (/ (- x) s)) (* s 4.0)))
float code(float x, float s) {
return expf((-x / s)) / (s * 4.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp((-x / s)) / (s * 4.0e0)
end function
function code(x, s) return Float32(exp(Float32(Float32(-x) / s)) / Float32(s * Float32(4.0))) end
function tmp = code(x, s) tmp = exp((-x / s)) / (s * single(4.0)); end
\begin{array}{l}
\\
\frac{e^{\frac{-x}{s}}}{s \cdot 4}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Taylor expanded in s around inf 94.0%
distribute-frac-neg294.0%
rec-exp94.0%
frac-2neg94.0%
add-sqr-sqrt-0.0%
sqrt-unprod18.4%
sqr-neg18.4%
sqrt-unprod21.3%
add-sqr-sqrt21.3%
remove-double-neg21.3%
frac-2neg21.3%
add-sqr-sqrt-0.0%
add-sqr-sqrt-0.0%
fabs-sqr-0.0%
add-sqr-sqrt-0.0%
sqrt-unprod54.2%
sqr-neg54.2%
sqrt-unprod57.6%
add-sqr-sqrt57.6%
Applied egg-rr57.6%
rec-exp57.6%
distribute-neg-frac257.6%
Simplified57.6%
Final simplification57.6%
(FPCore (x s)
:precision binary32
(if (<= x 2.499999956129175e-15)
(/ 0.25 s)
(if (<= x 4.0000001015105716e+26)
(/ (- (* (/ x s) -0.125) (/ (* 0.5 (* x -0.25)) s)) s)
(* (/ 0.5 s) (/ 1.0 (+ (/ x s) 2.0))))))
float code(float x, float s) {
float tmp;
if (x <= 2.499999956129175e-15f) {
tmp = 0.25f / s;
} else if (x <= 4.0000001015105716e+26f) {
tmp = (((x / s) * -0.125f) - ((0.5f * (x * -0.25f)) / s)) / s;
} else {
tmp = (0.5f / s) * (1.0f / ((x / s) + 2.0f));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 2.499999956129175e-15) then
tmp = 0.25e0 / s
else if (x <= 4.0000001015105716e+26) then
tmp = (((x / s) * (-0.125e0)) - ((0.5e0 * (x * (-0.25e0))) / s)) / s
else
tmp = (0.5e0 / s) * (1.0e0 / ((x / s) + 2.0e0))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(2.499999956129175e-15)) tmp = Float32(Float32(0.25) / s); elseif (x <= Float32(4.0000001015105716e+26)) tmp = Float32(Float32(Float32(Float32(x / s) * Float32(-0.125)) - Float32(Float32(Float32(0.5) * Float32(x * Float32(-0.25))) / s)) / s); else tmp = Float32(Float32(Float32(0.5) / s) * Float32(Float32(1.0) / Float32(Float32(x / s) + Float32(2.0)))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(2.499999956129175e-15)) tmp = single(0.25) / s; elseif (x <= single(4.0000001015105716e+26)) tmp = (((x / s) * single(-0.125)) - ((single(0.5) * (x * single(-0.25))) / s)) / s; else tmp = (single(0.5) / s) * (single(1.0) / ((x / s) + single(2.0))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.499999956129175 \cdot 10^{-15}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{elif}\;x \leq 4.0000001015105716 \cdot 10^{+26}:\\
\;\;\;\;\frac{\frac{x}{s} \cdot -0.125 - \frac{0.5 \cdot \left(x \cdot -0.25\right)}{s}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{s} \cdot \frac{1}{\frac{x}{s} + 2}\\
\end{array}
\end{array}
if x < 2.49999996e-15Initial program 99.5%
*-commutative99.5%
Simplified99.5%
Taylor expanded in s around inf 31.5%
if 2.49999996e-15 < x < 4.0000001e26Initial program 99.8%
*-commutative99.8%
Simplified99.8%
Applied egg-rr10.4%
Taylor expanded in s around inf 45.5%
Taylor expanded in s around -inf 82.0%
mul-1-neg82.0%
associate-*r/82.1%
distribute-rgt-out--82.1%
metadata-eval82.1%
associate-*r/85.9%
*-commutative85.9%
Simplified85.9%
Taylor expanded in x around inf 77.1%
if 4.0000001e26 < x Initial program 100.0%
*-commutative100.0%
Simplified100.0%
Applied egg-rr-0.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around 0 73.0%
Final simplification48.1%
(FPCore (x s) :precision binary32 (if (<= x 4.0000001015105716e+26) (/ (- (+ 0.25 (/ (* x -0.125) s)) (/ (* 0.5 (* x -0.25)) s)) s) (* (/ 0.5 s) (/ 1.0 (+ (/ x s) 2.0)))))
float code(float x, float s) {
float tmp;
if (x <= 4.0000001015105716e+26f) {
tmp = ((0.25f + ((x * -0.125f) / s)) - ((0.5f * (x * -0.25f)) / s)) / s;
} else {
tmp = (0.5f / s) * (1.0f / ((x / s) + 2.0f));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 4.0000001015105716e+26) then
tmp = ((0.25e0 + ((x * (-0.125e0)) / s)) - ((0.5e0 * (x * (-0.25e0))) / s)) / s
else
tmp = (0.5e0 / s) * (1.0e0 / ((x / s) + 2.0e0))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(4.0000001015105716e+26)) tmp = Float32(Float32(Float32(Float32(0.25) + Float32(Float32(x * Float32(-0.125)) / s)) - Float32(Float32(Float32(0.5) * Float32(x * Float32(-0.25))) / s)) / s); else tmp = Float32(Float32(Float32(0.5) / s) * Float32(Float32(1.0) / Float32(Float32(x / s) + Float32(2.0)))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(4.0000001015105716e+26)) tmp = ((single(0.25) + ((x * single(-0.125)) / s)) - ((single(0.5) * (x * single(-0.25))) / s)) / s; else tmp = (single(0.5) / s) * (single(1.0) / ((x / s) + single(2.0))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.0000001015105716 \cdot 10^{+26}:\\
\;\;\;\;\frac{\left(0.25 + \frac{x \cdot -0.125}{s}\right) - \frac{0.5 \cdot \left(x \cdot -0.25\right)}{s}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{s} \cdot \frac{1}{\frac{x}{s} + 2}\\
\end{array}
\end{array}
if x < 4.0000001e26Initial program 99.6%
*-commutative99.6%
Simplified99.6%
Applied egg-rr67.5%
Taylor expanded in s around inf 37.1%
Taylor expanded in s around -inf 75.1%
mul-1-neg75.1%
associate-*r/75.1%
distribute-rgt-out--75.1%
metadata-eval75.1%
associate-*r/76.8%
*-commutative76.8%
Simplified76.8%
if 4.0000001e26 < x Initial program 100.0%
*-commutative100.0%
Simplified100.0%
Applied egg-rr-0.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around 0 73.0%
Final simplification76.5%
(FPCore (x s) :precision binary32 (* (/ 0.5 s) (/ 1.0 (+ (/ x s) 2.0))))
float code(float x, float s) {
return (0.5f / s) * (1.0f / ((x / s) + 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.5e0 / s) * (1.0e0 / ((x / s) + 2.0e0))
end function
function code(x, s) return Float32(Float32(Float32(0.5) / s) * Float32(Float32(1.0) / Float32(Float32(x / s) + Float32(2.0)))) end
function tmp = code(x, s) tmp = (single(0.5) / s) * (single(1.0) / ((x / s) + single(2.0))); end
\begin{array}{l}
\\
\frac{0.5}{s} \cdot \frac{1}{\frac{x}{s} + 2}
\end{array}
Initial program 99.6%
*-commutative99.6%
Simplified99.6%
Applied egg-rr62.5%
Taylor expanded in x around 0 58.4%
Taylor expanded in x around 0 41.6%
Final simplification41.6%
(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}
Initial program 99.6%
*-commutative99.6%
Simplified99.6%
Taylor expanded in s around inf 24.2%
Final simplification24.2%
herbie shell --seed 2024095
(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))))))