
(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}
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 1.0 s) (* (+ 1.0 (exp (/ (- x) s))) (+ 1.0 (exp (/ (fabs x) s))))))
x = abs(x);
float code(float x, float s) {
return (1.0f / s) / ((1.0f + expf((-x / s))) * (1.0f + expf((fabsf(x) / s))));
}
NOTE: x should be positive before calling this function
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))) * (1.0e0 + exp((abs(x) / s))))
end function
x = abs(x) function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) * Float32(Float32(1.0) + exp(Float32(abs(x) / s))))) end
x = abs(x) function tmp = code(x, s) tmp = (single(1.0) / s) / ((single(1.0) + exp((-x / s))) * (single(1.0) + exp((abs(x) / s)))); end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{s}}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
*-un-lft-identity99.5%
neg-mul-199.5%
times-frac99.5%
metadata-eval99.5%
metadata-eval99.5%
times-frac99.5%
neg-mul-199.5%
*-un-lft-identity99.5%
distribute-frac-neg99.5%
rec-exp99.4%
add-sqr-sqrt45.6%
fabs-sqr45.6%
add-sqr-sqrt96.1%
Applied egg-rr96.1%
rec-exp96.1%
distribute-neg-frac96.1%
Simplified96.1%
Taylor expanded in s around 0 96.1%
associate-/r*96.2%
associate-*r/96.2%
mul-1-neg96.2%
Simplified96.2%
Final simplification96.2%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (* s (* (+ 1.0 (exp (/ (- x) s))) (+ 1.0 (exp (/ x s)))))))
x = abs(x);
float code(float x, float s) {
return 1.0f / (s * ((1.0f + expf((-x / s))) * (1.0f + expf((x / s)))));
}
NOTE: x should be positive before calling this function
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))) * (1.0e0 + exp((x / s)))))
end function
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) * Float32(Float32(1.0) + exp(Float32(x / s)))))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / (s * ((single(1.0) + exp((-x / s))) * (single(1.0) + exp((x / s))))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot \left(\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(1 + e^{\frac{x}{s}}\right)\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
*-un-lft-identity99.5%
neg-mul-199.5%
times-frac99.5%
metadata-eval99.5%
metadata-eval99.5%
times-frac99.5%
neg-mul-199.5%
*-un-lft-identity99.5%
distribute-frac-neg99.5%
rec-exp99.4%
add-sqr-sqrt45.6%
fabs-sqr45.6%
add-sqr-sqrt96.1%
Applied egg-rr96.1%
rec-exp96.1%
distribute-neg-frac96.1%
Simplified96.1%
add-sqr-sqrt45.6%
fabs-sqr45.6%
add-sqr-sqrt99.5%
expm1-log1p-u99.4%
expm1-udef99.4%
Applied egg-rr99.4%
expm1-def99.4%
expm1-log1p99.5%
Simplified99.5%
Taylor expanded in s around 0 99.4%
*-commutative99.4%
neg-mul-199.4%
Simplified99.4%
Final simplification99.4%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (* (+ 1.0 (exp (/ x s))) (+ s (* s (exp (/ (- x) s)))))))
x = abs(x);
float code(float x, float s) {
return 1.0f / ((1.0f + expf((x / s))) * (s + (s * expf((-x / s)))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((1.0e0 + exp((x / s))) * (s + (s * exp((-x / s)))))
end function
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(x / s))) * Float32(s + Float32(s * exp(Float32(Float32(-x) / s)))))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / ((single(1.0) + exp((x / s))) * (s + (s * exp((-x / s))))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot e^{\frac{-x}{s}}\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
*-un-lft-identity99.5%
neg-mul-199.5%
times-frac99.5%
metadata-eval99.5%
metadata-eval99.5%
times-frac99.5%
neg-mul-199.5%
*-un-lft-identity99.5%
distribute-frac-neg99.5%
rec-exp99.4%
add-sqr-sqrt45.6%
fabs-sqr45.6%
add-sqr-sqrt96.1%
Applied egg-rr96.1%
rec-exp96.1%
distribute-neg-frac96.1%
Simplified96.1%
add-sqr-sqrt45.6%
fabs-sqr45.6%
add-sqr-sqrt99.5%
expm1-log1p-u99.4%
expm1-udef99.4%
Applied egg-rr99.4%
expm1-def99.4%
expm1-log1p99.5%
Simplified99.5%
Taylor expanded in x around inf 99.4%
mul-1-neg99.4%
exp-neg99.4%
Applied egg-rr99.4%
rec-exp99.4%
distribute-neg-frac99.4%
Simplified99.4%
Final simplification99.4%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (+ (+ s s) (* (exp (/ x s)) (+ s s)))))
x = abs(x);
float code(float x, float s) {
return 1.0f / ((s + s) + (expf((x / s)) * (s + s)));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((s + s) + (exp((x / s)) * (s + s)))
end function
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s + s) + Float32(exp(Float32(x / s)) * Float32(s + s)))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / ((s + s) + (exp((x / s)) * (s + s))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{\left(s + s\right) + e^{\frac{x}{s}} \cdot \left(s + s\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
Taylor expanded in s around inf 94.8%
distribute-lft-in94.8%
*-rgt-identity94.8%
fma-udef94.8%
*-rgt-identity94.8%
fma-udef94.8%
*-rgt-identity94.8%
add-sqr-sqrt43.9%
fabs-sqr43.9%
add-sqr-sqrt60.5%
Applied egg-rr60.5%
Final simplification60.5%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 0.5 (* s (+ 1.0 (exp (/ x s))))))
x = abs(x);
float code(float x, float s) {
return 0.5f / (s * (1.0f + expf((x / s))));
}
NOTE: x should be positive before calling this function
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))))
end function
x = abs(x) function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + exp(Float32(x / s))))) end
x = abs(x) function tmp = code(x, s) tmp = single(0.5) / (s * (single(1.0) + exp((x / s)))); end
\begin{array}{l}
x = |x|\\
\\
\frac{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
Taylor expanded in s around inf 94.8%
add-log-exp78.1%
associate-/r*78.1%
fma-udef78.1%
*-rgt-identity78.1%
add-sqr-sqrt37.7%
fabs-sqr37.7%
add-sqr-sqrt43.6%
Applied egg-rr43.6%
Taylor expanded in s around 0 60.5%
Final simplification60.5%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (/ (* x x) s))))
x = abs(x);
float code(float x, float s) {
return 1.0f / ((s * 4.0f) + ((x * x) / s));
}
NOTE: x should be positive before calling this function
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
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(Float32(x * x) / s))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / ((s * single(4.0)) + ((x * x) / s)); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot 4 + \frac{x \cdot x}{s}}
\end{array}
Initial program 99.4%
Simplified99.5%
Taylor expanded in s around -inf 42.6%
+-commutative42.6%
mul-1-neg42.6%
distribute-lft1-in64.5%
metadata-eval64.5%
associate-*r/64.5%
mul-1-neg64.5%
remove-double-neg64.5%
associate-+r+64.5%
Simplified64.9%
Final simplification64.9%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (* x 2.0))))
x = abs(x);
float code(float x, float s) {
return 1.0f / ((s * 4.0f) + (x * 2.0f));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((s * 4.0e0) + (x * 2.0e0))
end function
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(2.0)))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / ((s * single(4.0)) + (x * single(2.0))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot 4 + x \cdot 2}
\end{array}
Initial program 99.4%
Simplified99.5%
Taylor expanded in s around inf 94.8%
distribute-lft-in94.8%
*-rgt-identity94.8%
fma-udef94.8%
*-rgt-identity94.8%
fma-udef94.8%
*-rgt-identity94.8%
add-sqr-sqrt43.9%
fabs-sqr43.9%
add-sqr-sqrt60.5%
Applied egg-rr60.5%
Taylor expanded in s around inf 29.3%
Final simplification29.3%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 0.5 s) (+ (/ x s) 2.0)))
x = abs(x);
float code(float x, float s) {
return (0.5f / s) / ((x / s) + 2.0f);
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.5e0 / s) / ((x / s) + 2.0e0)
end function
x = abs(x) function code(x, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(x / s) + Float32(2.0))) end
x = abs(x) function tmp = code(x, s) tmp = (single(0.5) / s) / ((x / s) + single(2.0)); end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{0.5}{s}}{\frac{x}{s} + 2}
\end{array}
Initial program 99.4%
Simplified99.5%
Taylor expanded in s around inf 94.8%
add-log-exp78.1%
associate-/r*78.1%
fma-udef78.1%
*-rgt-identity78.1%
add-sqr-sqrt37.7%
fabs-sqr37.7%
add-sqr-sqrt43.6%
Applied egg-rr43.6%
Taylor expanded in s around 0 60.5%
associate-/r*60.5%
Simplified60.5%
Taylor expanded in x around 0 50.5%
Final simplification50.5%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 2.0000000390829628e-24) (/ 0.25 s) 0.0))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 2.0000000390829628e-24f) {
tmp = 0.25f / s;
} else {
tmp = 0.0f;
}
return tmp;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 2.0000000390829628e-24) then
tmp = 0.25e0 / s
else
tmp = 0.0e0
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(2.0000000390829628e-24)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(0.0); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(2.0000000390829628e-24)) tmp = single(0.25) / s; else tmp = single(0.0); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.0000000390829628 \cdot 10^{-24}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 2.00000004e-24Initial program 99.1%
Simplified99.2%
Taylor expanded in s around inf 37.9%
if 2.00000004e-24 < x Initial program 99.9%
Simplified99.9%
Taylor expanded in s around inf 96.5%
add-log-exp92.3%
associate-/r*92.3%
fma-udef92.3%
*-rgt-identity92.3%
add-sqr-sqrt92.3%
fabs-sqr92.3%
add-sqr-sqrt92.3%
Applied egg-rr92.3%
Taylor expanded in s around inf 90.7%
Final simplification58.7%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 0.25 s))
x = abs(x);
float code(float x, float s) {
return 0.25f / s;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.25e0 / s
end function
x = abs(x) function code(x, s) return Float32(Float32(0.25) / s) end
x = abs(x) function tmp = code(x, s) tmp = single(0.25) / s; end
\begin{array}{l}
x = |x|\\
\\
\frac{0.25}{s}
\end{array}
Initial program 99.4%
Simplified99.5%
Taylor expanded in s around inf 27.3%
Final simplification27.3%
herbie shell --seed 2023308
(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))))))