summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2018-08-12 19:15:23 (GMT)
committerGitHub <noreply@github.com>2018-08-12 19:15:23 (GMT)
commit00414597b43b78c059f3741e7b1a98adf87a3ed3 (patch)
tree3aff9933a7f885e6da9cd8b0f2a14c26357cd3cf
parent4d12e4dc28b7c782c368bae2e8fd3815167ed37d (diff)
downloadcpython-00414597b43b78c059f3741e7b1a98adf87a3ed3.zip
cpython-00414597b43b78c059f3741e7b1a98adf87a3ed3.tar.gz
cpython-00414597b43b78c059f3741e7b1a98adf87a3ed3.tar.bz2
Add more tests and assertions for math.hypot() and math.dist() (GH-8747)
-rw-r--r--Lib/test/test_math.py17
-rw-r--r--Modules/mathmodule.c1
2 files changed, 13 insertions, 5 deletions
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index 448110b..fff82fe 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -751,6 +751,10 @@ class MathTests(unittest.TestCase):
self.assertEqual(1.0,
math.copysign(1.0, hypot(-0.0)) # Convert negative zero to positive zero
)
+ self.assertEqual( # Handling of moving max to the end
+ hypot(1.5, 1.5, 0.5),
+ hypot(1.5, 0.5, 1.5),
+ )
# Test handling of bad arguments
with self.assertRaises(TypeError): # Reject keyword args
@@ -771,7 +775,7 @@ class MathTests(unittest.TestCase):
self.assertEqual(hypot(-INF, -INF), INF)
self.assertEqual(hypot(10, -INF), INF)
- # If no infinity, any NaN gives a Nan.
+ # If no infinity, any NaN gives a NaN.
self.assertTrue(math.isnan(hypot(NAN)))
self.assertTrue(math.isnan(hypot(0, NAN)))
self.assertTrue(math.isnan(hypot(NAN, 10)))
@@ -831,9 +835,13 @@ class MathTests(unittest.TestCase):
self.assertEqual(1.0, # Convert negative zero to positive zero
math.copysign(1.0, dist((0.0,), (-0.0,)))
)
+ self.assertEqual( # Handling of moving max to the end
+ dist((1.5, 1.5, 0.5), (0, 0, 0)),
+ dist((1.5, 0.5, 1.5), (0, 0, 0))
+ )
# Verify tuple subclasses are allowed
- class T(tuple): # tuple subclas
+ class T(tuple):
pass
self.assertEqual(dist(T((1, 2, 3)), ((4, 2, -1))), 5.0)
@@ -855,8 +863,7 @@ class MathTests(unittest.TestCase):
with self.assertRaises(ValueError): # Check dimension agree
dist((1, 2, 3), (4, 5, 6, 7))
-
- # Verify that the one dimensional case equivalent to abs()
+ # Verify that the one dimensional case is equivalent to abs()
for i in range(20):
p, q = random.random(), random.random()
self.assertEqual(dist((p,), (q,)), abs(p - q))
@@ -870,7 +877,7 @@ class MathTests(unittest.TestCase):
# Any infinite difference gives positive infinity.
self.assertEqual(dist(p, q), INF)
elif any(map(math.isnan, diffs)):
- # If no infinity, any NaN gives a Nan.
+ # If no infinity, any NaN gives a NaN.
self.assertTrue(math.isnan(dist(p, q)))
# Verify scaling for extremely large values
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 896cf8d..ab49dce 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -2071,6 +2071,7 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
assert(n > 0);
for (i=0 ; i < n-1 ; i++) {
x = vec[i];
+ assert(Py_IS_FINITE(x) && x >= 0.0 && x <= max);
if (x == max) {
x = vec[n-1];
vec[n-1] = max;