hypot, hypotf, hypotl
来自cppreference.com
| 定义于头文件 <math.h>
|
||
| float hypotf( float x, float y ); |
(1) | (C99 起) |
| double hypot( double x, double y ); |
(2) | (C99 起) |
| long double hypotl( long double x, long double y ); |
(3) | (C99 起) |
| 定义于头文件 <tgmath.h>
|
||
| #define hypot( x, y ) |
(4) | (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 - 若参数之一为 ±∞ ,则
hypot返回 +∞ ,即使另一参数为 NaN - 否则,若任何参数为 NaN ,则返回 NaN
注意
实现通常保证小于 1 ulp (最后位置单位)的精度: GNU 、 BSD 、 Open64 。
hypot(x, y) 等价于 cabs(x + I*y) 。
POSIX 指定仅若二个参数均为非正规且正确结果亦为非正规才可以出现下溢(这禁止朴素实现)。
hypot(INFINITY, NAN) 返回 +∞ ,但 sqrt(INFINITY*INFINITY+NAN*NAN) 返回 NaN 。
示例
运行此代码
#include <stdio.h> #include <math.h> #include <errno.h> #include <fenv.h> #include <float.h> #pragma STDC FENV_ACCESS ON int main(void) { // 典型用法 printf("(1,1) cartesian is (%f,%f) polar\n", hypot(1,1), atan2(1,1)); // 特殊值 printf("hypot(NAN,INFINITY) = %f\n", hypot(NAN,INFINITY)); // 错误处理 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引用
- C11 standard (ISO/IEC 9899:2011):
- 7.12.7.3 The hypot functions (p: 248)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.4.3 The hypot functions (p: 524)
- C99 standard (ISO/IEC 9899:1999):
- 7.12.7.3 The hypot functions (p: 229)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.4.3 The hypot functions (p: 461)
参阅
| (C99)(C99) |
计算一个数的给定次幂( xy ) (函数) |
| (C99)(C99) |
计算平方根( √x ) (函数) |
| (C99)(C99)(C99) |
计算立方根( 3√x ) (函数) |
| (C99)(C99)(C99) |
计算复数的模(绝对值) (函数) |