Home » Java programming language

Java Math Class static double hypot(double d1, double d2) with example

Java Math Class static double hypot(double d1, double d2) method: Here, we are going to learn about the static double hypot(double d1, double d2) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on September 04, 2019

Math Class static double hypot(double d1, double d2)

  • This method is available in java.lang package.
  • This method is used to return the square root of (square(d1)+ square(d2)) without any intermediate operations or in other words it returns the sqrt(square(d1)+ square(d2)).
  • This is a static method so it is accessible with the class name too.
  • The return type of this method is double that means it returns the square root of the given argument.
  • In this method, we pass two parameters as arguments and both of the parameters are of double type.
  • This method does not throw any exception.

Syntax:

    public static double hypot(double d1 , double d2){
    }

Parameter(s): d1 and d2 – both are the double values which will be used to find the square root of (d1*d1 + d2*d2).

Return value:

The return type of this method is double, it returns the Euclidean norm i.e. square root of (d12+d22).

Note:

  • If we pass an infinity in any of the given arguments, it returns the infinity.
  • If we pass "NaN" in any of the given arguments, it returns the "NaN".

Java program to demonstrate example of hypot(double d1, double d2) method

// Java program to demonstrate the example of 
// hypot(double d1 , double d2) method of Math Class

public class HypotMethod {
    public static void main(String[] args) {
        // Here we are declaring few variables
        double d1 = 7.0 / 0.0;
        double d2 = 5.0;
        double d3 = 10.0;

        // displaying the values
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);

        // Here , we will get (NaN) because we are passing 
        // parameter whose value is (d2,d1)
        System.out.println("Math.hypot(d2,d1): " + Math.hypot(d2, d1));

        // Here , we will get (sqrt(sq(d2)+sq(d3))) because 
        // we are passing parameter whose value is (d2,d3)
        System.out.println("Math.hypot(d2,d3): " + Math.hypot(d2, d3));
    }
}

Output

E:\Programs>javac HypotMethod.java

E:\Programs>java HypotMethod
d1: Infinity
d2: 5.0
d3: 10.0
Math.hypot(d2,d1): Infinity
Math.hypot(d2,d3): 11.180339887498949


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.