
(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}
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ x_m (- s))))) (/ (/ t_0 s) (pow (+ t_0 1.0) 2.0))))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((x_m / -s));
return (t_0 / s) / powf((t_0 + 1.0f), 2.0f);
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((x_m / -s))
code = (t_0 / s) / ((t_0 + 1.0e0) ** 2.0e0)
end function
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(x_m / Float32(-s))) return Float32(Float32(t_0 / s) / (Float32(t_0 + Float32(1.0)) ^ Float32(2.0))) end
x_m = abs(x); function tmp = code(x_m, s) t_0 = exp((x_m / -s)); tmp = (t_0 / s) / ((t_0 + single(1.0)) ^ single(2.0)); end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{x\_m}{-s}}\\
\frac{\frac{t\_0}{s}}{{\left(t\_0 + 1\right)}^{2}}
\end{array}
\end{array}
Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.7%
Taylor expanded in x around 0 99.7%
associate-/r*99.7%
exp-prod99.6%
rem-square-sqrt44.5%
fabs-sqr44.5%
rem-square-sqrt57.2%
exp-prod57.2%
neg-mul-157.2%
distribute-neg-frac257.2%
+-commutative57.2%
exp-prod57.2%
rem-square-sqrt44.5%
fabs-sqr44.5%
rem-square-sqrt58.4%
exp-prod58.4%
neg-mul-158.4%
distribute-neg-frac258.4%
Simplified58.4%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ (exp (/ x_m (- s))) s) (+ 4.0 (* (/ x_m s) -4.0))))
x_m = fabs(x);
float code(float x_m, float s) {
return (expf((x_m / -s)) / s) / (4.0f + ((x_m / s) * -4.0f));
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = (exp((x_m / -s)) / s) / (4.0e0 + ((x_m / s) * (-4.0e0)))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(exp(Float32(x_m / Float32(-s))) / s) / Float32(Float32(4.0) + Float32(Float32(x_m / s) * Float32(-4.0)))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (exp((x_m / -s)) / s) / (single(4.0) + ((x_m / s) * single(-4.0))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{e^{\frac{x\_m}{-s}}}{s}}{4 + \frac{x\_m}{s} \cdot -4}
\end{array}
Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.7%
Taylor expanded in x around 0 99.7%
associate-/r*99.7%
exp-prod99.6%
rem-square-sqrt44.5%
fabs-sqr44.5%
rem-square-sqrt57.2%
exp-prod57.2%
neg-mul-157.2%
distribute-neg-frac257.2%
+-commutative57.2%
exp-prod57.2%
rem-square-sqrt44.5%
fabs-sqr44.5%
rem-square-sqrt58.4%
exp-prod58.4%
neg-mul-158.4%
distribute-neg-frac258.4%
Simplified58.4%
Taylor expanded in x around 0 57.4%
*-commutative57.4%
Simplified57.4%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (* (/ -1.0 (- -1.0 (exp (/ x_m s)))) (/ 0.5 s)))
x_m = fabs(x);
float code(float x_m, float s) {
return (-1.0f / (-1.0f - expf((x_m / s)))) * (0.5f / s);
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = ((-1.0e0) / ((-1.0e0) - exp((x_m / s)))) * (0.5e0 / s)
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(-1.0) / Float32(Float32(-1.0) - exp(Float32(x_m / s)))) * Float32(Float32(0.5) / s)) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(-1.0) / (single(-1.0) - exp((x_m / s)))) * (single(0.5) / s); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{-1}{-1 - e^{\frac{x\_m}{s}}} \cdot \frac{0.5}{s}
\end{array}
Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.7%
Taylor expanded in x around 0 99.7%
+-commutative99.7%
exp-prod99.7%
rem-square-sqrt44.5%
fabs-sqr44.5%
rem-square-sqrt96.2%
exp-prod96.2%
neg-mul-196.2%
distribute-neg-frac296.2%
Simplified96.2%
add-sqr-sqrt-0.0%
add-sqr-sqrt96.2%
add-sqr-sqrt44.4%
fabs-sqr44.4%
add-sqr-sqrt58.4%
*-rgt-identity58.4%
frac-times58.4%
div-inv58.4%
div-inv58.4%
unpow258.4%
times-frac59.5%
Applied egg-rr67.6%
Taylor expanded in x around 0 56.7%
associate-*r/56.7%
clear-num56.8%
+-commutative56.8%
un-div-inv56.8%
Applied egg-rr56.8%
associate-/r/56.7%
Simplified56.7%
Final simplification56.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 0.5 s) (+ (exp (/ x_m s)) 1.0)))
x_m = fabs(x);
float code(float x_m, float s) {
return (0.5f / s) / (expf((x_m / s)) + 1.0f);
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = (0.5e0 / s) / (exp((x_m / s)) + 1.0e0)
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(0.5) / s) / Float32(exp(Float32(x_m / s)) + Float32(1.0))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(0.5) / s) / (exp((x_m / s)) + single(1.0)); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{0.5}{s}}{e^{\frac{x\_m}{s}} + 1}
\end{array}
Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.7%
Taylor expanded in x around 0 99.7%
+-commutative99.7%
exp-prod99.7%
rem-square-sqrt44.5%
fabs-sqr44.5%
rem-square-sqrt96.2%
exp-prod96.2%
neg-mul-196.2%
distribute-neg-frac296.2%
Simplified96.2%
add-sqr-sqrt-0.0%
add-sqr-sqrt96.2%
add-sqr-sqrt44.4%
fabs-sqr44.4%
add-sqr-sqrt58.4%
*-rgt-identity58.4%
frac-times58.4%
div-inv58.4%
div-inv58.4%
unpow258.4%
times-frac59.5%
Applied egg-rr67.6%
Taylor expanded in x around 0 56.7%
Taylor expanded in s around 0 56.7%
associate-/r*56.7%
Simplified56.7%
Final simplification56.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ (exp (/ x_m (- s))) s) 4.0))
x_m = fabs(x);
float code(float x_m, float s) {
return (expf((x_m / -s)) / s) / 4.0f;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = (exp((x_m / -s)) / s) / 4.0e0
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(exp(Float32(x_m / Float32(-s))) / s) / Float32(4.0)) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (exp((x_m / -s)) / s) / single(4.0); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{e^{\frac{x\_m}{-s}}}{s}}{4}
\end{array}
Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.7%
Taylor expanded in x around 0 99.7%
associate-/r*99.7%
exp-prod99.6%
rem-square-sqrt44.5%
fabs-sqr44.5%
rem-square-sqrt57.2%
exp-prod57.2%
neg-mul-157.2%
distribute-neg-frac257.2%
+-commutative57.2%
exp-prod57.2%
rem-square-sqrt44.5%
fabs-sqr44.5%
rem-square-sqrt58.4%
exp-prod58.4%
neg-mul-158.4%
distribute-neg-frac258.4%
Simplified58.4%
Taylor expanded in x around 0 57.4%
*-commutative57.4%
Simplified57.4%
Taylor expanded in x around 0 56.0%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= x_m 1.0000000138484279e+24) (/ (- (+ 0.25 (* (/ x_m s) 0.25)) (/ (* x_m 0.25) s)) s) (/ (/ 1.0 s) (+ 4.0 (* (/ x_m s) -4.0)))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (x_m <= 1.0000000138484279e+24f) {
tmp = ((0.25f + ((x_m / s) * 0.25f)) - ((x_m * 0.25f) / s)) / s;
} else {
tmp = (1.0f / s) / (4.0f + ((x_m / s) * -4.0f));
}
return tmp;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
real(4) :: tmp
if (x_m <= 1.0000000138484279e+24) then
tmp = ((0.25e0 + ((x_m / s) * 0.25e0)) - ((x_m * 0.25e0) / s)) / s
else
tmp = (1.0e0 / s) / (4.0e0 + ((x_m / s) * (-4.0e0)))
end if
code = tmp
end function
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (x_m <= Float32(1.0000000138484279e+24)) tmp = Float32(Float32(Float32(Float32(0.25) + Float32(Float32(x_m / s) * Float32(0.25))) - Float32(Float32(x_m * Float32(0.25)) / s)) / s); else tmp = Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x_m / s) * Float32(-4.0)))); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, s) tmp = single(0.0); if (x_m <= single(1.0000000138484279e+24)) tmp = ((single(0.25) + ((x_m / s) * single(0.25))) - ((x_m * single(0.25)) / s)) / s; else tmp = (single(1.0) / s) / (single(4.0) + ((x_m / s) * single(-4.0))); end tmp_2 = tmp; end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 1.0000000138484279 \cdot 10^{+24}:\\
\;\;\;\;\frac{\left(0.25 + \frac{x\_m}{s} \cdot 0.25\right) - \frac{x\_m \cdot 0.25}{s}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{s}}{4 + \frac{x\_m}{s} \cdot -4}\\
\end{array}
\end{array}
if x < 1.00000001e24Initial 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.7%
Taylor expanded in x around 0 99.7%
+-commutative99.7%
exp-prod99.7%
rem-square-sqrt38.2%
fabs-sqr38.2%
rem-square-sqrt95.8%
exp-prod95.8%
neg-mul-195.8%
distribute-neg-frac295.8%
Simplified95.8%
Taylor expanded in s around -inf 47.9%
mul-1-neg47.9%
*-commutative47.9%
Simplified47.9%
*-commutative47.9%
add-sqr-sqrt26.3%
fabs-sqr26.3%
add-sqr-sqrt72.7%
associate-*l/72.7%
Applied egg-rr72.7%
if 1.00000001e24 < x Initial program 100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg2100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
Simplified100.0%
Taylor expanded in x around 0 100.0%
associate-/r*100.0%
exp-prod100.0%
rem-square-sqrt100.0%
fabs-sqr100.0%
rem-square-sqrt100.0%
exp-prod100.0%
neg-mul-1100.0%
distribute-neg-frac2100.0%
+-commutative100.0%
exp-prod100.0%
rem-square-sqrt100.0%
fabs-sqr100.0%
rem-square-sqrt100.0%
exp-prod100.0%
neg-mul-1100.0%
distribute-neg-frac2100.0%
Simplified100.0%
Taylor expanded in x around 0 100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 80.1%
Final simplification73.4%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (* 0.5 (/ (/ 1.0 s) (+ 2.0 (/ x_m s)))))
x_m = fabs(x);
float code(float x_m, float s) {
return 0.5f * ((1.0f / s) / (2.0f + (x_m / s)));
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 0.5e0 * ((1.0e0 / s) / (2.0e0 + (x_m / s)))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(0.5) * Float32(Float32(Float32(1.0) / s) / Float32(Float32(2.0) + Float32(x_m / s)))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(0.5) * ((single(1.0) / s) / (single(2.0) + (x_m / s))); end
\begin{array}{l}
x_m = \left|x\right|
\\
0.5 \cdot \frac{\frac{1}{s}}{2 + \frac{x\_m}{s}}
\end{array}
Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.7%
Taylor expanded in x around 0 99.7%
+-commutative99.7%
exp-prod99.7%
rem-square-sqrt44.5%
fabs-sqr44.5%
rem-square-sqrt96.2%
exp-prod96.2%
neg-mul-196.2%
distribute-neg-frac296.2%
Simplified96.2%
add-sqr-sqrt-0.0%
add-sqr-sqrt96.2%
add-sqr-sqrt44.4%
fabs-sqr44.4%
add-sqr-sqrt58.4%
*-rgt-identity58.4%
frac-times58.4%
div-inv58.4%
div-inv58.4%
unpow258.4%
times-frac59.5%
Applied egg-rr67.6%
Taylor expanded in x around 0 56.7%
Taylor expanded in x around 0 51.5%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 0.25 s))
x_m = fabs(x);
float code(float x_m, float s) {
return 0.25f / s;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 0.25e0 / s
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(0.25) / s) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(0.25) / s; end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{0.25}{s}
\end{array}
Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.7%
Taylor expanded in s around inf 28.1%
herbie shell --seed 2024172
(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))))))