命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | numeric‎ | math
 
 
 
常用数学函数
函数
基本运算
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指数函数
(C++11)
(C++11)

(C++11)
(C++11)
幂函数
(C++11)
hypot
(C++11)
三角函数
双曲函数
(C++11)
(C++11)
(C++11)

误差和伽玛函数
(C++11)
(C++11)
(C++11)
(C++11)
最接近整数的浮点运算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮点数操作函数
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分类和比较
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型
(C++11)
(C++11)
(C++11)
宏常量
分类
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定义于头文件 <cmath>
(1)
float       hypot ( float x, float y );

double      hypot ( double x, double y );

long double hypot ( long double x, long double y );
(since C++11)
(until C++23)
/*floating-point-type*/

            hypot ( /*floating-point-type*/ x,

                    /*floating-point-type*/ y );
(since C++23)
(constexpr since C++26)
float       hypotf( float x, float y );
(2) (since C++11)
(constexpr since C++26)
long double hypotl( long double x, long double y );
(3) (since C++11)
(constexpr since C++26)
(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 );
(since C++17)
(until C++23)
/*floating-point-type*/

            hypot ( /*floating-point-type*/ x,
                    /*floating-point-type*/ y,

                    /*floating-point-type*/ z );
(since C++23)
(constexpr since C++26)
定义于头文件 <cmath>
template< class Arithmetic1, Arithmetic2 >

/*common-floating-point-type*/

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

/*common-floating-point-type*/

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

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

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

目录

[编辑] 参数

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

[编辑] 返回值

1-3,A) 如果没有错误发生,则返回直角三角形的斜边,x2
+y2
4,B) 如果没有错误发生,则返回 3D 空间中到原点的距离,x2
+y2
+z2

如果由于溢出而发生范围错误,则返回 +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
  • 如果其中一个参数是 ±∞,则 std::hypot(x, y) 返回 +∞,即使另一个参数是 NaN。
  • 否则,如果任何参数是 NaN,则返回 NaN。

[编辑] 注解

实现通常保证精度小于 1 ulp(最后一位单位 - 最小精度单位):GNUBSD

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

POSIX 规定,只有当两个参数都是次正规数且正确结果也是次正规数时,才可能发生下溢(这禁止了幼稚的实现)。

可以使用 std::hypot 的三参数重载计算 3D 空间中两点 (x1, y1, z1)(x2, y2, z2) 之间的距离,如下所示:std::hypot(x2 - x1, y2 - y1, z2 - z1)

(since C++17)

不需要完全按照 (A,B) 提供额外的重载。它们只需要足以确保对于它们的第一个参数 num1、第二个参数 num2 和可选的第三个参数 num3

  • 如果 num1num2num3 的类型为 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))
    相同的效果。
  • 否则,如果 num1num2 和/或 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))
    相同的效果。
  • 否则,如果 num1num2num3 的类型为 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))
    相同的效果。
(until C++23)

如果 num1num2num3 具有算术类型,则

  • 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*/ 是浮点类型,在 浮点转换等级浮点转换子等级 中,是 num1num2num3 类型中最高的,整数类型被认为具有与 double 相同的浮点转换等级。

如果不存在具有最高等级和子等级的此类浮点类型,则 重载解析 不会导致从提供的重载中产生可用的候选项。

(since C++23)
特性测试 Std 特性
__cpp_lib_hypot 201603L (C++17) std::hypot 的 3 参数重载 (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()
{
    // typical usage
    std::cout << "(1,1) cartesian is (" << std::hypot(1, 1)
              << ',' << std::atan2(1,1) << ") polar\n";
 
    Point3D a{3.14, 2.71, 9.87}, b{1.14, 5.71, 3.87};
    // C++17 has 3-argument hypot overload:
    std::cout << "distance(a,b) = "
              << std::hypot(a.x - b.x, a.y - b.y, a.z - b.z) << '\n';
 
    // special values
    std::cout << "hypot(NAN,INFINITY) = " << std::hypot(NAN, INFINITY) << '\n';
 
    // error handling
    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 raised\n";
}

输出

(1,1) cartesian is (1.41421,0.785398) polar
distance(a,b) = 7
hypot(NAN,INFINITY) = inf
hypot(DBL_MAX,DBL_MAX) = inf
    errno = ERANGE Numerical result out of range
    FE_OVERFLOW raised

[编辑] 参见

(C++11)(C++11)
将数字提升到给定的幂次 (xy)
(函数) [编辑]
(C++11)(C++11)
计算平方根 (x)
(函数) [编辑]
(C++11)(C++11)(C++11)
计算立方根 (3x)
(函数) [编辑]
返回复数的模
(函数模板) [编辑]
C 文档 关于 hypot