
(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 11 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) (fma s t_0 s)))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return t_0 / ((t_0 + 1.0f) * fmaf(s, t_0, s));
}
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(t_0 / Float32(Float32(t_0 + Float32(1.0)) * fma(s, t_0, 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 \mathsf{fma}\left(s, t\_0, s\right)}
\end{array}
\end{array}
Initial program 99.8%
*-commutative99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fma-define99.9%
fabs-neg99.9%
Simplified99.9%
Final simplification99.9%
(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}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
+-commutative99.8%
Simplified99.8%
Final simplification99.8%
(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(x / Float32(-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}
Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
associate-/r*99.8%
exp-prod99.8%
rem-square-sqrt50.7%
fabs-sqr50.7%
rem-square-sqrt63.1%
exp-prod63.1%
neg-mul-163.1%
distribute-neg-frac263.1%
+-commutative63.1%
exp-prod63.1%
rem-square-sqrt50.7%
fabs-sqr50.7%
rem-square-sqrt64.2%
exp-prod64.2%
neg-mul-164.2%
distribute-neg-frac264.2%
Simplified64.2%
Final simplification64.2%
(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(x / Float32(-s))) return Float32(t_0 / Float32(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{t\_0}{s \cdot {\left(1 + t\_0\right)}^{2}}
\end{array}
\end{array}
Initial program 99.8%
*-commutative99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fma-define99.9%
fabs-neg99.9%
Simplified99.9%
Taylor expanded in x around 0 99.8%
*-commutative99.8%
*-rgt-identity99.8%
distribute-lft-in99.8%
+-commutative99.8%
mul-1-neg99.8%
rec-exp99.8%
+-commutative99.8%
associate-*r*99.8%
rec-exp99.8%
mul-1-neg99.8%
unpow299.8%
Simplified97.6%
Taylor expanded in x around 0 97.6%
mul-1-neg97.6%
rem-square-sqrt50.7%
fabs-sqr50.7%
rem-square-sqrt64.2%
Simplified64.2%
Final simplification64.2%
(FPCore (x s) :precision binary32 (/ (exp (/ (- (fabs x)) s)) (+ (* s 4.0) (* x (- (* 3.0 (/ x s)) 4.0)))))
float code(float x, float s) {
return expf((-fabsf(x) / s)) / ((s * 4.0f) + (x * ((3.0f * (x / s)) - 4.0f)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp((-abs(x) / s)) / ((s * 4.0e0) + (x * ((3.0e0 * (x / s)) - 4.0e0)))
end function
function code(x, s) return Float32(exp(Float32(Float32(-abs(x)) / s)) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(Float32(Float32(3.0) * Float32(x / s)) - Float32(4.0))))) end
function tmp = code(x, s) tmp = exp((-abs(x) / s)) / ((s * single(4.0)) + (x * ((single(3.0) * (x / s)) - single(4.0)))); end
\begin{array}{l}
\\
\frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot 4 + x \cdot \left(3 \cdot \frac{x}{s} - 4\right)}
\end{array}
Initial program 99.8%
*-commutative99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fma-define99.9%
fabs-neg99.9%
Simplified99.9%
Taylor expanded in x around 0 99.8%
*-commutative99.8%
*-rgt-identity99.8%
distribute-lft-in99.8%
+-commutative99.8%
mul-1-neg99.8%
rec-exp99.8%
+-commutative99.8%
associate-*r*99.8%
rec-exp99.8%
mul-1-neg99.8%
unpow299.8%
Simplified97.6%
Taylor expanded in x around 0 97.2%
Final simplification97.2%
(FPCore (x s) :precision binary32 (/ (exp (/ x (- s))) (+ (* s 4.0) (* x (- (/ x s) 4.0)))))
float code(float x, float s) {
return expf((x / -s)) / ((s * 4.0f) + (x * ((x / 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) + (x * ((x / s) - 4.0e0)))
end function
function code(x, s) return Float32(exp(Float32(x / Float32(-s))) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(Float32(x / s) - Float32(4.0))))) end
function tmp = code(x, s) tmp = exp((x / -s)) / ((s * single(4.0)) + (x * ((x / s) - single(4.0)))); end
\begin{array}{l}
\\
\frac{e^{\frac{x}{-s}}}{s \cdot 4 + x \cdot \left(\frac{x}{s} - 4\right)}
\end{array}
Initial program 99.8%
*-commutative99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fma-define99.9%
fabs-neg99.9%
Simplified99.9%
Taylor expanded in x around 0 99.8%
*-commutative99.8%
*-rgt-identity99.8%
distribute-lft-in99.8%
+-commutative99.8%
mul-1-neg99.8%
rec-exp99.8%
+-commutative99.8%
associate-*r*99.8%
rec-exp99.8%
mul-1-neg99.8%
unpow299.8%
Simplified97.6%
Taylor expanded in x around 0 97.6%
mul-1-neg97.6%
rem-square-sqrt50.7%
fabs-sqr50.7%
rem-square-sqrt64.2%
Simplified64.2%
Taylor expanded in x around 0 62.7%
neg-mul-162.7%
unsub-neg62.7%
Simplified62.7%
Taylor expanded in x around 0 62.9%
Final simplification62.9%
(FPCore (x s) :precision binary32 (/ (/ (exp (/ x (- s))) s) (+ 4.0 (* (/ x s) -4.0))))
float code(float x, float s) {
return (expf((x / -s)) / s) / (4.0f + ((x / 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 + ((x / s) * (-4.0e0)))
end function
function code(x, s) return Float32(Float32(exp(Float32(x / Float32(-s))) / s) / Float32(Float32(4.0) + Float32(Float32(x / s) * Float32(-4.0)))) end
function tmp = code(x, s) tmp = (exp((x / -s)) / s) / (single(4.0) + ((x / s) * single(-4.0))); end
\begin{array}{l}
\\
\frac{\frac{e^{\frac{x}{-s}}}{s}}{4 + \frac{x}{s} \cdot -4}
\end{array}
Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
associate-/r*99.8%
exp-prod99.8%
rem-square-sqrt50.7%
fabs-sqr50.7%
rem-square-sqrt63.1%
exp-prod63.1%
neg-mul-163.1%
distribute-neg-frac263.1%
+-commutative63.1%
exp-prod63.1%
rem-square-sqrt50.7%
fabs-sqr50.7%
rem-square-sqrt64.2%
exp-prod64.2%
neg-mul-164.2%
distribute-neg-frac264.2%
Simplified64.2%
Taylor expanded in x around 0 63.0%
*-commutative63.0%
Simplified63.0%
(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(Float32(exp(Float32(x / Float32(-s))) / s) / Float32(4.0)) end
function tmp = code(x, s) tmp = (exp((x / -s)) / s) / single(4.0); end
\begin{array}{l}
\\
\frac{\frac{e^{\frac{x}{-s}}}{s}}{4}
\end{array}
Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
associate-/r*99.8%
exp-prod99.8%
rem-square-sqrt50.7%
fabs-sqr50.7%
rem-square-sqrt63.1%
exp-prod63.1%
neg-mul-163.1%
distribute-neg-frac263.1%
+-commutative63.1%
exp-prod63.1%
rem-square-sqrt50.7%
fabs-sqr50.7%
rem-square-sqrt64.2%
exp-prod64.2%
neg-mul-164.2%
distribute-neg-frac264.2%
Simplified64.2%
Taylor expanded in x around 0 62.6%
neg-mul-162.7%
unsub-neg62.7%
Simplified62.6%
Taylor expanded in x around 0 62.0%
(FPCore (x s) :precision binary32 (if (<= x 2.4999999206638063e-22) (/ 1.0 (* s (+ 4.0 (/ x (* s (/ s x)))))) 0.0))
float code(float x, float s) {
float tmp;
if (x <= 2.4999999206638063e-22f) {
tmp = 1.0f / (s * (4.0f + (x / (s * (s / x)))));
} else {
tmp = 0.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.4999999206638063e-22) then
tmp = 1.0e0 / (s * (4.0e0 + (x / (s * (s / x)))))
else
tmp = 0.0e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(2.4999999206638063e-22)) tmp = Float32(Float32(1.0) / Float32(s * Float32(Float32(4.0) + Float32(x / Float32(s * Float32(s / x)))))); else tmp = Float32(0.0); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(2.4999999206638063e-22)) tmp = single(1.0) / (s * (single(4.0) + (x / (s * (s / x))))); else tmp = single(0.0); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.4999999206638063 \cdot 10^{-22}:\\
\;\;\;\;\frac{1}{s \cdot \left(4 + \frac{x}{s \cdot \frac{s}{x}}\right)}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 2.4999999e-22Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.8%
clear-num99.8%
inv-pow99.8%
Applied egg-rr96.2%
unpow-196.2%
Simplified96.2%
Taylor expanded in x around 0 63.0%
Taylor expanded in s around inf 71.7%
+-commutative71.7%
unpow271.7%
unpow271.7%
times-frac72.9%
unpow272.9%
Simplified72.9%
unpow272.9%
clear-num72.9%
frac-times75.5%
*-un-lft-identity75.5%
Applied egg-rr75.5%
if 2.4999999e-22 < x Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.9%
Taylor expanded in s around inf 11.8%
expm1-log1p-u11.5%
expm1-undefine11.5%
Applied egg-rr11.5%
expm1-define11.5%
Simplified11.5%
expm1-undefine11.5%
log1p-undefine11.5%
rem-exp-log11.8%
Applied egg-rr11.8%
Taylor expanded in s around inf 92.5%
Final simplification83.0%
(FPCore (x s) :precision binary32 (if (<= x 5.000000097707407e-26) (/ 0.25 s) 0.0))
float code(float x, float s) {
float tmp;
if (x <= 5.000000097707407e-26f) {
tmp = 0.25f / s;
} else {
tmp = 0.0f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 5.000000097707407e-26) then
tmp = 0.25e0 / s
else
tmp = 0.0e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(5.000000097707407e-26)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(0.0); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(5.000000097707407e-26)) tmp = single(0.25) / s; else tmp = single(0.0); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.000000097707407 \cdot 10^{-26}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 5.0000001e-26Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in s around inf 32.4%
if 5.0000001e-26 < x Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in s around inf 12.6%
expm1-log1p-u12.2%
expm1-undefine12.1%
Applied egg-rr12.1%
expm1-define12.2%
Simplified12.2%
expm1-undefine12.1%
log1p-undefine12.1%
rem-exp-log12.6%
Applied egg-rr12.6%
Taylor expanded in s around inf 91.1%
Final simplification59.2%
(FPCore (x s) :precision binary32 0.0)
float code(float x, float s) {
return 0.0f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.0e0
end function
function code(x, s) return Float32(0.0) end
function tmp = code(x, s) tmp = single(0.0); end
\begin{array}{l}
\\
0
\end{array}
Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in s around inf 23.4%
expm1-log1p-u22.1%
expm1-undefine22.0%
Applied egg-rr22.0%
expm1-define22.1%
Simplified22.1%
expm1-undefine22.0%
log1p-undefine22.0%
rem-exp-log23.3%
Applied egg-rr23.3%
Taylor expanded in s around inf 79.2%
Final simplification79.2%
herbie shell --seed 2024151
(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))))))