HYPOT

Compute sqrt(x*x + y*y) without intermediate overflow or underflow.

Category: mathReturns: DOUBLEDialect: Standard

Syntax

HYPOT(x, y)

Description

## Overview Returns sqrt(x*x + y*y), the length of the hypotenuse of a right triangle with legs x and y. HYPOT is numerically safer than the hand-written SQRT(POW(x, 2) + POW(y, 2)): the intermediate square x*x can overflow to infinity or underflow to zero for inputs with very large or very small magnitudes, even when the final hypotenuse is well within the DOUBLE range. HYPOT is the standard primitive for 2D Euclidean distance, complex-number magnitude, and vector norm calculations. ## Behavior - Accepts any finite DOUBLE for both arguments. - Returns a non-negative DOUBLE. - Returns NULL if either argument is NULL. - HYPOT(x, 0) equals ABS(x); HYPOT(0, y) equals ABS(y). - Is symmetric: HYPOT(x, y) equals HYPOT(y, x) and HYPOT(-x, y) equals HYPOT(x, y). - Does not overflow or underflow for any finite input pair whose hypotenuse is itself finite. ## Numeric precision - Uses the IEEE 754 double-precision hypot routine, accurate to within one ULP. - Performs internal scaling to avoid intermediate overflow or underflow. - Agrees with SQRT(x*x + y*y) in magnitudes where both computations are stable. ## Compatibility - Conforms to the C99 and IEEE 754 hypot conventions. - Matches typical mathematical library implementations.

Parameters

NameTypeDescription
xSpecifies the length of one leg of the right triangle. Accepts any finite DOUBLE value.
ySpecifies the length of the other leg of the right triangle. Accepts any finite DOUBLE value.

Examples

-- Classic 3-4-5 triangle
SELECT HYPOT(3, 4);
-- Result: 5.0
-- 5-12-13 triangle
SELECT HYPOT(5, 12);
-- Result: 13.0
-- Unit diagonal
SELECT HYPOT(1, 1);
-- Result: 1.4142135623730951
-- One side is zero reduces to the absolute value of the other
SELECT HYPOT(7, 0);
-- Result: 7.0
-- Extreme inputs: SQRT(x*x + y*y) would overflow but HYPOT does not
SELECT HYPOT(1e200, 1e200);
-- Result: 1.4142135623730951E200
-- Column use: Euclidean distance between 2D points
SELECT segment_id, HYPOT(x2 - x1, y2 - y1) AS distance
FROM geometry.samples.segments;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →