Skip to content

Commit 0041459

Browse files
authored
Add more tests and assertions for math.hypot() and math.dist() (GH-8747)
1 parent 4d12e4d commit 0041459

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

Lib/test/test_math.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,10 @@ def testHypot(self):
751751
self.assertEqual(1.0,
752752
math.copysign(1.0, hypot(-0.0)) # Convert negative zero to positive zero
753753
)
754+
self.assertEqual( # Handling of moving max to the end
755+
hypot(1.5, 1.5, 0.5),
756+
hypot(1.5, 0.5, 1.5),
757+
)
754758

755759
# Test handling of bad arguments
756760
with self.assertRaises(TypeError): # Reject keyword args
@@ -771,7 +775,7 @@ def testHypot(self):
771775
self.assertEqual(hypot(-INF, -INF), INF)
772776
self.assertEqual(hypot(10, -INF), INF)
773777

774-
# If no infinity, any NaN gives a Nan.
778+
# If no infinity, any NaN gives a NaN.
775779
self.assertTrue(math.isnan(hypot(NAN)))
776780
self.assertTrue(math.isnan(hypot(0, NAN)))
777781
self.assertTrue(math.isnan(hypot(NAN, 10)))
@@ -831,9 +835,13 @@ def testDist(self):
831835
self.assertEqual(1.0, # Convert negative zero to positive zero
832836
math.copysign(1.0, dist((0.0,), (-0.0,)))
833837
)
838+
self.assertEqual( # Handling of moving max to the end
839+
dist((1.5, 1.5, 0.5), (0, 0, 0)),
840+
dist((1.5, 0.5, 1.5), (0, 0, 0))
841+
)
834842

835843
# Verify tuple subclasses are allowed
836-
class T(tuple): # tuple subclas
844+
class T(tuple):
837845
pass
838846
self.assertEqual(dist(T((1, 2, 3)), ((4, 2, -1))), 5.0)
839847

@@ -855,8 +863,7 @@ class T(tuple): # tuple subclas
855863
with self.assertRaises(ValueError): # Check dimension agree
856864
dist((1, 2, 3), (4, 5, 6, 7))
857865

858-
859-
# Verify that the one dimensional case equivalent to abs()
866+
# Verify that the one dimensional case is equivalent to abs()
860867
for i in range(20):
861868
p, q = random.random(), random.random()
862869
self.assertEqual(dist((p,), (q,)), abs(p - q))
@@ -870,7 +877,7 @@ class T(tuple): # tuple subclas
870877
# Any infinite difference gives positive infinity.
871878
self.assertEqual(dist(p, q), INF)
872879
elif any(map(math.isnan, diffs)):
873-
# If no infinity, any NaN gives a Nan.
880+
# If no infinity, any NaN gives a NaN.
874881
self.assertTrue(math.isnan(dist(p, q)))
875882

876883
# Verify scaling for extremely large values

Modules/mathmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,7 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
20712071
assert(n > 0);
20722072
for (i=0 ; i < n-1 ; i++) {
20732073
x = vec[i];
2074+
assert(Py_IS_FINITE(x) && x >= 0.0 && x <= max);
20742075
if (x == max) {
20752076
x = vec[n-1];
20762077
vec[n-1] = max;

0 commit comments

Comments
 (0)