8000 bpo-33089: Multidimensional math.hypot() by rhettinger · Pull Request #8474 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-33089: Multidimensional math.hypot() #8474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Add some tests
  • Loading branch information
rhettinger committed Jul 26, 2018
commit 51c52fc6e52be5643ef3312ece2269c1748040ca
23 changes: 23 additions & 0 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,29 @@ def testHypot(self):
self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
self.assertTru 8000 e(math.isnan(math.hypot(NAN, -2.0)))

def test_multi_hypot(self):
from decimal import Decimal as D

hypot = math.mh

self.assertEqual(hypot(12.0, 5.0), 13.0) # Float inputs. Exact output.
self.assertEqual(hypot(12, 5), 13) # Int inputs
self.assertEqual(hypot(D(12), D(5)), 13) # Decimal inputs

self.assertEqual(hypot(0.0, 0.0), 0.0) # Max input is zero
self.assertEqual(hypot(-10.5), 10.5) # Negative input

with self.assertRaises(TypeError):
hypot(x=1) # Reject keyword args

# Test 0 to 4 dimensional inputs
args = math.e, math.pi, math.sqrt(2.0), math.gamma(3.5)
for i in range(len(args)+1):
self.assertAlmostEqual(
hypot(*args[:i]),
math.sqrt(sum(s**2 for s in args[:i]))
)

def testLdexp(self):
self.assertRaises(TypeError, math.ldexp)
self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
Expand Down
9 changes: 6 additions & 3 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ raised for division by zero and mod by zero.
Any infinity gives infinity
If no infinity, any NaN gives a NaN

hypot(x=1) -> TypeError // no keyword args
hypot(-10.5) -> 10.5 // flip the sign to positive
* hypot(x=1) -> TypeError // no keyword args
* hypot(-10.5) -> 10.5 // flip the sign to positive
hypot(-Inf) -> Inf; // flip the sign to positive
hypot() -> 0.0 // degrade like sum([])
* hypot() -> 0.0 // degrade like sum([]) or reduce(hypot, sides, 0.0)
* hypot(0) -> 0.0 // don't divide by zero
* hypot(3, 4) // ints converted to floats
* hypot(D('3.0'), D('4.0'))
*/

/* AC: cannot convert yet, waiting for *args support */
Expand Down
0