×

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

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Trimorphic numbers in Java

Wondered what the Trimorphic number is!! In this article, you can learn about what a Trimorphic number is referred to as and how to find whether a number is trimorphic...

3 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Bottom view of a binary tree in Java

The lowest nodes in their horizontal distance are present and referred to as the bottom view of a binary tree. The horizontal distance between the nodes of a binary tree...

4 minutes read.

Java Lambda foreach

Before understanding lamda foreach, one must know about foreach loop in Java. foreach loop in Java Since J2SE 5.0, there has been a for-each loop in Java, also known as an extended...

4 minutes read.

Java Future Example

Future is an interface in the Java language that is a part of  java.util.concurrent package. It serves as a symbol for the output of an asynchronous computation. The interface offers ways to determine whether a computation has finished,  wait for it to finish, and receive its result. Once the task or calculation is finished, it cannot be undone. A Future interface offers ways to determine whether the computation is finished, to wait for it to finish, and to receive the computation's results. When the computation...

3 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 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.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.

Check whether a Number is a Power of 4 or Not in Java

There are many ways to figure out if an integer is a power of 4. This section will go over a variety of techniques for figuring out whether or not...

11 minutes read.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.