Python math.hypot() Method

Python math.hypot()
method is used to calculate the Euclidean norm of any input coordinate values. The Euclidian norm is the distance between the origin and the specified coordinates.
This method uses the formula sqrt(x*x + y*y)
to calculate the answer.
Syntax
math.hypot(p, b)
Parameters
p |
A positive or negative number represents the perpendicular side of a right-angle triangle. |
b |
A positive or negative number represents the base side of a right-angle triangle. |
Returns
The math.hypot()
returns a floating point value representing the Euclidean norm, sqrt(x*x + y*y).
Example Codes
Let’s learn the use of the math.hypot()
method by going through different code examples below.
Use math.hypot()
With Two Input Values
Example Code:
import math
p = 6
b = 4
value = math.hypot(p, b)
print(
f"The hypotenuse of a right-angle triangle having the perpendicular {p} and base {b} is {value}."
)
p = -10
b = -2
value = math.hypot(p, b)
print(
f"The hypotenuse of a right-angle triangle having the perpendicular {p} and base {b} is {value}."
)
p = 0
b = 0
value = math.hypot(p, b)
print(
f"The hypotenuse of a right-angle triangle having the perpendicular {p} and base {b} is {value}."
)
p = math.inf
b = math.inf
value = math.hypot(p, b)
print(
f"The hypotenuse of a right-angle triangle having the perpendicular {p} and base {b} is {value}."
)
Output:
The hypotenuse of a right-angle triangle having the perpendicular 6 and base 4 is 7.211102550927979.
The hypotenuse of a right-angle triangle having the perpendicular -10 and base -2 is 10.19803902718557.
The hypotenuse of a right-angle triangle having the perpendicular 0 and base 0 is 0.0.
The hypotenuse of a right-angle triangle having the perpendicular inf and base inf is inf.
Note that the values entered in the function can be used to compute the hypotenuse of any triangle.
Use math.hypot()
With N-Dimensional Points
Example Code:
import math
print(math.hypot(6, 9, 12))
print(math.hypot(9, 2, 11, 13))
print(math.hypot(10, 0, 3, 16, 23))
Output:
16.15549442140351
19.364916731037084
29.899832775452108
Note that newer versions of Python support multiple arguments to be entered in the math.hypot()
.
We can enter N-dimensional points, and the length of the vector from the point of origin to specified coordinates is returned.
Use math.hypot()
With Formula math.sqrt((p*p)+(b*b))
Example Code:
import math
p = 6
b = 4
value = math.sqrt((p * p) + (b * b))
print(
f"The hypotenuse of a right-angle triangle having the perpendicular {p} and base {b} is {value}"
)
Output:
The hypotenuse of a right-angle triangle having the perpendicular 6 and base 4 is 7.211102550927978.
Note that the above-stated formula is the underlying working of the math.hypot()
method.
Find Time Complexity of the math.hypot()
Method
Example Code:
import math
import math
import time
p = 3
b = 8
startHypot = time.time()
hypot = math.hypot(p, b)
print(
"The hypotenuse of the right-angle triangle using the formula is "
+ str(hypot)
+ "."
)
print(
"The time taken to compute the value using the formula is "
+ str(time.time() - startHypot)
+ "."
)
start = time.time()
hypo = math.sqrt((p * p) + (b * b))
print(
"The hypotenuse of the right-angle triangle using the equation is "
+ str(hypo)
+ "."
)
print(
"The time taken to compute the value using the equation is "
+ str(time.time() - start)
+ "."
)
Output:
The hypotenuse of the right-angle triangle using the formula is 8.54400374531753.
The time taken to compute the value using the formula is 2.9325485229492188e-05.
The hypotenuse of the right-angle triangle using the equation is 8.54400374531753.
The time taken to compute the value using the equation is 5.4836273193359375e-06.
The above code uses the time()
function to determine the time complexities of the math.hypot()
function and its underlying equation.
It is proven that the equation function computes the value faster, but the results may differ on various platforms built.
Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.
LinkedIn