命名空间
变体
操作

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 );
(C++11 起)
(直至 C++23)
/*floating-point-type*/

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

                    /*floating-point-type*/ 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)
/*floating-point-type*/

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

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

/*common-floating-point-type*/

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

/*common-floating-point-type*/

            hypot ( Arithmetic1 x, Arithmetic2 y, Arithmetic3 z );
(B) (C++17 起)
1-3) 计算 x 和 y 平方和的平方根,在计算的中间阶段没有不适当的溢出或下溢。库为所有 cv 非限定浮点类型作为参数 x 和 y 的类型提供了 std::hypot 的重载。(C++23 起)
4) 计算 x、y 和 z 平方和的平方根,在计算的中间阶段没有不适当的溢出或下溢。库为所有 cv 非限定浮点类型作为参数 x、y 和 z 的类型提供了 std::hypot 的重载。(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
  • 如果其中一个参数是 ±∞,即使另一个参数是 NaN,std::hypot(x, y) 也返回 +∞。
  • 否则,如果任何参数是 NaN,则返回 NaN。

[编辑] 注意

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

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)

(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))
    相同。
(直至 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 具有相同的浮点转换等级。

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

(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()
{
    // 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