
(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}
x_m = (fabs.f32 x)
(FPCore (x_m s)
:precision binary32
(let* ((t_0 (exp (/ x_m s))))
(if (<= (fabs x_m) 0.20000000298023224)
(/ (exp (+ (/ x_m s) (* -2.0 (log1p t_0)))) s)
(/ (/ 0.5 s) (+ 1.0 t_0)))))x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((x_m / s));
float tmp;
if (fabsf(x_m) <= 0.20000000298023224f) {
tmp = expf(((x_m / s) + (-2.0f * log1pf(t_0)))) / s;
} else {
tmp = (0.5f / s) / (1.0f + t_0);
}
return tmp;
}
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(x_m / s)) tmp = Float32(0.0) if (abs(x_m) <= Float32(0.20000000298023224)) tmp = Float32(exp(Float32(Float32(x_m / s) + Float32(Float32(-2.0) * log1p(t_0)))) / s); else tmp = Float32(Float32(Float32(0.5) / s) / Float32(Float32(1.0) + t_0)); end return tmp end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{x\_m}{s}}\\
\mathbf{if}\;\left|x\_m\right| \leq 0.20000000298023224:\\
\;\;\;\;\frac{e^{\frac{x\_m}{s} + -2 \cdot \mathsf{log1p}\left(t\_0\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{0.5}{s}}{1 + t\_0}\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.200000003Initial program 98.4%
fabs-neg98.4%
distribute-frac-neg98.4%
distribute-frac-neg298.4%
fabs-neg98.4%
*-commutative98.4%
fabs-neg98.4%
+-commutative98.4%
fabs-neg98.4%
Simplified98.6%
Applied egg-rr78.2%
associate-*l/79.1%
add-exp-log79.0%
prod-exp97.9%
pow-flip97.9%
log-pow98.5%
metadata-eval98.5%
log1p-define98.8%
Applied egg-rr98.8%
if 0.200000003 < (fabs.f32 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%
Applied egg-rr45.5%
Taylor expanded in x around 0 56.5%
associate-*l/56.5%
*-un-lft-identity56.5%
Applied egg-rr56.5%
Final simplification78.5%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ (fabs x_m) (- s))))) (/ t_0 (* (+ 1.0 t_0) (+ s (/ s (exp (/ (fabs x_m) s))))))))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((fabsf(x_m) / -s));
return t_0 / ((1.0f + t_0) * (s + (s / expf((fabsf(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
real(4) :: t_0
t_0 = exp((abs(x_m) / -s))
code = t_0 / ((1.0e0 + t_0) * (s + (s / exp((abs(x_m) / s)))))
end function
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(abs(x_m) / Float32(-s))) return Float32(t_0 / Float32(Float32(Float32(1.0) + t_0) * Float32(s + Float32(s / exp(Float32(abs(x_m) / s)))))) end
x_m = abs(x); function tmp = code(x_m, s) t_0 = exp((abs(x_m) / -s)); tmp = t_0 / ((single(1.0) + t_0) * (s + (s / exp((abs(x_m) / s))))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{\left|x\_m\right|}{-s}}\\
\frac{t\_0}{\left(1 + t\_0\right) \cdot \left(s + \frac{s}{e^{\frac{\left|x\_m\right|}{s}}}\right)}
\end{array}
\end{array}
Initial program 99.2%
Simplified99.3%
Final simplification99.3%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ (fabs x_m) (- s))))) (/ (/ t_0 s) (pow (+ 1.0 t_0) 2.0))))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((fabsf(x_m) / -s));
return (t_0 / s) / powf((1.0f + t_0), 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((abs(x_m) / -s))
code = (t_0 / s) / ((1.0e0 + t_0) ** 2.0e0)
end function
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(abs(x_m) / Float32(-s))) return Float32(Float32(t_0 / s) / (Float32(Float32(1.0) + t_0) ^ Float32(2.0))) end
x_m = abs(x); function tmp = code(x_m, s) t_0 = exp((abs(x_m) / -s)); tmp = (t_0 / s) / ((single(1.0) + t_0) ^ single(2.0)); end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{\left|x\_m\right|}{-s}}\\
\frac{\frac{t\_0}{s}}{{\left(1 + t\_0\right)}^{2}}
\end{array}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Taylor expanded in x around 0 99.3%
associate-/r*99.2%
mul-1-neg99.2%
distribute-neg-frac299.2%
+-commutative99.2%
mul-1-neg99.2%
distribute-neg-frac299.2%
Simplified99.2%
Final simplification99.2%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (* 0.5 (/ 1.0 (+ 1.0 (pow E (/ x_m s))))) s))
x_m = fabs(x);
float code(float x_m, float s) {
return (0.5f * (1.0f / (1.0f + powf(((float) M_E), (x_m / s))))) / s;
}
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(0.5) * Float32(Float32(1.0) / Float32(Float32(1.0) + (Float32(exp(1)) ^ Float32(x_m / s))))) / s) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(0.5) * (single(1.0) / (single(1.0) + (single(2.71828182845904523536) ^ (x_m / s))))) / s; end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{0.5 \cdot \frac{1}{1 + {e}^{\left(\frac{x\_m}{s}\right)}}}{s}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Applied egg-rr63.3%
Taylor expanded in x around 0 63.8%
associate-*r/63.8%
Applied egg-rr63.8%
*-un-lft-identity63.8%
exp-prod63.8%
Applied egg-rr63.8%
exp-1-e63.8%
Simplified63.8%
Final simplification63.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 0.5 s) (+ 1.0 (exp (/ x_m s)))))
x_m = fabs(x);
float code(float x_m, float s) {
return (0.5f / s) / (1.0f + expf((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 / s) / (1.0e0 + exp((x_m / s)))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(1.0) + exp(Float32(x_m / s)))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(0.5) / s) / (single(1.0) + exp((x_m / s))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{0.5}{s}}{1 + e^{\frac{x\_m}{s}}}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Applied egg-rr63.3%
Taylor expanded in x around 0 63.8%
associate-*l/63.8%
*-un-lft-identity63.8%
Applied egg-rr63.8%
Final simplification63.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ -0.5 (- -1.0 (exp (/ x_m s)))) s))
x_m = fabs(x);
float code(float x_m, float s) {
return (-0.5f / (-1.0f - expf((x_m / s)))) / 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) - exp((x_m / s)))) / s
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(-0.5) / Float32(Float32(-1.0) - exp(Float32(x_m / s)))) / s) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(-0.5) / (single(-1.0) - exp((x_m / s)))) / s; end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{-0.5}{-1 - e^{\frac{x\_m}{s}}}}{s}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Applied egg-rr63.3%
Taylor expanded in x around 0 63.8%
associate-*r/63.8%
Applied egg-rr63.8%
frac-2neg63.8%
distribute-frac-neg263.8%
associate-*l/63.8%
metadata-eval63.8%
distribute-neg-frac63.8%
metadata-eval63.8%
Applied egg-rr63.8%
Final simplification63.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 1.0 (/ s (* 0.5 (/ 1.0 (+ 2.0 (/ x_m s)))))))
x_m = fabs(x);
float code(float x_m, float s) {
return 1.0f / (s / (0.5f * (1.0f / (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 = 1.0e0 / (s / (0.5e0 * (1.0e0 / (2.0e0 + (x_m / s)))))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(1.0) / Float32(s / Float32(Float32(0.5) * Float32(Float32(1.0) / Float32(Float32(2.0) + Float32(x_m / s)))))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(1.0) / (s / (single(0.5) * (single(1.0) / (single(2.0) + (x_m / s))))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{1}{\frac{s}{0.5 \cdot \frac{1}{2 + \frac{x\_m}{s}}}}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Applied egg-rr63.3%
Taylor expanded in x around 0 63.8%
Taylor expanded in x around 0 51.5%
associate-*r/51.5%
clear-num51.5%
+-commutative51.5%
Applied egg-rr51.5%
Final simplification51.5%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= x_m 0.004999999888241291) (/ 0.25 s) (* (/ 0.5 s) (/ s x_m))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (x_m <= 0.004999999888241291f) {
tmp = 0.25f / s;
} else {
tmp = (0.5f / s) * (s / x_m);
}
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 <= 0.004999999888241291e0) then
tmp = 0.25e0 / s
else
tmp = (0.5e0 / s) * (s / x_m)
end if
code = tmp
end function
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (x_m <= Float32(0.004999999888241291)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(0.5) / s) * Float32(s / x_m)); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, s) tmp = single(0.0); if (x_m <= single(0.004999999888241291)) tmp = single(0.25) / s; else tmp = (single(0.5) / s) * (s / x_m); end tmp_2 = tmp; end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 0.004999999888241291:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{s} \cdot \frac{s}{x\_m}\\
\end{array}
\end{array}
if x < 0.00499999989Initial program 98.9%
fabs-neg98.9%
distribute-frac-neg98.9%
distribute-frac-neg298.9%
fabs-neg98.9%
*-commutative98.9%
fabs-neg98.9%
+-commutative98.9%
fabs-neg98.9%
Simplified99.0%
Taylor expanded in s around inf 40.3%
if 0.00499999989 < 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%
Applied egg-rr-0.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around 0 44.0%
Taylor expanded in x around inf 33.3%
Final simplification38.5%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 0.5 (* s (+ 2.0 (/ x_m s)))))
x_m = fabs(x);
float code(float x_m, float s) {
return 0.5f / (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 / (s * (2.0e0 + (x_m / s)))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(2.0) + Float32(x_m / s)))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(0.5) / (s * (single(2.0) + (x_m / s))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{0.5}{s \cdot \left(2 + \frac{x\_m}{s}\right)}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Applied egg-rr63.3%
Taylor expanded in x around 0 63.8%
Taylor expanded in x around 0 51.5%
clear-num51.5%
frac-times51.5%
metadata-eval51.5%
/-rgt-identity51.5%
+-commutative51.5%
Applied egg-rr51.5%
Final simplification51.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.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Taylor expanded in s around inf 31.0%
Final simplification31.0%
herbie shell --seed 2024076
(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))))))