Java hypot Function

The Java hypot Function is one of the Java Math functions, which is to return 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 which represent Cartesian X – Coordinate.
  • Y: It can be a double value or a valid numerical expression which represent Cartesian Y – Coordinate.

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

  • If the number argument is a positive integer and Negative integer, the Math.hypot function will return the Output.
  • If either of the arguments is infinite, then the result of this Java Math.hypot function will be positive infinite.
  • When the arguments are not a number or infinite, the Math.hypot function 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 are going to find the same with both positive and negative values and display the output.

// Java Math.hypot Function

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 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 hypot 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 Java hypot 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));

Next, We used the Math.hypot Function directly on both Positive & Negative double values.

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));