std:: hypot, std:: hypotf, std:: hypotl
|
定义于头文件
<cmath>
|
||
| (1) | ||
|
float
hypot
(
float
x,
float
y
)
;
double
hypot
(
double
x,
double
y
)
;
|
(C++11 起)
(C++23 前) |
|
|
/*浮点类型*/
hypot
(
/*浮点类型*/
x,
|
(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
)
;
|
(C++17 起)
(C++23 前) |
|
|
/*浮点类型*/
hypot
(
/*浮点类型*/
x,
|
(C++23 起)
(C++26 起为 constexpr) |
|
|
定义于头文件
<cmath>
|
||
|
template
<
class
Arithmetic1, Arithmetic2
>
/*公共浮点类型*/
|
(A) |
(C++11 起)
(C++26 起为 constexpr) |
|
template
<
class
Arithmetic1, Arithmetic2, Arithmetic3
>
/*公共浮点类型*/
|
(B) | (C++17 起) |
std::hypot
的重载,作为参数
x
和
y
的类型。
(C++23 起)
std::hypot
的重载版本,作为参数
x
、
y
和
z
的类型。
(C++23 起)
此函数的双参数版本计算的是直角边长度为
x
和
y
的直角三角形的斜边长度,或点
(x,y)
到原点
(0,0)
的距离,或复数
x+
i
y
的模。
此函数的三参数版本计算的是点
(x,y,z)
到原点
(0,0,0)
的距离。
目录 |
参数
| x, y, z | - | 浮点数或整数值 |
返回值
+y 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 规范 规定,仅当两个参数均为次正规数且正确结果也是次正规数时才可能发生下溢(这排除了简单的实现方式)。
|
三维空间中两点
|
(since C++17) |
额外的重载并不需要严格按照 (A,B) 的形式提供。它们只需确保对于第一个参数 num1 、第二个参数 num2 以及可选的第三个参数 num3 能够满足以下条件:
|
(C++23 前) |
|
如果 num1 、 num2 和 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
|
|