How to use Math.hypot() in Java?
The hypot() method is a static method of the java.lang.Math library. It takes two numbers and returns the square root of + without any intermediate underflow or overflow.
There are special cases regarding this function:
-
The result will be
positiveinfinityif any of the parameters are infinite. -
The result will be
NaNif any of the parameters are NaN and none of them are infinite.
Syntax
public static double hypot(double x, double y)
Parameters
This method will take two numbers of primitive data type double:
xcontains the first argument.ycontains the second argument.
Return value
This function will return the answer of √( + ) without any overflow or underflow of double type.
Example
This code illustrates how to use the hypot() method to compute sqrt ( + ).
- We initialized
num1= 32176.2andnum2= -298.77, then called the function to perform the calculations. - It returns
32177.5870.
import java.lang.*;public class EdPresso {public static void main(String[] args) {//initializng the variablesdouble num1 = 32176.2;double num2 = -298.77;//calling the hypot method//and printing the outputSystem.out.println("answer= " + Math.hypot(num1, num2));}}