Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean-up comments and over-specified tests
  • Loading branch information
rhettinger committed Aug 10, 2020
commit 4742ef8aaac225f52e682e04a30882716cad14a9
3 changes: 2 additions & 1 deletion Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@ def testHypot(self):
# Verify scaling for extremely large values
fourthmax = FLOAT_MAX / 4.0
for n in range(32):
self.assertEqual(hypot(*([fourthmax]*n)), fourthmax * math.sqrt(n))
self.assertTrue(math.isclose(hypot(*([fourthmax]*n)),
Comment thread
rhettinger marked this conversation as resolved.
fourthmax * math.sqrt(n)))

# Verify scaling for extremely small values
for exp in range(32):
Expand Down
12 changes: 5 additions & 7 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2458,12 +2458,7 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
return max;
}
frexp(max, &max_e);
if (-1020 <= max_e && max_e <= 1020) {
/* Stay away from extreme values.
ldexp(1.0, -1075) returns zero.
ldexp(1.0, -1023) is subnormal.
ldexp(1.0, 1024) gives Inf.
*/
if (max_e >= -1023) {
Comment thread
rhettinger marked this conversation as resolved.
scale = ldexp(1.0, -max_e);
assert(max * scale >= 0.5);
assert(max * scale < 1.0);
Expand All @@ -2480,7 +2475,10 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
}
return sqrt(csum - 1.0 + frac) / scale;
}
/* Near extreme values, just divide by *max*. */
/* When max_e < -1023, a scale computed given by ldexp(1.0, -max_e)
would be subnormal and break range invariants for max*scale.
Instead, we just divide by *max*.
*/
for (i=0 ; i < n ; i++) {
x = vec[i];
assert(Py_IS_FINITE(x) && fabs(x) <= max);
Expand Down