Namespaces
Variants

std:: hypot, std:: hypotf, std:: hypotl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float hypot ( float x, float y ) ;

double hypot ( double x, double y ) ;

long double hypot ( long double x, long double y ) ;
(C++11 起)
(C++23 前)
/*浮点类型*/

hypot ( /*浮点类型*/ x,

/*浮点类型*/ y ) ;
(C++23 起)
(C++26 起为 constexpr)
float hypotf ( float x, float y ) ;
(2) (C++11 起)
(C++26 起为 constexpr)
long double hypotl ( long double x, long double y ) ;
(3) (C++11 起)
(C++26 起为 constexpr)
(4)
float hypot ( float x, float y, float z ) ;

double hypot ( double x, double y, double z ) ;

long double hypot ( long double x, long double y, long double z ) ;
(C++17 起)
(C++23 前)
/*浮点类型*/

hypot ( /*浮点类型*/ x,
/*浮点类型*/ y,

/*浮点类型*/ z ) ;
(C++23 起)
(C++26 起为 constexpr)
定义于头文件 <cmath>
template < class Arithmetic1, Arithmetic2 >

/*公共浮点类型*/

hypot ( Arithmetic1 x, Arithmetic2 y ) ;
(A) (C++11 起)
(C++26 起为 constexpr)
template < class Arithmetic1, Arithmetic2, Arithmetic3 >

/*公共浮点类型*/

hypot ( Arithmetic1 x, Arithmetic2 y, Arithmetic3 z ) ;
(B) (C++17 起)
1-3) 计算 x y 的平方和的平方根,在计算的中间阶段不会出现不当的上溢或下溢。 标准库为所有 cv 未限定浮点类型提供了 std::hypot 的重载,作为参数 x y 的类型。 (C++23 起)
4) 计算 x y z 的平方和的平方根,在计算过程中不会出现不当的溢出或下溢。 标准库为所有无 cv 限定的浮点类型提供了 std::hypot 的重载版本,作为参数 x y z 的类型。 (C++23 起)
A,B) 为所有其他算术类型的组合提供了额外的重载。

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

此函数的三参数版本计算的是点 (x,y,z) 到原点 (0,0,0) 的距离。

目录

参数

x, y, z - 浮点数或整数值

返回值

1-3,A) 若未发生错误,则返回直角三角形的斜边值 x 2
+y 2
4,B) 若未发生错误,则返回三维空间中距原点的距离 x 2
+y 2
+z 2

如果发生因溢出导致的范围错误,将返回 +HUGE_VAL +HUGE_VALF +HUGE_VALL

如果发生因下溢导致的范围错误,将返回正确结果(经舍入后)。

错误处理

错误报告方式遵循 math_errhandling 中的规范。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • std :: hypot ( x, y ) std :: hypot ( y, x ) std :: hypot ( x, - y ) 是等价的。
  • 若任一参数为 ±0,则 std :: hypot ( x, y ) 等价于对非零参数调用 std::fabs
  • 若任一参数为 ±∞,则即使另一参数为 NaN, std :: hypot ( x, y ) 也返回 +∞。
  • 否则,若任一参数为 NaN,则返回 NaN。

注释

实现通常保证精度小于1 ulp (末位单位——最小精度单位): GNU BSD

std :: hypot ( x, y ) 等价于 std :: abs ( std:: complex < double > ( x, y ) )

POSIX 规范 规定,仅当两个参数均为次正规数且正确结果也是次正规数时才可能发生下溢(这排除了简单的实现方式)。

三维空间中两点 (x1, y1, z1) (x2, y2, z2) 间的距离可通过三参数重载版本的 std::hypot 计算,即 std :: hypot ( x2 - x1, y2 - y1, z2 - z1 )

(since C++17)

