C 标准库 - <math.h>

C 语言 hypot() 函数用于计算两个数的平方和的平方根,即 sqrt(x² + y²),常用于求斜边长度。

原型

double hypot(double x, double y);

它比直接写 sqrt(x*x + y*y) 更安全,因为能避免中间平方溢出。

参数

  • x - 第一个直角边;
  • y - 第二个直角边。

示例

#include <stdio.h>
#include <math.h>

int main() {
    printf("hypot(3, 4) = %.1f\n", hypot(3, 4));
    return 0;
}

输出:

hypot(3, 4) = 5.0

注意事项

使用 math.h 的函数时,多数编译器需要链接数学库(编译选项 -lm)。