hypot() function - Python
Last Updated :
19 Feb, 2025
math.hypot() function in Python is used to compute the Euclidean distance from the origin (0, 0) to a point (x, y) in a 2D plane. It calculates the hypotenuse of a right triangle, given the lengths of the two other sides.
Example
Python
import math
x = 3
y = 4
res = math.hypot(x, y)
print(res)
Explanation:
In this example, the two sides of the right triangle are 3 and 4. The hypot() function calculates the hypotenuse as:
\sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt{25} = 5.0
Thus, the output is 5.0, which is the length of the hypotenuse.
Syntax of hypot() method
math.hypot(x, y)
Parameters
- x and y are numerical values
Return Type
- Returns a float value having Euclidean norm, \sqrt{(x^2 + y^2)}.
Error
- When more than two arguments are passed, it returns a TypeError.
Note : One has to import math module before using hypot() function.
Examples of hypot() method
1. Basic Hypotenuse Calculation
This example demonstrates how the hypot() function works with positive and negative values to compute the hypotenuse.
Python
import math
# Use of hypot function
print("hypot(3, 4) : ", math.hypot(3, 4))
# Neglects the negative sign
print("hypot(-3, 4) : ", math.hypot(-3, 4))
print("hypot(6, 6) : ", math.hypot(6, 6))
Outputhypot(3, 4) : 5.0
hypot(-3, 4) : 5.0
hypot(6, 6) : 8.48528137423857
Explanation
- math.hypot(3, 4) computes \sqrt{(3^2 + 4^2)} = 5.0.
- math.hypot(-3, 4) also returns 5.0 since the negative sign is ignored.
- math.hypot(6, 6) computes \sqrt{(6^2 + 6^2)} ≈ 8.485.
2. Error Handling in hypot()
This example shows an error when an incorrect number of arguments is passed to the hypot() function (more than two arguments).
Python
import math
# Use of hypot() function with incorrect number of arguments
try:
print("hypot(3, 4, 6) : ", math.hypot(3, 4, 6))
except TypeError as e:
print("Error:", e)
Outputhypot(3, 4, 6) : 7.810249675906654
Explanation
- math.hypot() function takes only two arguments.
- Passing three arguments (3, 4, 6) will raise a TypeError because the function cannot handle more than two inputs.
- The try-except block catches the error and prints a message explaining the issue.
3. Hypotenuse Calculation Using Perpendicular and Base
Practical Application : Given perpendicular and base of a right angle triangle find the hypotenuse. Using Pythagorean theorem which states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides.
This example demonstrates how to use the hypot() function to calculate the hypotenuse of a right triangle given the perpendicular and base.
Python
from math import hypot
# Perpendicular and base
p = 3
b = 4
# Calculates the hypotenuse
print(hypot(p, b))
Explanation
- from math import hypot statement imports the hypot() function from the math module.
- The values of p (perpendicular) and b (base) are set to 3 and 4, respectively.
- hypot(p, b) function computes the hypotenuse using the Pythagorean theorem: \sqrt{(p^2 + b^2)}.
Similar Reads
pow() Function - Python pow() function in Python is a built-in tool that calculates one number raised to the power of another. It also has an optional third part that gives the remainder when dividing the result. Example:Pythonprint(pow(3,2))Output9 Explanation: pow(3, 2) calculates 32 = 9, where the base is positive and t
2 min read
Python oct() Function Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal
2 min read
Python oct() Function Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal
2 min read
PHP hypot() function The hypot() function is a built-in mathematical function in PHP and returns the square root of sum of square of arguments passed. It finds the hypotenuse, hypotenuse is the longest side of a right angled triangle. It is calculated by the formula : h = \sqrt{x^2+y^2} where x and y are the other two s
1 min read
Python int() Function The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.Example:In this example, we passed a string as an argument to the int() function and printed it.Pythonage = "21" print("age =", int(age)
3 min read
modf() function - Python modf() function is an inbuilt function in Python that returns the fractional and integer parts of the number in a two-item tuple. Both parts have the same sign as the number. The integer part is returned as a float. Example:Pythonimport math # modf() function used with a positive number print("math.
3 min read