额外的重载并不需要严格按照 (A,B) 的形式提供。它们只需确保对于第一个参数 num1 、第二个参数 num2 以及可选的第三个参数 num3 能够满足以下条件:

  • 如果 num1 num2 num3 具有 long double 类型,则
  • std :: hypot ( num1, num2 ) 的效果等同于 std :: hypot ( static_cast < long double > ( num1 ) ,
    static_cast < long double > ( num2 ) )
    ,且
  • std :: hypot ( num1, num2, num3 ) 的效果等同于 std :: hypot ( static_cast < long double > ( num1 ) ,
    static_cast < long double > ( num2 ) ,
    static_cast < long double > ( num3 ) )
  • 否则,如果 num1 num2 和/或 num3 具有 double 类型或整数类型,则
  • std :: hypot ( num1, num2 ) 的效果等同于 std :: hypot ( static_cast < double > ( num1 ) ,
    static_cast < double > ( num2 ) )
    ,且
  • std :: hypot ( num1, num2, num3 ) 的效果等同于 std :: hypot ( static_cast < double > ( num1 ) ,
    static_cast < double > ( num2 ) ,
    static_cast < double > ( num3 ) )
  • 否则,如果 num1 num2 num3 具有 float 类型,则
  • std :: hypot ( num1, num2 ) 的效果等同于 std :: hypot ( static_cast < float > ( num1 ) ,
    static_cast < float > ( num2 ) )
    ,且
  • std :: hypot ( num1, num2, num3 ) 的效果等同于 std :: hypot ( static_cast < float > ( num1 ) ,
    static_cast < float > ( num2 ) ,
    static_cast < float > ( num3 ) )
(C++23 前)

如果 num1 num2 num3 具有算术类型,则

  • std :: hypot ( num1, num2 ) 的效果等同于 std :: hypot ( static_cast < /*common-floating-point-type*/ > ( num1 ) ,
    static_cast < /*common-floating-point-type*/ > ( num2 ) )
    ,且
  • std :: hypot ( num1, num2, num3 ) 的效果等同于 std :: hypot ( static_cast < /*common-floating-point-type*/ > ( num1 ) ,
    static_cast < /*common-floating-point-type*/ > ( num2 ) ,
    static_cast < /*common-floating-point-type*/ > ( num3 ) )

其中 /*common-floating-point-type*/ 是在 num1 num2 num3 的类型中具有最高 浮点转换等级 和最高 浮点转换子等级 的浮点类型,整数类型的参数被认为具有与 double 相同的浮点转换等级。

如果不存在具有最高等级和子等级的此类浮点类型,则 重载决议 不会从提供的重载中产生可用的候选函数。

(C++23 起)
功能测试 标准 功能
__cpp_lib_hypot 201603L (C++17) std::hypot 的三参数重载 (4,B)

示例

#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
struct Point3D { float x, y, z; };
int main()
{
    // 典型用法
    std::cout << "(1,1) 笛卡尔坐标转换为极坐标为 (" << std::hypot(1, 1)
              << ',' << std::atan2(1,1) << ")\n";
    Point3D a{3.14, 2.71, 9.87}, b{1.14, 5.71, 3.87};
    // C++17 具有三参数 hypot 重载:
    std::cout << "distance(a,b) = "
              << std::hypot(a.x - b.x, a.y - b.y, a.z - b.z) << '\n';
    // 特殊值
    std::cout << "hypot(NAN,INFINITY) = " << std::hypot(NAN, INFINITY) << '\n';
    // 错误处理
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "hypot(DBL_MAX,DBL_MAX) = " << std::hypot(DBL_MAX, DBL_MAX) << '\n';
    if (errno == ERANGE)
        std::cout << "    errno = ERANGE " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_OVERFLOW))
        std::cout << "    FE_OVERFLOW 已触发\n";
}

输出:

(1,1) 笛卡尔坐标转换为极坐标为 (1.41421,0.785398)
distance(a,b) = 7
hypot(NAN,INFINITY) = inf
hypot(DBL_MAX,DBL_MAX) = inf
    errno = ERANGE 数值结果超出范围
    FE_OVERFLOW 已触发

参见

(C++11) (C++11)
计算一个数的指定次幂 ( x y )
(函数)
(C++11) (C++11)
计算平方根 ( x )
(函数)
(C++11) (C++11) (C++11)
计算立方根 ( 3 x )
(函数)
返回复数的模
(函数模板)
C 文档 for hypot