×

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

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic....

3 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

Java Pass-by-Reference

In Java, passing parameters can be done using one of two fundamental methods. Pass-by-value is used for the first and pass-by-reference is used for the second. One thing to keep...

2 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Java Enum

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond,...

3 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1)....

3 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.