
(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}
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ (- x_m) s)))) (/ (/ t_0 (+ t_0 1.0)) (+ s (/ s (exp (/ x_m s)))))))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((-x_m / s));
return (t_0 / (t_0 + 1.0f)) / (s + (s / 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
real(4) :: t_0
t_0 = exp((-x_m / s))
code = (t_0 / (t_0 + 1.0e0)) / (s + (s / exp((x_m / s))))
end function
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(Float32(-x_m) / s)) return Float32(Float32(t_0 / Float32(t_0 + Float32(1.0))) / Float32(s + Float32(s / exp(Float32(x_m / s))))) end
x_m = abs(x); function tmp = code(x_m, s) t_0 = exp((-x_m / s)); tmp = (t_0 / (t_0 + single(1.0))) / (s + (s / exp((x_m / s)))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{-x\_m}{s}}\\
\frac{\frac{t\_0}{t\_0 + 1}}{s + \frac{s}{e^{\frac{x\_m}{s}}}}
\end{array}
\end{array}
Initial program 99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
distribute-lft-in99.2%
*-rgt-identity99.2%
+-commutative99.2%
Simplified99.2%
Taylor expanded in x around 0 99.2%
associate-/r*99.3%
Simplified64.5%
Final simplification64.5%
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(Float32(-x_m) / s)) return Float32(t_0 / Float32(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{t\_0}{s \cdot {\left(t\_0 + 1\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%
exp-prod99.2%
rem-square-sqrt53.6%
fabs-sqr53.6%
rem-square-sqrt63.2%
exp-prod63.2%
neg-mul-163.2%
distribute-neg-frac263.2%
Simplified63.8%
Taylor expanded in x around inf 64.1%
mul-1-neg64.1%
distribute-frac-neg64.1%
Applied egg-rr64.1%
Final simplification64.1%
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(Float32(-x_m) / 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.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%
exp-prod99.2%
rem-square-sqrt53.6%
fabs-sqr53.6%
rem-square-sqrt63.2%
exp-prod63.2%
neg-mul-163.2%
distribute-neg-frac263.2%
Simplified63.8%
Final simplification63.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ (- x_m) s)))) (/ (/ t_0 (+ t_0 1.0)) (+ s (/ s (+ 1.0 (/ x_m s)))))))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((-x_m / s));
return (t_0 / (t_0 + 1.0f)) / (s + (s / (1.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
real(4) :: t_0
t_0 = exp((-x_m / s))
code = (t_0 / (t_0 + 1.0e0)) / (s + (s / (1.0e0 + (x_m / s))))
end function
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(Float32(-x_m) / s)) return Float32(Float32(t_0 / Float32(t_0 + Float32(1.0))) / Float32(s + Float32(s / Float32(Float32(1.0) + Float32(x_m / s))))) end
x_m = abs(x); function tmp = code(x_m, s) t_0 = exp((-x_m / s)); tmp = (t_0 / (t_0 + single(1.0))) / (s + (s / (single(1.0) + (x_m / s)))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{-x\_m}{s}}\\
\frac{\frac{t\_0}{t\_0 + 1}}{s + \frac{s}{1 + \frac{x\_m}{s}}}
\end{array}
\end{array}
Initial program 99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
distribute-lft-in99.2%
*-rgt-identity99.2%
+-commutative99.2%
Simplified99.2%
Taylor expanded in x around 0 99.2%
associate-/r*99.3%
Simplified64.5%
Taylor expanded in x around 0 60.9%
Final simplification60.9%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ (- x_m) s)))) (/ (/ t_0 (+ t_0 1.0)) (+ s s))))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((-x_m / s));
return (t_0 / (t_0 + 1.0f)) / (s + 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((-x_m / s))
code = (t_0 / (t_0 + 1.0e0)) / (s + s)
end function
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(Float32(-x_m) / s)) return Float32(Float32(t_0 / Float32(t_0 + Float32(1.0))) / Float32(s + s)) end
x_m = abs(x); function tmp = code(x_m, s) t_0 = exp((-x_m / s)); tmp = (t_0 / (t_0 + single(1.0))) / (s + s); end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{-x\_m}{s}}\\
\frac{\frac{t\_0}{t\_0 + 1}}{s + s}
\end{array}
\end{array}
Initial program 99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
distribute-lft-in99.2%
*-rgt-identity99.2%
+-commutative99.2%
Simplified99.2%
Taylor expanded in x around 0 99.2%
associate-/r*99.3%
Simplified64.5%
Taylor expanded in x around 0 59.7%
Final simplification59.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ (exp (/ (- x_m) s)) 2.0) (+ s (/ s (exp (/ x_m s))))))
x_m = fabs(x);
float code(float x_m, float s) {
return (expf((-x_m / s)) / 2.0f) / (s + (s / 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 = (exp((-x_m / s)) / 2.0e0) / (s + (s / exp((x_m / s))))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(exp(Float32(Float32(-x_m) / s)) / Float32(2.0)) / Float32(s + Float32(s / exp(Float32(x_m / s))))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (exp((-x_m / s)) / single(2.0)) / (s + (s / exp((x_m / s)))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{e^{\frac{-x\_m}{s}}}{2}}{s + \frac{s}{e^{\frac{x\_m}{s}}}}
\end{array}
Initial program 99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
distribute-lft-in99.2%
*-rgt-identity99.2%
+-commutative99.2%
Simplified99.2%
Taylor expanded in x around 0 99.2%
associate-/r*99.3%
Simplified64.5%
Taylor expanded in x around 0 59.7%
Final simplification59.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 1.0 (* 4.0 (* s (exp (/ x_m s))))))
x_m = fabs(x);
float code(float x_m, float s) {
return 1.0f / (4.0f * (s * 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 = 1.0e0 / (4.0e0 * (s * exp((x_m / s))))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(1.0) / Float32(Float32(4.0) * Float32(s * exp(Float32(x_m / s))))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(1.0) / (single(4.0) * (s * exp((x_m / s)))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{1}{4 \cdot \left(s \cdot e^{\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%
Taylor expanded in x around 0 99.3%
associate-/r*99.2%
exp-prod99.2%
rem-square-sqrt53.6%
fabs-sqr53.6%
rem-square-sqrt63.2%
exp-prod63.2%
neg-mul-163.2%
distribute-neg-frac263.2%
Simplified63.8%
Taylor expanded in x around 0 60.5%
clear-num60.5%
inv-pow60.5%
Applied egg-rr60.5%
unpow-160.5%
Simplified60.5%
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(Float32(-x_m) / 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.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%
exp-prod99.2%
rem-square-sqrt53.6%
fabs-sqr53.6%
rem-square-sqrt63.2%
exp-prod63.2%
neg-mul-163.2%
distribute-neg-frac263.2%
Simplified63.8%
Taylor expanded in x around 0 60.5%
Final simplification60.5%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ (+ (* 0.25 (+ s (* x_m 0.5))) (* x_m -0.125)) s) s))
x_m = fabs(x);
float code(float x_m, float s) {
return (((0.25f * (s + (x_m * 0.5f))) + (x_m * -0.125f)) / 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.25e0 * (s + (x_m * 0.5e0))) + (x_m * (-0.125e0))) / s) / s
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(Float32(Float32(0.25) * Float32(s + Float32(x_m * Float32(0.5)))) + Float32(x_m * Float32(-0.125))) / s) / s) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (((single(0.25) * (s + (x_m * single(0.5)))) + (x_m * single(-0.125))) / s) / s; end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{0.25 \cdot \left(s + x\_m \cdot 0.5\right) + x\_m \cdot -0.125}{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-rr87.1%
associate-*r/87.1%
*-rgt-identity87.1%
Simplified87.1%
Taylor expanded in s around inf 66.2%
Simplified23.6%
Taylor expanded in s around 0 88.9%
cancel-sign-sub-inv88.9%
distribute-lft-out88.9%
distribute-rgt1-in88.9%
metadata-eval88.9%
*-commutative88.9%
metadata-eval88.9%
*-commutative88.9%
Simplified88.9%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= x_m 3.99999992980668e-14) (/ 0.25 s) 0.0))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (x_m <= 3.99999992980668e-14f) {
tmp = 0.25f / s;
} else {
tmp = 0.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 <= 3.99999992980668e-14) then
tmp = 0.25e0 / s
else
tmp = 0.0e0
end if
code = tmp
end function
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (x_m <= Float32(3.99999992980668e-14)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(0.0); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, s) tmp = single(0.0); if (x_m <= single(3.99999992980668e-14)) tmp = single(0.25) / s; else tmp = single(0.0); end tmp_2 = tmp; end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 3.99999992980668 \cdot 10^{-14}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 3.99999993e-14Initial 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 34.6%
if 3.99999993e-14 < x 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%
Applied egg-rr67.0%
associate-*r/67.1%
*-rgt-identity67.1%
Simplified67.1%
Taylor expanded in s around inf 59.6%
Simplified6.4%
Taylor expanded in s around 0 94.1%
div-sub63.6%
associate-*r/61.6%
distribute-rgt1-in61.6%
metadata-eval61.6%
*-commutative61.6%
associate-*r/47.5%
associate-*l*48.5%
metadata-eval48.5%
distribute-lft-neg-in48.5%
*-commutative48.5%
metadata-eval48.5%
associate-*l*48.5%
*-commutative48.5%
*-commutative48.5%
*-commutative48.5%
associate-*r*48.5%
distribute-rgt-neg-in48.5%
metadata-eval48.5%
associate-*r/63.6%
+-inverses94.1%
Simplified94.1%
Taylor expanded in s around 0 94.1%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 0.0)
x_m = fabs(x);
float code(float x_m, float s) {
return 0.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.0e0
end function
x_m = abs(x) function code(x_m, s) return Float32(0.0) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(0.0); end
\begin{array}{l}
x_m = \left|x\right|
\\
0
\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-rr87.1%
associate-*r/87.1%
*-rgt-identity87.1%
Simplified87.1%
Taylor expanded in s around inf 66.2%
Simplified23.6%
Taylor expanded in s around 0 75.2%
div-sub53.7%
associate-*r/53.0%
distribute-rgt1-in53.0%
metadata-eval53.0%
*-commutative53.0%
associate-*r/37.7%
associate-*l*38.1%
metadata-eval38.1%
distribute-lft-neg-in38.1%
*-commutative38.1%
metadata-eval38.1%
associate-*l*38.1%
*-commutative38.1%
*-commutative38.1%
*-commutative38.1%
associate-*r*38.1%
distribute-rgt-neg-in38.1%
metadata-eval38.1%
associate-*r/53.3%
+-inverses75.2%
Simplified75.2%
Taylor expanded in s around 0 75.2%
herbie shell --seed 2024137
(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))))))