Java Math hypot()

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The hypot() method of java.lang.Math class returns the square root of x2+y2x^2 + y^2. The method does not produce an intermediate overflow or underflow.

  • Infinity is the outcome if either of the arguments are infinite.
  • The outcome is NaN if any of the arguments are NaN and neither argument is infinite.

Syntax of Math hypot() in Java

The syntax of hypot() method is

Parameters of Math hypot() in Java

Math.hypot() takes two arguments:

  • x, y - double type arguments used to calculate the output as Math.sqrt($x^2 + y^2$).

Return Value of Math hypot() in Java

Math.hypot() returns Math.sqrt(x2x^{2}+y2y^{2}) of double type without intermediate overflow or underflow.

Exceptions of Math hypot() in Java

Math.hypot() does NOT throw any exception.

Example

In this example, we are going to see how to use hypot() method in java.

Output:

Explanation:

  • In the above example, we have initialized x as 3 and y as 4.
  • So as we discussed hypot() method computes square root of x2 + y2 which is equal to 25. Hence, square root of 25 is printed.

What is Math hypot() in Java

The Java Math.hypot() method calculates the hypotenuse, or square root of x2+y2x^{2} + y^{2}, where x and y are its arguments.

Special cases:

  • The outcome is Infinity if either argument is infinite.
  • The outcome is NaN if any of the arguments is NaN and neither argument is infinite.

More Examples

Example 1: Pythagoras Theorem Using Math.hypot()

In this example, we are going to find the hypotenuse of the right-angle triangle using Pythagoras' Theorem.

Output:

Explanation:

In the above code, we have used Math.hypot() method to find the hypotenuse of a triangle having height 3 and base 4.

Example 2: Using hypot() with Infinity

In this example we are going to use Math.hypot() with infinity

Output:

Explanation:

As we discussed above that if one of the arguments is infinite, the hypot() method will return Infinity.

Example 3: Using hypot() with One Argument Math.sqrt(-4)

In this Example we are going to use Math.hypot() with one argument as sqrt(-4).

Output:

Explanation:

  • First Math.sqrt(-4) returns NaN as the square root of a negative number.
  • Now arguments passed in hypot() are 3 and NaN, and as discussed this will return NaN.

Example 4: Using hypot() with NaN and Infinity

In this example we are going to use Hypot with NaN and Infinity.

Output:

Explanation:

If any of the arguments is Infinite, the return value is Infinity.

Conclusion

  • hypot() method of Math class return the Euclidean norm, sqrt(xx+yy)sqrt(x * x + y * y).
  • The function does not produce an intermediate overflow or underflow and returns sqrt(x^2 + y^2).
  • Positive Infinity is the outcome if either argument is infinite.
  • The outcome is NaN if any of the arguments is NaN and neither argument is infinite.