GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java Math hypot() Method - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java Math hypot() Method

Last Updated : 7 May, 2025

The Math.hypot() function is an in-built Math function in Java. Before going deep dive to the topic, first understand the reason behind to use this function.

Suppose we have a right-angled triangle and we know the lengths of the two shorter sides i.e., x and y. To calculate the hypotenuse, we use sqrt(x*x + y*y) formula. But Java has this in-built function i.e., Math.hypot() to handle very large or small values easily.

This function returns the Euclidean distance of a right-angled triangle with the sides of length x and y. The method returns sqrt(x2 + y2) without intermediate overflow or underflow.

 \sqrt{(x * x + y * y)}
  • If the argument is infinite, then the result is positive infinity.
  • If the argument is NaN and neither argument is infinite, then the result is NaN.

Syntax of Math hypot() Method

public static double hypot(double x, double y)

  • Parameter: x and y are the lengths of the sides.
  • Returns: It returns the square root of (x*x + y*y) without intermediate overflow or underflow. 

Examples of Java Math hypot() Method

Example 1: In this example, we will see the working of java.lang.Math.hypot() method.

Java
// Java program to demonstrate working 
// of java.lang.Math.hypot() method 
public class Geeks {
    
    public static void main(String[] args) {
        
        double x = 3.0;
        double y = 4.0;
        System.out.println("hypot(3,4): " + Math.hypot(x, y));
    }
}

Output
hypot(3,4): 5.0

Explanation: The square root of (3*3 + 4*4) is 5.


Example 2: In this example, we will see how to handle NaN.

Java
// Java program to handle NaN
public class Geeks {
    
    public static void main(String[] args) {
        
        double nan = Double.NaN;
        double y   = 2.0;
        System.out.println("hypot(NaN,2): " + Math.hypot(nan, y));
    }
}

Output
hypot(NaN,2): NaN

Explanation: Here, one argument is NaN and one is no infinite value, so the result id NaN.


Example 3: In this example, we will see how to handle infinite values.

Java
// Java program to handle infinite values
public class Geeks {
    
    public static void main(String[] args) {
        
        double p = Double.POSITIVE_INFINITY;
        double n = Double.NEGATIVE_INFINITY;
        System.out.println("hypot(Inf,5): " + Math.hypot(p, 5));
        System.out.println("hypot(Inf, -Inf): " + Math.hypot(p, n));
    }
}

Output
hypot(Inf,5): Infinity
hypot(Inf, -Inf): Infinity

Explanation: Here, both the arguments are infinite, so it results hypotenuse.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.