Java Math hypot() Method
Example
Get the distance of 2D points (x, y) from the origin (0, 0):
System.out.println(Math.hypot(3, 4));
System.out.println(Math.hypot(1, 1));
System.out.println(Math.hypot(1, 10));
Definition and Usage
The hypot()
method returns the length of the hypotenuse of a right angle triangle, which is equivalent to the distance between a 2D point (x, y) and the origin (0, 0).
This method returns a value equal to Math.sqrt(x * x + y * y)
but it is optimized to prevent overflows and underflows caused during intermediate operations such as addition and multiplication.
Syntax
public static double hypot(double x, double y)
Parameter Values
Parameter | Description |
---|---|
x | Required. The x coordinate of a point. |
y | Required. The y coordinate of a point. |
Technical Details
Returns: | A double value representing the distance between a point (x, y) and the origin (0, 0). |
---|---|
Java version: | 1.5+ |
❮ Math Methods