
(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_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.5%
Simplified99.6%
Final simplification99.6%
(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.5%
Simplified99.6%
*-un-lft-identity99.6%
times-frac99.5%
Applied egg-rr60.0%
Taylor expanded in x around 0 59.5%
*-un-lft-identity59.5%
exp-prod59.5%
Applied egg-rr59.5%
exp-1-e59.5%
Simplified59.5%
Final simplification59.5%
(FPCore (x s) :precision binary32 (* (/ 0.5 s) (/ 1.0 (+ 1.0 (exp (/ x s))))))
float code(float x, float s) {
return (0.5f / 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 = (0.5e0 / s) * (1.0e0 / (1.0e0 + exp((x / s))))
end function
function code(x, s) return Float32(Float32(Float32(0.5) / s) * Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = (single(0.5) / s) * (single(1.0) / (single(1.0) + exp((x / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s} \cdot \frac{1}{1 + e^{\frac{x}{s}}}
\end{array}
Initial program 99.5%
Simplified99.6%
*-un-lft-identity99.6%
times-frac99.5%
Applied egg-rr60.0%
Taylor expanded in x around 0 59.5%
Final simplification59.5%
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ 1.0 (exp (/ x s))))))
float code(float x, float s) {
return 0.5f / (s * (1.0f + expf((x / s))));
}
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
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = single(0.5) / (s * (single(1.0) + exp((x / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.5%
Simplified99.6%
*-un-lft-identity99.6%
times-frac99.5%
Applied egg-rr60.0%
Taylor expanded in x around 0 59.5%
clear-num59.5%
frac-times59.5%
metadata-eval59.5%
/-rgt-identity59.5%
+-commutative59.5%
Applied egg-rr59.5%
Final simplification59.5%
(FPCore (x s) :precision binary32 (if (<= x 0.029999999329447746) (/ 0.25 s) (* (/ 0.5 s) (/ 1.0 (/ x s)))))
float code(float x, float s) {
float tmp;
if (x <= 0.029999999329447746f) {
tmp = 0.25f / s;
} else {
tmp = (0.5f / s) * (1.0f / (x / s));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 0.029999999329447746e0) then
tmp = 0.25e0 / s
else
tmp = (0.5e0 / s) * (1.0e0 / (x / s))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.029999999329447746)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(0.5) / s) * Float32(Float32(1.0) / Float32(x / s))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.029999999329447746)) tmp = single(0.25) / s; else tmp = (single(0.5) / s) * (single(1.0) / (x / s)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.029999999329447746:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{s} \cdot \frac{1}{\frac{x}{s}}\\
\end{array}
\end{array}
if x < 0.0299999993Initial program 99.3%
Simplified99.4%
Taylor expanded in s around inf 30.6%
if 0.0299999993 < x Initial program 100.0%
Simplified100.0%
*-un-lft-identity100.0%
times-frac100.0%
Applied egg-rr-0.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around 0 47.5%
Taylor expanded in x around inf 47.5%
Final simplification35.8%
(FPCore (x s) :precision binary32 (if (<= x 0.029999999329447746) (/ 0.25 s) (* (/ 0.5 s) (/ s x))))
float code(float x, float s) {
float tmp;
if (x <= 0.029999999329447746f) {
tmp = 0.25f / s;
} else {
tmp = (0.5f / s) * (s / 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.029999999329447746e0) then
tmp = 0.25e0 / s
else
tmp = (0.5e0 / s) * (s / x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.029999999329447746)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(0.5) / s) * Float32(s / x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.029999999329447746)) tmp = single(0.25) / s; else tmp = (single(0.5) / s) * (s / x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.029999999329447746:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{s} \cdot \frac{s}{x}\\
\end{array}
\end{array}
if x < 0.0299999993Initial program 99.3%
Simplified99.4%
Taylor expanded in s around inf 30.6%
if 0.0299999993 < x Initial program 100.0%
Simplified100.0%
*-un-lft-identity100.0%
times-frac100.0%
Applied egg-rr-0.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around 0 47.5%
Taylor expanded in x around inf 32.7%
Final simplification31.3%
(FPCore (x s) :precision binary32 (* (/ 1.0 s) (/ 0.5 (+ (/ x s) 2.0))))
float code(float x, float s) {
return (1.0f / s) * (0.5f / ((x / s) + 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) * (0.5e0 / ((x / s) + 2.0e0))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) * Float32(Float32(0.5) / Float32(Float32(x / s) + Float32(2.0)))) end
function tmp = code(x, s) tmp = (single(1.0) / s) * (single(0.5) / ((x / s) + single(2.0))); end
\begin{array}{l}
\\
\frac{1}{s} \cdot \frac{0.5}{\frac{x}{s} + 2}
\end{array}
Initial program 99.5%
Simplified99.6%
*-un-lft-identity99.6%
times-frac99.5%
Applied egg-rr60.0%
Taylor expanded in x around 0 59.5%
Taylor expanded in x around 0 47.9%
associate-*r/47.9%
clear-num47.9%
+-commutative47.9%
Applied egg-rr47.9%
associate-/r/47.9%
associate-*l/47.9%
metadata-eval47.9%
+-commutative47.9%
Simplified47.9%
Final simplification47.9%
(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.5%
Simplified99.6%
*-un-lft-identity99.6%
times-frac99.5%
Applied egg-rr60.0%
Taylor expanded in x around 0 59.5%
Taylor expanded in x around 0 47.9%
Final simplification47.9%
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ (/ x s) 2.0))))
float code(float x, float s) {
return 0.5f / (s * ((x / s) + 2.0f));
}
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
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(x / s) + Float32(2.0)))) end
function tmp = code(x, s) tmp = single(0.5) / (s * ((x / s) + single(2.0))); end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(\frac{x}{s} + 2\right)}
\end{array}
Initial program 99.5%
Simplified99.6%
*-un-lft-identity99.6%
times-frac99.5%
Applied egg-rr60.0%
Taylor expanded in x around 0 59.5%
Taylor expanded in x around 0 47.9%
frac-times47.9%
metadata-eval47.9%
+-commutative47.9%
Applied egg-rr47.9%
Final simplification47.9%
(FPCore (x s) :precision binary32 (/ (/ 0.5 s) (+ (/ x s) 2.0)))
float code(float x, float s) {
return (0.5f / s) / ((x / s) + 2.0f);
}
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
function code(x, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(x / s) + Float32(2.0))) end
function tmp = code(x, s) tmp = (single(0.5) / s) / ((x / s) + single(2.0)); end
\begin{array}{l}
\\
\frac{\frac{0.5}{s}}{\frac{x}{s} + 2}
\end{array}
Initial program 99.5%
Simplified99.6%
*-un-lft-identity99.6%
times-frac99.5%
Applied egg-rr60.0%
Taylor expanded in x around 0 59.5%
Taylor expanded in x around 0 47.9%
associate-*l/47.9%
*-un-lft-identity47.9%
+-commutative47.9%
Applied egg-rr47.9%
Final simplification47.9%
(FPCore (x s) :precision binary32 (if (<= x 0.029999999329447746) (/ 0.25 s) (/ 0.5 x)))
float code(float x, float s) {
float tmp;
if (x <= 0.029999999329447746f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / 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.029999999329447746e0) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / x
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.029999999329447746)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / x); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.029999999329447746)) tmp = single(0.25) / s; else tmp = single(0.5) / x; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.029999999329447746:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < 0.0299999993Initial program 99.3%
Simplified99.4%
Taylor expanded in s around inf 30.6%
if 0.0299999993 < x Initial program 100.0%
Simplified100.0%
*-un-lft-identity100.0%
times-frac100.0%
Applied egg-rr-0.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around 0 47.5%
Taylor expanded in x around inf 10.9%
Final simplification24.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.5%
Simplified99.6%
Taylor expanded in s around inf 22.7%
Final simplification22.7%
herbie shell --seed 2024096
(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))))))