Python math.hypot() Method



The Python math.hypot() method is used to compute the length of a hypotenuse in a right-angled triangle.

This method was just used to find the length of hypotenuse in a right-angled triangle, in the versions prior to Python 3.8; but in versions after Python 3.8, it is also used to find the Euclidean norm.

The Euclidean norm is defined as the distance recorded between the origin and the coordinates on a Cartesian plane. Hence, it is nothing but a hypotenuse where the coordinate axes act as base and perpendicular sides of a right-angled triangle.

To understand this better, let us look at the diagram below −

Number Hypot

Note − This function is not accessible directly, so we need to import math module and then we need to call this function using math static object.

Syntax

Following is the syntax for the Python math.hypot() method −

math.hypot(x, y)

Parameters

  • x, y − These must be numeric values.

Return Value

This method returns Euclidean norm, sqrt(x*x + y*y).

Example

The following example shows the usage of the Python math.hypot() method. Here, we are creating two number objects containing positive float values. These objects are passed as arguments to this method and the Euclidean norm is calculated.

import math # This will import the math module

# Create two numeric objects
x = 12.98
y = 44.06

# Calculate the euclidean norm
hyp = math.hypot(x, y)

# Display the length of the euclidean norm
print("The length of Euclidean norm is:", hyp)

When we run above program, it produces following result −

The length of Euclidean norm is: 45.93216737755797

Example

Even if we pass negative coordinates x and y as arguments to this method, the Euclidean norm is obtained as a positive value (since its the distance).

In the following example, we are creating two number objects containing negative values. These objects are passed as arguments to the method and the length of Euclidean norm is computed.

import math # This will import the math module

# Create two numeric objects with negative values
x = -10.66
y = -15.12

# Calculate the euclidean norm
hyp = math.hypot(x, y)

# Display the length of the euclidean norm
print("The length of Euclidean norm is:", hyp)

Let us compile and run the program given, the output is produced as follows −

The length of Euclidean norm is: 18.5

Example

Suppose the x and y coordinates passed as positive infinity and negative infinity values respectively, the length of the Euclidean norm will be a positive infinity.

import math # This will import the math module

# Create two numeric objects with infinity
x = float("inf")
y = float("-inf")

# Calculate the Euclidean norm
hyp = math.hypot(x, y)

# Display the length of the Euclidean norm
print("The length of Euclidean norm is:", hyp)

On executing the program above, the output is given as follows −

The length of Euclidean norm is: inf

Example

When we pass the arguments to this method as NaN values, the return value will also be a NaN value.

import math # This will import the math module

# Create two numeric objects with NaN values
x = float("nan")
y = float("nan")

# Calculate the euclidean norm
hyp = math.hypot(x, y)

# Display the length of the euclidean norm
print("The length of Euclidean norm is:", hyp)

Compile and run the program to produce the result as follows −

The length of Euclidean norm is: nan
python_maths.htm
Advertisements