The hypot() function computes √(x² + y²)—the hypotenuse of a right triangle with legs x and y. It is the standard way to measure 2D distance in graphics, games, and geometry, with built-in protection against overflow.
01
√(x²+y²)
Pythagoras.
02
math.h
Link -lm.
03
3-4-5
Classic.
04
Distance
2D points.
05
Safe
No overflow.
06
vs sqrt
Prefer hypot.
Fundamentals
Definition and Usage
The Pythagorean theorem says that for a right triangle with legs x and y, the hypotenuse is √(x² + y²). In C, hypot(x, y) computes this directly.
Geometrically, hypot(x, y) is the straight-line distance from the origin (0, 0) to the point (x, y). For two points (x1, y1) and (x2, y2), use hypot(x2 - x1, y2 - y1).
💡
Beginner Tip
Writing sqrt(x*x + y*y) looks fine for small numbers like 3 and 4, but squaring huge coordinates can overflow. hypot scales the calculation internally—use it whenever distance matters in real code.
Foundation
📝 Syntax
Standard C declaration:
C
double hypot(double x, double y);
Related variants
C
float hypotf(float x, float y); /* <math.h> */
long double hypotl(long double x, long double y); /* <math.h> */
Parameters
x — first leg (horizontal offset or x-coordinate).
y — second leg (vertical offset or y-coordinate).
Return Value
√(x² + y²) as a non-negative double.
hypot(3, 4) returns 5.0.
hypot(0, y) returns |y|; hypot(x, 0) returns |x|.
Headers and linking
#include <math.h>
Compile: gcc hypot.c -std=c11 -o hypot -lm
Cheat Sheet
⚡ Quick Reference
Call
Meaning
Result
hypot(3, 4)
3-4-5 triangle
5.0
hypot(5, 12)
5-12-13 triangle
13.0
hypot(-3, 4)
Sign ignored
5.0
hypot(dx, dy)
Point distance
Euclidean
sqrt(x*x+y*y)
Manual (risky)
Same if safe
Hypotenuse
hypot(x, y)
2D length
Two points
hypot(x2-x1, y2-y1)
Distance
1D only
fabs(a - b)
Line dist
Manual
sqrt(x*x + y*y)
Avoid large x,y
Hands-On
Examples Gallery
Compile every example with gcc file.c -std=c11 -o out -lm. Distance calculations appear constantly in graphics and collision detection.
📚 Getting Started
Calculate the hypotenuse of a classic right triangle.
With a zero leg, the triangle collapses to a line—distance equals the absolute value of the other leg.
Example 5 — hypot() vs sqrt(x² + y²)
Both work for small values; hypot is safer at scale.
C
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 3.0;
double y = 4.0;
double manual = sqrt(x * x + y * y);
double library = hypot(x, y);
printf("sqrt(x*x + y*y) = %.6f\n", manual);
printf("hypot(x, y) = %.6f\n", library);
return 0;
}
📤 Output:
sqrt(x*x + y*y) = 5.000000
hypot(x, y) = 5.000000
How It Works
Results match for 3 and 4. For very large coordinates (e.g. map tiles in millions), x*x can overflow to infinity while hypot still returns a finite distance.
Applications
🚀 Common Use Cases
Graphics and games — distance between sprites, click points, or enemies.
Collision detection — circular hit radius checks.
GPS / mapping — approximate planar distance from coordinate deltas.
Physics — vector magnitude in 2D simulations.
Geometry homework — right-triangle side calculations.
🧠 How hypot() Works
1
Receive x and y
Leg lengths or coordinate differences (any sign).
Input
2
Scale safely
Avoid overflow when squaring large values.
Compute
3
Return √(x²+y²)
Non-negative distance as double.
Output
=
🔢
Hypotenuse / distance
Compare to a radius: hypot(dx,dy) < r.
Important
📝 Notes
Computes √(x² + y²), always non-negative.
Sign of x and y does not affect the result.
Designed to avoid overflow/underflow vs naive squaring.
For 1D distance on a line, fabs(a - b) is enough.
3D distance: some platforms offer non-standard helpers; otherwise combine two hypot calls carefully.
Link with -lm on GCC/Clang Unix-like builds.
Performance
⚡ Optimization
hypot() costs more than a few multiplications but buys numerical stability. In inner loops comparing squared distances, some games compare dx*dx + dy*dy < r*r to skip sqrt—but when you need the actual length, use hypot or sqrt of the summed squares only if values are known small.
Wrap Up
Conclusion
hypot() computes the Pythagorean length √(x² + y²) safely—ideal for triangle hypotenuses and 2D point distance. Prefer it over manual sqrt(x*x + y*y) when coordinates can be large.
Continue with ldexp() for scaling by powers of two, or review fabs() for 1D distance.
Compare squared distance in hot loops when only ordering matters
Include <math.h> and link -lm
❌ Don’t
Square huge coordinates without considering overflow
Manually abs() each leg before hypot (unnecessary)
Use hypot for 1D when fabs suffices
Forget hypot returns length, not squared length
Assume 3D hypot exists in standard C (it does not)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about hypot()
Hypotenuse and distance in C, explained simply.
5
Core concepts
🔢01
√(x²+y²)
Pythagoras.
Basics
📚02
3-4-5
hypot(3,4)=5.
Classic
📈03
Distance
2D points.
Pattern
📄04
Overflow safe
vs sqrt.
Why
🔄05
Sign free
Squares.
Rule
❓ Frequently Asked Questions
hypot(x, y) returns the square root of x*x + y*y — the length of the hypotenuse of a right triangle with legs x and y. For example, hypot(3, 4) is 5.0. It also gives the distance from (0, 0) to (x, y).
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double hypot(double x, double y).
Squaring very large or very small values can overflow or underflow before sqrt runs. hypot() scales internally to compute sqrt(x^2 + y^2) more safely. Prefer hypot for robust distance math.
No. hypot uses squares, so hypot(-3, 4) equals hypot(3, 4), both 5.0. Pass raw coordinate differences without worrying about sign.
hypotf(x, y) takes float arguments and returns float. hypotl(x, y) uses long double. Match the variant to your variable types.
Use hypot(x2 - x1, y2 - y1). For example, distance from (1, 2) to (4, 6) is hypot(3, 4) = 5.
Did you know?
The 3-4-5 triangle is the smallest integer-sided right triangle. Ancient builders used knotted ropes with 12 evenly spaced knots to lay out perfect right angles—the same math hypot(3, 4) encodes in one line of C.