[Python-checkins] Factor-out constant calculation. (GH-29491)

rhettinger webhook-mailer at python.org
Tue Nov 9 11:30:11 EST 2021


https://github.com/python/cpython/commit/c3bc0fe5a6e4ee50bd186eebb638e881b1c4bf54
commit: c3bc0fe5a6e4ee50bd186eebb638e881b1c4bf54
branch: main
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2021-11-09T10:30:06-06:00
summary:

Factor-out constant calculation. (GH-29491)

files:
M Lib/statistics.py

diff --git a/Lib/statistics.py b/Lib/statistics.py
index 13e5fe7fc0e86..e67c51709136a 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -139,6 +139,8 @@
 from operator import itemgetter, mul
 from collections import Counter, namedtuple
 
+_SQRT2 = sqrt(2.0)
+
 # === Exceptions ===
 
 class StatisticsError(ValueError):
@@ -1102,7 +1104,7 @@ def cdf(self, x):
         "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 @@ def overlap(self, other):
         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



More information about the Python-checkins mailing list