Namespaces
Variants

hypot, hypotf, hypotl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
hypot
(C99)
(C23)
(C23)
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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 ,则调用该函数的长双精度版本。否则,若任意参数具有整数类型或类型 double ,则调用该函数的双精度版本。否则,调用该函数的 float 版本。

此函数计算的是直角边长为 x y 的直角三角形的斜边长度,或点 ( x, y ) 到原点 ( 0 , 0 ) 的距离,或复数 x+ i y 的模。

目录

参数

x - 浮点数值
y - 浮点数值

返回值

如果未发生错误,将返回直角三角形的斜边 x 2
+y 2

如果发生因上溢导致的范围错误,则返回 +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( 最小精度单位 ): 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)
{
    // 典型用法
    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

参考文献

  • 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)
计算一个数的指定次幂 ( x y )
(函数)
(C99) (C99)
计算平方根 ( x )
(函数)
(C99) (C99) (C99)
计算立方根 ( 3 x )
(函数)
(C99) (C99) (C99)
计算复数的模
(函数)
C++ 文档 for hypot