hypot, hypotf, hypotl, _hypot, _hypotf, _hypotl

计算斜边。

语法

double hypot(
   double x,
   double y
);
float hypotf(
   float x,
   float y
);
long double hypotl(
   long double x,
   long double y
);
double _hypot(
   double x,
   double y
);
float _hypotf(
   float x,
   float y
);
long double _hypotl(
   long double x,
   long double y
);
#define hypotf(X, Y) // Requires C11 or higher

参数

xy
浮点值。

返回值

如果成功,则 hypot 返回斜边长度;如果溢出,则 hypot 返回 INF(无穷大),而 errno 变量会被设置为 ERANGE。 可使用 _matherr 来修改错误处理。

有关返回代码的详细信息,请参阅 errno_doserrno_sys_errlist_sys_nerr

备注

在给定 xy 这两条边的长度的情况下,hypot 函数会计算直角三角形的斜边长度(也就是 x2 + y2 的平方根)。

提供的带有前导下划线的函数版本便于与早期的标准兼容。 它们的行为与没有前导下划线的版本相同。 我们建议将不带前导下划线的版本用于新代码。

如果使用 <tgmath.h>hypot() 宏,则参数的类型将决定选择哪个版本的函数。 有关详细信息,请参阅泛型类型数学

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

要求

例程 必需的标头
hypot, hypotf, hypotl, _hypot, _hypotf, _hypotl <math.h>
hypot <tgmath.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_hypot.c
// This program prints the hypotenuse of a right triangle.

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

int main( void )
{
   double x = 3.0, y = 4.0;

   printf( "If a right triangle has sides %2.1f and %2.1f, "
           "its hypotenuse is %2.1f\n", x, y, _hypot( x, y ) );
}
If a right triangle has sides 3.0 and 4.0, its hypotenuse is 5.0

另请参阅

数学和浮点支持
_cabs
_matherr