Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python math.hypot() Method

Python math.hypot() Method

Python math Module Python math Module

Python math.hypot() The method returns the Euclidean norm.

The Euclidean norm is the distance from the origin to the given coordinates.

The Euclidean metric, also called Euclidean distance, is the "ordinary" (that is, straight-line) distance between two points in Euclidean space.

Before Python 3.8, this method was used to find the hypotenuse of a right triangle: sqrt(xx + yy)。

Starting with Python 3.8, this method is also used to compute the Euclidean norm. For the n-dimensional case, suppose the coordinates passed are like (x1, x2, x3, ..., xn), then the Euclidean length from the origin is computed by sqrt(x1x1 + x2x2 +x3x3 .... xnxn).

Python version: 3.8

Syntax

The syntax of the math.hypot() method is as follows:

math.hypot(x1, x2, x3, ..., xn)

Parameter Description:

Return Value

A floating-point value representing the Euclidean distance of n inputs to the origin, or the hypotenuse of a right triangle for two inputs

Example

The following example computes the hypotenuse of a right triangle:

Example

Import the math package

import

math

Set the vertical line and the base

parendicular

=

10

base

=

5

Output the hypotenuse of the right triangle

print

(

math

.

hypot

(

parendicular

,

base

)

)

Output result:


11.180339887498949

The following example computes the Euclidean norm of given coordinates:

Example

Import the math package

import

math

Output the Euclidean norm of the given coordinates

print

(

math

.

hypot

(

10

,

2

,

4

,

13

)

)

print

(

math

.

hypot

(

4

,

7

,

8

)

)

print

(

math

.

hypot

(

12

,

14

)

)

Output result:


17.0
11.357816691600547
18.439088914585774

Python math Module Python math Module