Home » Java programming language

Java StrictMath hypot() method with example

StrictMath Class hypot() method: Here, we are going to learn about the hypot() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 26, 2019

StrictMath Class hypot() method

  • hypot() method is available in java.lang package.
  • hypot() method is used to return the square root of sqrt(sq(d1)+sq(d2)) without any intermediate operations or in other words it returns the sqrt(sq(d1)+sq(d2)).
  • hypot() method is a static method so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • hypot() method does not throw any exception.

Syntax:

    public static double hypot(double d1 , double d2);

Parameter(s):

  • double d1 , double d2 – represent the values which are being used to calculate hypot.

Return value:

The return type of this method is double – it returns the square root of the given argument.

Note:

  • If we pass infinity in any of the arguments, method returns the positive infinity.
  • If we pass NaN in any of the arguments, method returns NaN.

Example:

// Java program to demonstrate the example
// of hypot(double d1 , double d2) method of 
// StrictMath class.

public class Hypot {
    public static void main(String[] args) {
        // variable declarations
        double d1 = 7.0 / 0.0;
        double d2 = 5.0;
        double d3 = 10.0;

        // Display previous value of d1,d2 and d3  
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);

        // Here , we will get (Infinity) because we are 
        // passing parameter whose value is (d2,d1)
        System.out.println("StrictMath.hypot(d2,d1): " + StrictMath.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("StrictMath.hypot(d2,d3): " + StrictMath.hypot(d2, d3));
    }
}

Output

d1: Infinity
d2: 5.0
d3: 10.0
StrictMath.hypot(d2,d1): Infinity
StrictMath.hypot(d2,d3): 11.180339887498949


Comments and Discussions!

Load comments ↻





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