Java Math hypot() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

Definition

In Java, Math.hypot(x, y) returns the length of the hypotenuse of a right triangle with sides x and y, which is sqrt(x * x + y * y). Math.hypot(3, 4) is 5.0.

It is mainly used for the distance between two points and the length of a 2D vector. Unlike the hand-written formula, it avoids a temporary overflow or underflow in the squares. It was added in Java 5 (JDK 1.5).

A static method of the java.lang.Math class, added in Java 5 (JDK 1.5). Call it on the class name, as in Math.hypot(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass the two side lengths; the signs do not matter. The result is a double.

Java
double h = Math.hypot(3, 4);                  // 5.0
double dist = Math.hypot(x2 - x1, y2 - y1);   // distance
double len3 = Math.hypot(Math.hypot(x, y), z);  // 3D length
CallWhat it does
Math.hypot(3, 4)Returns 5.0.
Math.hypot(-3, -4)Signs are ignored: returns 5.0.
Math.hypot(dx, dy)Distance for the differences in x and y.
Math.hypot(Math.hypot(x, y), z)Length of a 3D vector (two nested calls).

hypot takes exactly two arguments. For three or more dimensions, nest the calls. The result is within one unit in the last place of the true value, and it is a bit slower than a plain sqrt, so use Math.sqrt(x * x + y * y) in tight loops where overflow cannot happen.

Library declarations
Java
// java.lang.Math (Java 5+)
public static double hypot(double x, double y)

Parameters

ParameterDescription
xThe length of the first side.
yThe length of the second side.

Return Value

Return ValueDescription
doubleThe square root of x*x + y*y, computed so that the squares cannot overflow or underflow. If either argument is infinite, the result is Infinity, even when the other one is NaN; otherwise a NaN argument gives NaN.

Compare with Similar Methods

Distances, lengths, and angles:

MethodHow it differs
sqrt(x*x + y*y)Same result for normal values; overflows for huge ones.
atan2(y, x)The angle of the same right triangle, not its length.
pow(x, 2)Squares one value; hypot squares, adds, and roots in one safe step.
abs(a - b)Distance on a single line (one dimension).

Java Examples

Example 1 — Distance Between Two Points

Measure how far a delivery bike travels in a straight line on a map grid, and the length of a 3D vector.

Java
public class MathHypotDistance {
    public static void main(String[] args) {
        double x1 = 1;
        double y1 = 2;
        double x2 = 4;
        double y2 = 6;
        double dist = Math.hypot(x2 - x1, y2 - y1);
        System.out.println("Distance: " + dist);

        double shop = Math.hypot(2, 3);
        System.out.println("Home to shop: " + Math.round(shop * 100) / 100.0);

        double arrow = Math.hypot(Math.hypot(1, 2), 2);
        System.out.println("3D length (rounded): " + Math.round(arrow));
    }
}

How It Works

  1. Differences first. The two points are 3 apart in x and 4 apart in y, so the straight-line distance is the hypotenuse of a 3-4-5 triangle: exactly 5.0.
  2. Not a nice number. The sides 2 and 3 give the square root of 13, about 3.6056; the Math.round trick shows it as 3.61.
  3. Nesting for three sides. hypot(1, 2) is the square root of 5; combining it with 2 gives the square root of 9, which rounds to the whole number 3.

Example 2 — Where the Hand-Written Formula Fails

Use values near the limits of a double and compare sqrt(x*x + y*y) with hypot.

Java
public class MathHypotOverflow {
    public static void main(String[] args) {
        double big = 1e200;
        System.out.println("big * big = " + big * big);
        System.out.println("naive:  " + Math.sqrt(big * big + big * big));
        double safe = Math.hypot(big, big);
        System.out.println("hypot finite? " + !Double.isInfinite(safe));
        System.out.println("hypot / 1e200 = " + Math.round(safe / big * 10000) / 10000.0);

        double tiny = 1e-200;
        System.out.println("naive tiny:  " + Math.sqrt(tiny * tiny + tiny * tiny));
        System.out.println("hypot tiny > 0? " + (Math.hypot(tiny, tiny) > 0));
    }
}

How It Works

  1. The square is the culprit. The true value 1e400 is far above the largest double (about 1.8e308), so big * big becomes Infinity and drags the whole formula with it.
  2. Scaled inside. hypot rescales its arguments internally, so the answer is finite; dividing by 1e200 shows the factor 1.4142, the square root of 2.
  3. Underflow is the mirror image. 1e-200 squared is 1e-400, below the smallest positive double, so it becomes 0.0 and the naive length is 0.0, while hypot stays above zero.

Example 3 — Negative Sides, Zero, Infinity, and NaN

The unusual inputs and what hypot returns for them.

Java
public class MathHypotSpecial {
    public static void main(String[] args) {
        System.out.println("hypot(-3, -4) = " + Math.hypot(-3, -4));
        System.out.println("hypot(0, 0) = " + Math.hypot(0, 0));
        System.out.println("hypot(NaN, 1) = " + Math.hypot(Double.NaN, 1));
        System.out.println("hypot(Infinity, NaN) = " + Math.hypot(Double.POSITIVE_INFINITY, Double.NaN));
    }
}

How It Works

  1. Lengths are never negative. The sides are squared, so -3 and -4 behave like 3 and 4 and the result is the positive 5.0.
  2. Zero sides. A triangle with both sides 0 has hypotenuse 0.0, always a positive zero.
  3. An infinite side wins. If one side is already infinite, the length is infinite no matter what the other value is, so Infinity beats NaN; a lone NaN with a finite side stays NaN.

Frequently Asked Questions

It returns the square root of x squared plus y squared, which is the length of the hypotenuse of a right triangle. Math.hypot(3, 4) returns 5.0.
They agree for normal values. hypot avoids intermediate overflow and underflow, so it still works with very large or very small numbers, where the plain formula gives Infinity or 0.0.
Call Math.hypot(x2 - x1, y2 - y1). For 3D points, nest the calls: Math.hypot(Math.hypot(dx, dy), dz).
Yes. The signs do not matter because the values are squared: Math.hypot(-3, -4) is 5.0.
Java 5 (JDK 1.5). Older versions need Math.sqrt(x * x + y * y).

Next: IEEEremainder()

IEEE 754 remainder: x minus the nearest whole multiple of y.

IEEEremainder() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful