C++ cmath hypot() function
Example
Get the distance of 2D points (x, y) from the origin (0, 0):
cout << hypot(3.0f, 4.0f);
cout << hypot(1.0, 1.0);
cout << hypot(1.0, 10.0);
Try it Yourself »
Definition and Usage
The hypot()
function returns the length of the hypotenuse of a right angle triangle with sides of length x and y, which is equivalent to the distance between a 2D point (x, y) and the origin (0, 0).
The hypot()
function returns a value equal to sqrt(x * x + y * y)
but it is optimized to prevent overflows and underflows caused during intermediate operations such as addition and multiplication.
The hypot()
function is defined in the <cmath>
header file.
Syntax
One of the following:
hypot(double x, double y);
hypot(float x, float y);
Parameter Values
Parameter | Description |
---|---|
x |
Required. The x coordinate of a point or the length of the adjacent side of a right angle triangle. If this is an integer type then it will be treated as a double .
|
y |
Required. The y coordinate of a point or the length of the opposite side of a right angle triangle. If this is an integer type then it will be treated as a double .
|
Technical Details
Returns: | A float value (if all the arguments are float) or double value (in any other case) representing the hypotenuse of a right angle triangle, or the distance of a point (x, y) from the origin (0, 0). |
---|