]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Factor-out constant calculation. (GH-29491)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Tue, 9 Nov 2021 16:30:06 +0000 (10:30 -0600)
committerGitHub <noreply@github.com>
Tue, 9 Nov 2021 16:30:06 +0000 (10:30 -0600)
Lib/statistics.py

index 13e5fe7fc0e861cd3669e6a0392a39a901cd02f8..e67c51709136a16eec9164db2306eae69e4dd5ae 100644 (file)
@@ -139,6 +139,8 @@ from math import hypot, sqrt, fabs, exp, erf, tau, log, fsum
 from operator import itemgetter, mul
 from collections import Counter, namedtuple
 
+_SQRT2 = sqrt(2.0)
+
 # === Exceptions ===
 
 class StatisticsError(ValueError):
@@ -1102,7 +1104,7 @@ class NormalDist:
         "Cumulative distribution function.  P(X <= x)"
         if not self._sigma:
             raise StatisticsError('cdf() not defined when sigma is zero')
-        return 0.5 * (1.0 + erf((x - self._mu) / (self._sigma * sqrt(2.0))))
+        return 0.5 * (1.0 + erf((x - self._mu) / (self._sigma * _SQRT2)))
 
     def inv_cdf(self, p):
         """Inverse cumulative distribution function.  x : P(X <= x) = p
@@ -1158,7 +1160,7 @@ class NormalDist:
         dv = Y_var - X_var
         dm = fabs(Y._mu - X._mu)
         if not dv:
-            return 1.0 - erf(dm / (2.0 * X._sigma * sqrt(2.0)))
+            return 1.0 - erf(dm / (2.0 * X._sigma * _SQRT2))
         a = X._mu * Y_var - Y._mu * X_var
         b = X._sigma * Y._sigma * sqrt(dm * dm + dv * log(Y_var / X_var))
         x1 = (a + b) / dv