CKTIS / C
hypot — C23 mathematical function
`hypot(x, y)` from `<math.h>` returns the nonnegative `sqrt(x*x + y*y)`, but computes it without undue intermediate overflow or underflow. The `hypotf` and `hypotl` variants serve `float` and `long double`, respectively.
Version: C23
Syntax
double hypot(double x, double y);hypot(x, y) from <math.h> returns the nonnegative sqrt(x*x + y*y), but computes it without undue intermediate overflow or underflow. The hypotf and hypotl variants serve float and long double, respectively.
The example prints 5.0 for sides 3 and 4, then evaluates hypot(1e308, 1e308). The result remains finite and greater than 1e308, so 5.0 1 1 demonstrates the advantage over naive sqrt(x*x + y*y), whose squared terms would overflow.
A range error may occur if the final result is outside the representable range; inspect math_errhandling, errno, or floating-point exceptions as supported. Under Annex F semantics, an infinite argument produces +Infinity even when the other is NaN, while a NaN with no infinity propagates. Two zero arguments produce +0, without preserving a negative-zero sign.
Examples
#include <math.h>
#include <stdio.h>
int main(void){double ordinary=hypot(3.0,4.0),large=hypot(1e308,1e308);printf("%.1f %d %d\n",ordinary,isfinite(large)!=0,large>1e308);return 0;}
Result
5.0 1 1
The example prints `5.0` for sides 3 and 4, then evaluates `hypot(1e308, 1e308)`. The result remains finite and greater than `1e308`, so `5.0 1 1` demonstrates the advantage over naive `sqrt(x*x + y*y)`, whose squared terms would overflow.