×

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow .

Syntax:

public static double hypot(double x, double y)

Parameters:

The parameters ‘x’ and ‘y’ represent the values.

Return Value:

The hypot () method returns the square root for the expression x2 + y2.

Special cases of the hypot() method are as follows:

  • It returns positive infinity, if either argument is infinite.
  • It returns NaN , if either argument is NaN and neither argument is infinite.

Example 1:

public class JavaMathHypotExample1 {
    public static void main(String[] args) {
        double a = 4;
        double b = 3;
        //returns the square root for the expression x.x + y.y
        System.out.println("Hypotenuse : "+Math.hypot(a,b));
    }
}

Output:

Hypotenuse : 5.0

Example 2:

public class JavaMathHypotExample2 {
    public static void main(String[] args) {
        double a = 12;
        double b = 5;
        double c=Math.hypot(a, b);
        //returns the square root for the expression x.x + y.y
        System.out.println("Hypotenuse : " + c);
        double d=a*a+b*b;
        if(d==c*c){
            System.out.println("The triangle ia a right angled triangle. ");
        }
        else{
            System.out.println("The triangle is not a right angled triangle.");
        }
    }
}

Output:

Hypotenuse : 13.0
The triangle ia a right angled triangle.

Example 3:

public class JavaMathHypotExample3 {
    public static void main(String[] args) {
        double a = 4/0.0d;
        double b = 3;
        //returns positive infinity, if either argument is infinite
        System.out.println("Hypotenuse : "+Math.hypot(a,b));
    }
}

Output:

Hypotenuse : Infinity

Example 4:

public class JavaMathHypotExample4 {
    public static void main(String[] args) {
        double a = -4/0.0d;
        double b = 3;
        //returns positive infinity, if either argument is negative infinite
        System.out.println("Hypotenuse : "+Math.hypot(a,b));
    }
}

Output:

Hypotenuse : Infinity

Example 5:

public class JavaMathHypotExample5 {
    public static void main(String[] args) {
        double a = 0.0d/0.0d;
        double b = 3;
        //returns NaN, if either argument is NaN
        System.out.println("Hypotenuse : "+Math.hypot(a,b));
    }
}

Output:

Hypotenuse : NaN

Related Topics

Hierarchy of operators in Java

Operators are the most frequently used terminology in any area of programming, and it helps in various approaches to efficiently solve daily life problems by computer programming. The simple addition of...

4 minutes read.

Upcasting and Downcasting in Java

Type casting in Java is an important and very interesting topic to deal with. But here upcasting and downcasting is somewhat related to typecasting. In normal typecasting, we convert from...

6 minutes read.

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Thread Scheduler in java

Scheduling: it is defined as the execution of multiple threads on a single CPU in some order is called scheduling. Preemptive-priority scheduling: This algorithm schedules threads based on their priority relative to other...

11 minutes read.

Activity selection problem in Java

The activity selection problem is a multiple objective problem hat requires choosing non-conflicting tasks to complete within a specific amount of time from a list of tasks identified by a...

5 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Level order Traversal of a Binary Tree in Java

In Java, a level order traversal of a tree structure is also referred to as the breadth-first traversal of the binary tree. Regarding the subsequent binary tree: Level order traversal is as...

5 minutes read.

Java String equals() method

equals() method compares two string based on the their content. Syntax public boolean equals(Objects anObject) parameter anObject: Object to be compared with the current String. Returns It returns true when the current String is equivalent to...

1 minute read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.