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
Address Tim's review comments.
* Made test for the zero argument case explicit.
* Use exactly representable numbers in the extreme small value tests.
* Guard against a malicious __float__ that succeeds on the first call
  and fails on the second.
  • Loading branch information
rhettinger committed Jul 27, 2018
commit 43471a9a33a0439846c5f379dc0dacb44b1692bf
3 changes: 2 additions & 1 deletion Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ def testHypot(self):
# Test corner cases
self.assertEqual(hypot(0.0, 0.0), 0.0) # Max input is zero
self.assertEqual(hypot(-10.5), 10.5) # Negative input
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add self.assertEqual(hypot(), 0.0)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, added an explict test for this case. FWIW, this was already tested in the section "Test different numbers of arguments (from zero to five)".

self.assertEqual(hypot(), 0.0) # Negative input

# Test handling of bad arguments
with self.assertRaises(TypeError): # Reject keyword args
Expand Down Expand Up @@ -781,7 +782,7 @@ def testHypot(self):
# Verify scaling for extremely small values
for exp in range(32):
scale = FLOAT_MIN / 2.0 ** exp
self.assertEqual(math.hypot(12*scale, 5*scale), 13*scale)
self.assertEqual(math.hypot(4*scale, 3*scale), 5*scale)

def testLdexp(self):
self.assertRaises(TypeError, math.ldexp)
Expand Down
6 changes: 5 additions & 1 deletion Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,11 @@ math_hypot(PyObject *self, PyObject *args)
}
for (i=0 ; i<n ; i++) {
item = PyTuple_GET_ITEM(args, i);
x = PyFloat_AsDouble(item) / max;
x = PyFloat_AsDouble(item);
if (x == -1.0 && PyErr_Occurred()) {
return NULL;
}
x /= max;
csum += x * x;
}
return PyFloat_FromDouble(max * sqrt(csum)); // XXX Handle overflow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good enough for me that +Inf will result from overflow here, but that it is a change from, e.g., what 3.7 does on Windows:

>>> import sys
>>> import math
>>> x = sys.float_info.max
>>> math.hypot(x, x)
Traceback (most recent call last):
    ...
OverflowError: math range error

Copy link
Contributor Author

@rhettinger rhettinger Jul 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is good enough for me too :-) I like that it matches the behavior of the pure python code:

>>> import sys
>>> coordinates = [sys.float_info.max] * 1
>>> scale * sqrt(sum((x/scale) ** x for x in coordinates))
1.7976931348623157e+308
>>> coordinates = [sys.float_info.max] * 2
>>> scale * sqrt(sum((x/scale) ** x for x in coordinates))
inf

Expand Down