`Math.hypot(x, y)` computes a right triangle’s hypotenuse while avoiding a failure mode in the direct Pythagorean expression `Math.sqrt(x * x + y * y)`: intermediate multiplication can underflow even when the final hypotenuse is representable. section 4.2.1
Very small sides
With `x = 3e-200` and `y = 4e-200`, squaring each value can underflow, making the direct square-root calculation return zero rather than the expected `5e-200`. Use `Math.hypot(x, y)` for this calculation instead. section 4.2.1
Use it when numerical range matters:
- Prefer `Math.hypot(x, y)` over manually squaring and adding tiny floating-point values. section 4.2.1
- Treat underflow as an edge case: it is uncommon in ordinary work, but it can silently turn a nonzero result into zero. section 4.2.1
double x = 3e-200; double y = 4e-200; double hypot = Math.sqrt(x * x + y * y);
Compare the direct Pythagorean calculation with `Math.hypot` when testing extremely small operands. section 4.2.1
