Java .hypot()
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()isdouble. - If either the
xoryparameter is infinite, the result is positive infinity. - If neither argument is infinite, but one or both parameters are
NaN, the result isNaN.
Example
The following example demonstrates using .hypot() to find the hypotenuse of a right-angled triangle:
// Test.javapublic 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
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours