.hypot()

YanisaHS's avatar
Published Oct 20, 2022
Contribute to Docs

The Math.hypot() method returns the hypotenuse of a right-angled triangle. The hypotenuse is the square root of x2 + y2, where x and y (of type double) are the sides that form the right angle. It does not return intermediate overflow or underflow, which means the .hypot() method will not fail due to overflow or underflow of a x2 or y2 value.

Syntax

Math.hypot(x, y)
  • The return type for .hypot() is double.
  • If either the x or y parameter is infinite, the result is positive infinity.
  • If neither argument is infinite, but one or both parameters are NaN, the result is NaN.

Example

The following example demonstrates using .hypot() to find the hypotenuse of a right-angled triangle:

// Test.java
public class Test {
public static void main(String args[]) {
double base_x = 8;
double height_y = 10;
System.out.println(Math.hypot(base_x, height_y));
}
}

This results in the following output:

12.806248474865697

All contributors

Contribute to Docs

Learn Java on Codecademy