Java hypot Function

The Java hypot Function is one of the Math functions which returns the square root of the sum of squares of the specified arguments.

Java hypot Function syntax

The basic syntax of the Math.hypot Function in Java Programming language is as shown below.

static double hypot(double x, double y); //Return Type is Double

// In order to use in program: 
Math.hypot(double x, double y);
  • X: It can be a double value or a valid numerical expression representing Cartesian X – Coordinate.
  • Y: It can be a double value or a valid numerical expression representing Cartesian Y – Coordinate.

Return Value: The Java hypot function will return sqrt(x² + y²) as an output, but there are some special cases where the output will change:

  • The function will return the Output if the number argument is a positive and negative integer.
  • If either of the arguments is infinite, then the result of this function will be positive infinite.
  • When the arguments are not a number or infinite, they will return NaN.

Java hypot Function Example

The Math.hypot function finds the square root of the sum of squares of a given number. In this Java program, we will find the same with both positive and negative values and display the output.

package TrigonometricFunctions;

public class HypotMethod {
	public static void main(String[] args) {
		
		System.out.println("\nHypot result of Zero Value = " + Math.hypot(0, 10));
		System.out.println("Hypot result of Zero Value =  " + Math.hypot(10, 0));	
		System.out.println("Hypot result of Zero Value = " + Math.hypot(0, -5));
		System.out.println("Hypot result of Zero Value =  " + Math.hypot(-10, 0));
		
		System.out.println("\nHypot result of Positive Value =  " + Math.hypot(4, 5));
		System.out.println("Hypot result of Positive Value =  " + Math.hypot(6, 10));

		System.out.println("\nHypot result of Negative Value =  " + Math.hypot(-2.50, -5.50));
		System.out.println("Hypot result of Negative Value =  " + Math.hypot(-6.25, -12.75));
		
		System.out.println("\nHypot result of Both Postive & Negative Value =  " + Math.hypot(3.50, -9.50));
		System.out.println("Hypot result of Both Postive & Negative Value =  " + Math.hypot(-4.25, 10.75));
		
	}
}
Java hypot Function 1

First, We used the Java Math.hypot Function with zero as one argument and Positive or negative double values as the second argument. The following Java statements will find the sqrt(x² + y²).

System.out.println("\nHypot result of Zero Value = " + Math.hypot(0, 10));
System.out.println("Hypot result of Zero Value =  " + Math.hypot(10, 0));	
System.out.println("Hypot result of Zero Value = " + Math.hypot(0, -5));
System.out.println("Hypot result of Zero Value =  " + Math.hypot(-10, 0));

Next, we used the Math function directly on Positive double values. For example, Math.hypot(4, 5) means sqrt(4² + 5²) ==> √41 = 6.40

System.out.println("\nHypot result of Positive Value =  " + Math.hypot(4, 5));
System.out.println("Hypot result of Positive Value =  " + Math.hypot(6, 10));

Here, we used the Function directly on Negative double values.

System.out.println("\nHypot result of Negative Value =  " + Math.hypot(-2.50, -5.50));
System.out.println("Hypot result of Negative Value =  " + Math.hypot(-6.25, -12.75));

We used it directly in the last two statements on both Positive & Negative double values.