hypot, hypotf, hypotl
来自 cppreference.cn
定义于头文件 <math.h> |
||
float hypotf( float x, float y ); |
(1) | (since C99) |
double hypot( double x, double y ); |
(2) | (since C99) |
long double hypotl( long double x, long double y ); |
(3) | (since C99) |
定义于头文件 <tgmath.h> |
||
#define hypot( x, y ) |
(4) | (since C99) |
1-3) 计算 x 和 y 的平方和的平方根,在计算的中间阶段没有不适当的溢出或下溢。
4) 类型泛型宏:如果任何参数具有 long double 类型,则调用该函数的 long double 版本。否则,如果任何参数具有整数类型或 double 类型,则调用该函数的 double 版本。否则,调用 float 版本。
此函数计算的值是边长为 x 和 y 的直角三角形的斜边长度,或点 (x, y) 到原点 (0, 0) 的距离,或复数 x+iy
的模。
内容 |
[编辑] 参数
x | - | 浮点数值 |
y | - | 浮点数值 |
[编辑] 返回值
如果没有错误发生,则返回直角三角形的斜边,√x2
+y2
。
如果发生由于溢出引起的范围错误,则返回 +HUGE_VAL、+HUGE_VALF
或 +HUGE_VALL
。
如果发生由于下溢引起的范围错误,则返回正确的结果(在舍入后)。
[编辑] 错误处理
错误报告方式如 math_errhandling
中所指定。
如果实现支持 IEEE 浮点算术 (IEC 60559),
- hypot(x, y)、hypot(y, x) 和 hypot(x, -y) 是等价的
- 如果其中一个参数是 ±0,则
hypot
等价于对非零参数调用 fabs - 如果其中一个参数是 ±∞,则即使另一个参数是 NaN,
hypot
也返回 +∞ - 否则,如果任何参数是 NaN,则返回 NaN。
[编辑] 注解
实现通常保证精度小于 1 ulp (units in the last place):GNU, BSD。
hypot(x, y) 等价于 cabs(x + I*y)。
POSIX 规范指出,只有当两个参数都是次正规数且正确结果也是次正规数时,才可能发生下溢(这禁止了幼稚的实现)。
hypot(INFINITY, NAN) 返回 +∞,但 sqrt(INFINITY * INFINITY + NAN * NAN) 返回 NaN。
[编辑] 示例
运行此代码
#include <errno.h> #include <fenv.h> #include <float.h> #include <math.h> #include <stdio.h> // #pragma STDC FENV_ACCESS ON int main(void) { // typical usage printf("(1,1) cartesian is (%f,%f) polar\n", hypot(1,1), atan2(1, 1)); // special values printf("hypot(NAN,INFINITY) = %f\n", hypot(NAN, INFINITY)); // error handling errno = 0; feclearexcept(FE_ALL_EXCEPT); printf("hypot(DBL_MAX,DBL_MAX) = %f\n", hypot(DBL_MAX, DBL_MAX)); if (errno == ERANGE) perror(" errno == ERANGE"); if (fetestexcept(FE_OVERFLOW)) puts(" FE_OVERFLOW raised"); }
可能的输出
(1,1) cartesian is (1.414214,0.785398) polar hypot(NAN,INFINITY) = inf hypot(DBL_MAX,DBL_MAX) = inf errno == ERANGE: Numerical result out of range FE_OVERFLOW raised
[编辑] 参考文献
- C23 标准 (ISO/IEC 9899:2024)
- 7.12.7.3 hypot 函数 (p: TBD)
- 7.25 类型泛型数学 <tgmath.h> (p: TBD)
- F.10.4.3 hypot 函数 (p: TBD)
- C17 标准 (ISO/IEC 9899:2018)
- 7.12.7.3 hypot 函数 (p: 181)
- 7.25 类型泛型数学 <tgmath.h> (p: 272-273)
- F.10.4.3 hypot 函数 (p: 382)
- C11 标准 (ISO/IEC 9899:2011)
- 7.12.7.3 hypot 函数 (p: 248)
- 7.25 类型泛型数学 <tgmath.h> (p: 373-375)
- F.10.4.3 hypot 函数 (p: 524)
- C99 标准 (ISO/IEC 9899:1999)
- 7.12.7.3 hypot 函数 (p: 229)
- 7.22 类型泛型数学 <tgmath.h> (p: 335-337)
- F.9.4.3 hypot 函数 (p: 461)
[编辑] 参见
(C99)(C99) |
计算给定幂的数字 (xy) (函数) |
(C99)(C99) |
计算平方根 (√x) (函数) |
(C99)(C99)(C99) |
计算立方根 (3√x) (函数) |
(C99)(C99)(C99) |
计算复数的模 (函数) |
C++ 文档 关于 hypot
|