C++官网参考链接:https://cplusplus.com/reference/cmath/hypot/
函数
<cmath> <ctgmath>
cbrt
C99
double hypot(double x, double y);
float hypotf(float x, float y);
long double hypotl(long double x, long double y);
C++11
double hypot(double x, double y);
float hypot(float x, float y);
long double hypot(long double x, long double y);
double hypot (Type1 x, Type2 y); // additional overloads
计算斜边
返回边长为x和y的直角三角形的斜边。
函数返回x和y平方和的平方根(根据毕达哥拉斯定理),但不会引起中间值的过度上溢或下溢。
C99
头文件<<tgmath.h>提供了该函数的泛型类型宏版本。
C++11
这个头文件(<cmath>)为其他算术类型(arithmetic types)(Type1和Type2)的组合提供了额外的重载:这些重载在计算之前有效地将其实参转换为double类型,除非至少有一个实参是long double类型(在这种情况下,两个实参都被转换为long double类型)。
形参
x,y
与计算斜边的直角三角形的两边相对应的浮点值。
返回值
x^2+y^2之和的平方根。
如果结果的大小太大,无法用返回类型的值表示,函数可能返回带有适当符号的HUGE_VAL(或HUGE_VALF或HUGE_VALL)(在这种情况下,会发生上溢范围错误):
如果发生上溢范围错误:
—math_errhandling具有MATH_ERRNO集合:全局变量errno设置为ERANGE。
—math_errhandling具有MATH_ERREXCEPT集合:引发FE_OVERFLOW。
用例
/* hypot example */
#include <stdio.h> /* printf */
#include <math.h> /* hypot */
int main ()
{
double leg_x, leg_y, result;
leg_x = 3;
leg_y = 4;
result = hypot (leg_x, leg_y);
printf ("%f, %f and %f form a right-angled triangle.\n",leg_x,leg_y,result);
return 0;
}
输出: