BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.hypot() Method

By Chaitanya Singh | Filed Under: java

Java Math.hypot(double x, double y) method returns the sqrt(x2 + y2), where x is the first argument and y is the second argument. This method doesn’t throw any exception, if there is an overflow or underflow. This means, if the result of this method is less than Double.MIN_VALUE or greater than Double.MAX_VALUE then it doesn’t throw exception.

public class JavaExample
{
  public static void main(String[] args)
  {
    double x = 3;
    double y = 4;
    //square root of 3*3 + 4*4 == sqrt of 25
    System.out.println(Math.hypot(x, y));
  }
}

Output:

5.0

Syntax of Math.hypot() method

public static double hypot(double x, double y)

hypot() Description

It returns the square root of sum of squares of its arguments without overflow or underflow. The return type of this method is double.

hypot() Parameters

  • x: First argument.
  • y: Second argument.

hypot() Return Value

  • Returns sqrt(x2 + y2) where x and y are the arguments passed to this method while calling it.
  • No exception is thrown, if the result overflow or underflow the double lower and upper limit.
  • If any argument is infinity, then it returns positive infinity.
  • If any argument is NaN (Not a number) and the other argument is not infinity, then it returns NaN.

Example 1

public class JavaExample
{
  public static void main(String[] args)
  {
    double x = 3;
    double y = -4;
    //square root of 3*3 + -4*-4 == sqrt of 25
    System.out.println(Math.hypot(x, y));
  }
}

Output:

Java Math.hypot() Example Output1

Example 2

public class JavaExample
{
  public static void main(String[] args)
  {
    double x = Double.MAX_VALUE;
    double y = 5;
    // No exception in case of an overflow
    //the returned value is equal to the MAX_VALUE
    System.out.println(Math.hypot(x, y));

    //to show you the MAX_VALUE printing it
    System.out.println(Double.MAX_VALUE);
  }
}

Output:

Java Math.hypot() Example Output2

Example 3

public class JavaExample
{
  public static void main(String[] args)
  {
    double x = Double.MIN_VALUE;
    double y = 0;
    // No exception in case of an underflow
    //the returned value is equal to the MIN_VALUE
    System.out.println(Math.hypot(x, y));
    System.out.println(Double.MIN_VALUE);
  }
}

Output:

Java Math.hypot() Example Output3

Recommended Posts

  • Java Math.rint()
  • Java Math.random()
  • Java Math.floorDiv()
  • Java Math.floor()
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap