
(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 (/ 1.0 (* s (+ (+ 2.0 (exp (/ (- (fabs x)) s))) (exp (/ (fabs x) s))))))
float code(float x, float s) {
return 1.0f / (s * ((2.0f + expf((-fabsf(x) / s))) + expf((fabsf(x) / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (s * ((2.0e0 + exp((-abs(x) / s))) + exp((abs(x) / s))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(Float32(2.0) + exp(Float32(Float32(-abs(x)) / s))) + exp(Float32(abs(x) / s))))) end
function tmp = code(x, s) tmp = single(1.0) / (s * ((single(2.0) + exp((-abs(x) / s))) + exp((abs(x) / s)))); end
\begin{array}{l}
\\
\frac{1}{s \cdot \left(\left(2 + e^{\frac{-\left|x\right|}{s}}\right) + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
Initial program 99.3%
Simplified99.3%
Taylor expanded in s around 0 99.3%
associate-+r+99.3%
+-commutative99.3%
mul-1-neg99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (x s) :precision binary32 (/ (/ (exp (/ (- (fabs x)) s)) s) (pow (+ 1.0 (exp (/ x s))) 2.0)))
float code(float x, float s) {
return (expf((-fabsf(x) / s)) / s) / powf((1.0f + expf((x / s))), 2.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (exp((-abs(x) / s)) / s) / ((1.0e0 + exp((x / s))) ** 2.0e0)
end function
function code(x, s) return Float32(Float32(exp(Float32(Float32(-abs(x)) / s)) / s) / (Float32(Float32(1.0) + exp(Float32(x / s))) ^ Float32(2.0))) end
function tmp = code(x, s) tmp = (exp((-abs(x) / s)) / s) / ((single(1.0) + exp((x / s))) ^ single(2.0)); end
\begin{array}{l}
\\
\frac{\frac{e^{\frac{-\left|x\right|}{s}}}{s}}{{\left(1 + e^{\frac{x}{s}}\right)}^{2}}
\end{array}
Initial program 99.3%
Taylor expanded in x around 0 99.2%
associate-/r*99.2%
mul-1-neg99.2%
distribute-frac-neg99.2%
+-commutative99.2%
mul-1-neg99.2%
distribute-frac-neg99.2%
Simplified99.2%
unpow299.2%
distribute-lft-in99.3%
Applied egg-rr95.5%
*-rgt-identity95.5%
distribute-lft-in95.5%
unpow295.5%
Simplified95.5%
Final simplification95.5%
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ (exp (/ (fabs x) s)) 3.0)))
float code(float x, float s) {
return (1.0f / s) / (expf((fabsf(x) / s)) + 3.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / (exp((abs(x) / s)) + 3.0e0)
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(exp(Float32(abs(x) / s)) + Float32(3.0))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / (exp((abs(x) / s)) + single(3.0)); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + 3}
\end{array}
Initial program 99.3%
Simplified99.3%
Taylor expanded in s around inf 95.2%
Final simplification95.2%
(FPCore (x s) :precision binary32 (/ (/ (exp (/ (- (fabs x)) s)) s) 4.0))
float code(float x, float s) {
return (expf((-fabsf(x) / s)) / 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
end function
function code(x, s) return Float32(Float32(exp(Float32(Float32(-abs(x)) / s)) / s) / Float32(4.0)) end
function tmp = code(x, s) tmp = (exp((-abs(x) / s)) / s) / single(4.0); end
\begin{array}{l}
\\
\frac{\frac{e^{\frac{-\left|x\right|}{s}}}{s}}{4}
\end{array}
Initial program 99.3%
Taylor expanded in x around 0 99.2%
associate-/r*99.2%
mul-1-neg99.2%
distribute-frac-neg99.2%
+-commutative99.2%
mul-1-neg99.2%
distribute-frac-neg99.2%
Simplified99.2%
Taylor expanded in s around inf 92.9%
Final simplification92.9%
(FPCore (x s) :precision binary32 (if (<= x -0.0020000000949949026) (pow (* x -262144.0) -262144.0) (/ 1.0 (* s (+ 2.0 (* 2.0 (exp (/ x s))))))))
float code(float x, float s) {
float tmp;
if (x <= -0.0020000000949949026f) {
tmp = powf((x * -262144.0f), -262144.0f);
} else {
tmp = 1.0f / (s * (2.0f + (2.0f * expf((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.0020000000949949026e0)) then
tmp = (x * (-262144.0e0)) ** (-262144.0e0)
else
tmp = 1.0e0 / (s * (2.0e0 + (2.0e0 * exp((x / s)))))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-0.0020000000949949026)) tmp = Float32(x * Float32(-262144.0)) ^ Float32(-262144.0); else tmp = Float32(Float32(1.0) / Float32(s * Float32(Float32(2.0) + Float32(Float32(2.0) * exp(Float32(x / s)))))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-0.0020000000949949026)) tmp = (x * single(-262144.0)) ^ single(-262144.0); else tmp = single(1.0) / (s * (single(2.0) + (single(2.0) * exp((x / s))))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.0020000000949949026:\\
\;\;\;\;{\left(x \cdot -262144\right)}^{-262144}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{s \cdot \left(2 + 2 \cdot e^{\frac{x}{s}}\right)}\\
\end{array}
\end{array}
if x < -0.00200000009Initial program 100.0%
Taylor expanded in s around inf 100.0%
Applied egg-rr100.0%
*-commutative100.0%
Simplified100.0%
if -0.00200000009 < x Initial program 99.0%
Simplified99.1%
Taylor expanded in s around 0 99.1%
associate-+r+99.1%
+-commutative99.1%
mul-1-neg99.1%
Simplified99.1%
add-sqr-sqrt99.1%
Applied egg-rr99.1%
distribute-lft-in99.1%
Applied egg-rr83.9%
distribute-lft-out83.9%
associate-+r+83.8%
count-283.8%
Simplified83.8%
Final simplification88.3%
(FPCore (x s) :precision binary32 (if (<= x -0.0020000000949949026) (pow (* x -262144.0) -262144.0) (/ (/ 1.0 (* s (exp (/ x s)))) 4.0)))
float code(float x, float s) {
float tmp;
if (x <= -0.0020000000949949026f) {
tmp = powf((x * -262144.0f), -262144.0f);
} else {
tmp = (1.0f / (s * expf((x / s)))) / 4.0f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= (-0.0020000000949949026e0)) then
tmp = (x * (-262144.0e0)) ** (-262144.0e0)
else
tmp = (1.0e0 / (s * exp((x / s)))) / 4.0e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(-0.0020000000949949026)) tmp = Float32(x * Float32(-262144.0)) ^ Float32(-262144.0); else tmp = Float32(Float32(Float32(1.0) / Float32(s * exp(Float32(x / s)))) / Float32(4.0)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(-0.0020000000949949026)) tmp = (x * single(-262144.0)) ^ single(-262144.0); else tmp = (single(1.0) / (s * exp((x / s)))) / single(4.0); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.0020000000949949026:\\
\;\;\;\;{\left(x \cdot -262144\right)}^{-262144}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{s \cdot e^{\frac{x}{s}}}}{4}\\
\end{array}
\end{array}
if x < -0.00200000009Initial program 100.0%
Taylor expanded in s around inf 100.0%
Applied egg-rr100.0%
*-commutative100.0%
Simplified100.0%
if -0.00200000009 < x Initial program 99.0%
Taylor expanded in x around 0 98.9%
associate-/r*98.9%
mul-1-neg98.9%
distribute-frac-neg98.9%
+-commutative98.9%
mul-1-neg98.9%
distribute-frac-neg98.9%
Simplified98.9%
clear-num98.9%
inv-pow98.9%
Applied egg-rr87.5%
unpow-187.5%
Simplified87.5%
Taylor expanded in s around inf 83.2%
Final simplification87.8%
(FPCore (x s)
:precision binary32
(let* ((t_0 (pow (* x -262144.0) -262144.0)))
(if (<= x -0.20000000298023224)
t_0
(if (<= x 4.0000000781659255e-25)
(/ (/ 1.0 s) (+ 4.0 (* (/ x s) (/ x s))))
(if (<= x 0.0010000000474974513)
(/ (/ 1.0 s) (+ 4.0 (/ (* x x) (* s s))))
t_0)))))
float code(float x, float s) {
float t_0 = powf((x * -262144.0f), -262144.0f);
float tmp;
if (x <= -0.20000000298023224f) {
tmp = t_0;
} else if (x <= 4.0000000781659255e-25f) {
tmp = (1.0f / s) / (4.0f + ((x / s) * (x / s)));
} else if (x <= 0.0010000000474974513f) {
tmp = (1.0f / s) / (4.0f + ((x * x) / (s * s)));
} else {
tmp = t_0;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: tmp
t_0 = (x * (-262144.0e0)) ** (-262144.0e0)
if (x <= (-0.20000000298023224e0)) then
tmp = t_0
else if (x <= 4.0000000781659255e-25) then
tmp = (1.0e0 / s) / (4.0e0 + ((x / s) * (x / s)))
else if (x <= 0.0010000000474974513e0) then
tmp = (1.0e0 / s) / (4.0e0 + ((x * x) / (s * s)))
else
tmp = t_0
end if
code = tmp
end function
function code(x, s) t_0 = Float32(x * Float32(-262144.0)) ^ Float32(-262144.0) tmp = Float32(0.0) if (x <= Float32(-0.20000000298023224)) tmp = t_0; elseif (x <= Float32(4.0000000781659255e-25)) tmp = Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x / s) * Float32(x / s)))); elseif (x <= Float32(0.0010000000474974513)) tmp = Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x * x) / Float32(s * s)))); else tmp = t_0; end return tmp end
function tmp_2 = code(x, s) t_0 = (x * single(-262144.0)) ^ single(-262144.0); tmp = single(0.0); if (x <= single(-0.20000000298023224)) tmp = t_0; elseif (x <= single(4.0000000781659255e-25)) tmp = (single(1.0) / s) / (single(4.0) + ((x / s) * (x / s))); elseif (x <= single(0.0010000000474974513)) tmp = (single(1.0) / s) / (single(4.0) + ((x * x) / (s * s))); else tmp = t_0; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {\left(x \cdot -262144\right)}^{-262144}\\
\mathbf{if}\;x \leq -0.20000000298023224:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq 4.0000000781659255 \cdot 10^{-25}:\\
\;\;\;\;\frac{\frac{1}{s}}{4 + \frac{x}{s} \cdot \frac{x}{s}}\\
\mathbf{elif}\;x \leq 0.0010000000474974513:\\
\;\;\;\;\frac{\frac{1}{s}}{4 + \frac{x \cdot x}{s \cdot s}}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if x < -0.200000003 or 0.00100000005 < x Initial program 100.0%
Taylor expanded in s around inf 100.0%
Applied egg-rr100.0%
*-commutative100.0%
Simplified100.0%
if -0.200000003 < x < 4.00000008e-25Initial program 98.9%
Simplified99.1%
Taylor expanded in s around 0 99.2%
associate-/r*99.1%
associate-+r+99.1%
+-commutative99.1%
mul-1-neg99.1%
distribute-frac-neg99.1%
associate-+l+99.1%
Simplified99.1%
Taylor expanded in s around inf 50.0%
associate-+r+50.0%
distribute-lft1-in50.0%
metadata-eval50.0%
mul0-lft50.0%
+-rgt-identity50.0%
+-commutative50.0%
unpow250.0%
sqr-abs50.0%
unpow250.0%
times-frac70.5%
Simplified70.5%
if 4.00000008e-25 < x < 0.00100000005Initial program 96.8%
Simplified96.8%
Taylor expanded in s around 0 96.7%
associate-/r*96.7%
associate-+r+96.8%
+-commutative96.8%
mul-1-neg96.8%
distribute-frac-neg96.8%
associate-+l+96.8%
Simplified96.8%
Taylor expanded in s around inf 77.7%
associate-+r+77.7%
distribute-lft1-in77.7%
metadata-eval77.7%
mul0-lft77.7%
+-rgt-identity77.7%
+-commutative77.7%
unpow277.7%
sqr-abs77.7%
unpow277.7%
Simplified77.7%
Final simplification88.7%
(FPCore (x s) :precision binary32 (if (<= x 4.0000000781659255e-25) (/ (/ 1.0 s) (+ 4.0 (* (/ x s) (/ x s)))) (/ (/ 1.0 s) (+ 4.0 (/ (* x x) (* s s))))))
float code(float x, float s) {
float tmp;
if (x <= 4.0000000781659255e-25f) {
tmp = (1.0f / s) / (4.0f + ((x / s) * (x / s)));
} else {
tmp = (1.0f / s) / (4.0f + ((x * x) / (s * s)));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 4.0000000781659255e-25) then
tmp = (1.0e0 / s) / (4.0e0 + ((x / s) * (x / s)))
else
tmp = (1.0e0 / s) / (4.0e0 + ((x * x) / (s * s)))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(4.0000000781659255e-25)) tmp = Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x / s) * Float32(x / s)))); else tmp = Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x * x) / Float32(s * s)))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(4.0000000781659255e-25)) tmp = (single(1.0) / s) / (single(4.0) + ((x / s) * (x / s))); else tmp = (single(1.0) / s) / (single(4.0) + ((x * x) / (s * s))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.0000000781659255 \cdot 10^{-25}:\\
\;\;\;\;\frac{\frac{1}{s}}{4 + \frac{x}{s} \cdot \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{s}}{4 + \frac{x \cdot x}{s \cdot s}}\\
\end{array}
\end{array}
if x < 4.00000008e-25Initial program 99.4%
Simplified99.5%
Taylor expanded in s around 0 99.6%
associate-/r*99.5%
associate-+r+99.5%
+-commutative99.5%
mul-1-neg99.5%
distribute-frac-neg99.5%
associate-+l+99.5%
Simplified99.5%
Taylor expanded in s around inf 46.4%
associate-+r+46.4%
distribute-lft1-in46.4%
metadata-eval46.4%
mul0-lft68.4%
+-rgt-identity68.4%
+-commutative68.4%
unpow268.4%
sqr-abs68.4%
unpow268.4%
times-frac79.0%
Simplified79.0%
if 4.00000008e-25 < x Initial program 99.1%
Simplified99.1%
Taylor expanded in s around 0 99.1%
associate-/r*99.1%
associate-+r+99.1%
+-commutative99.1%
mul-1-neg99.1%
distribute-frac-neg99.1%
associate-+l+99.1%
Simplified99.1%
Taylor expanded in s around inf 53.7%
associate-+r+53.7%
distribute-lft1-in53.7%
metadata-eval53.7%
mul0-lft84.1%
+-rgt-identity84.1%
+-commutative84.1%
unpow284.1%
sqr-abs84.1%
unpow284.1%
Simplified84.1%
Final simplification81.3%
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ 4.0 (* (/ x s) (/ x s)))))
float code(float x, float s) {
return (1.0f / s) / (4.0f + ((x / s) * (x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / (4.0e0 + ((x / s) * (x / s)))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x / s) * Float32(x / s)))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / (single(4.0) + ((x / s) * (x / s))); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{4 + \frac{x}{s} \cdot \frac{x}{s}}
\end{array}
Initial program 99.3%
Simplified99.3%
Taylor expanded in s around 0 99.3%
associate-/r*99.3%
associate-+r+99.3%
+-commutative99.3%
mul-1-neg99.3%
distribute-frac-neg99.3%
associate-+l+99.3%
Simplified99.3%
Taylor expanded in s around inf 49.7%
associate-+r+49.7%
distribute-lft1-in49.7%
metadata-eval49.7%
mul0-lft75.5%
+-rgt-identity75.5%
+-commutative75.5%
unpow275.5%
sqr-abs75.5%
unpow275.5%
times-frac78.0%
Simplified78.0%
Final simplification78.0%
(FPCore (x s) :precision binary32 (* x 262144.0))
float code(float x, float s) {
return x * 262144.0f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = x * 262144.0e0
end function
function code(x, s) return Float32(x * Float32(262144.0)) end
function tmp = code(x, s) tmp = x * single(262144.0); end
\begin{array}{l}
\\
x \cdot 262144
\end{array}
Initial program 99.3%
Taylor expanded in s around inf 93.9%
Applied egg-rr4.9%
*-commutative4.9%
fma-udef4.9%
*-commutative4.9%
associate-*r*4.9%
distribute-rgt-out4.9%
distribute-lft-out5.2%
metadata-eval5.2%
metadata-eval5.2%
metadata-eval5.2%
Simplified5.2%
Final simplification5.2%
(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.3%
Simplified99.3%
Taylor expanded in s around inf 25.6%
Final simplification25.6%
herbie shell --seed 2023189
(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))))))