std::hypot, std::hypotf, std::hypotl
| 在标头 <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 起) |
x 和 y 的平方和的平方根,而不会在计算的中间阶段有过度的上溢或下溢。标准库提供所有以无 cv 限定的浮点数类型作为形参 x 和 y 的类型的 std::hypot 重载。(C++23 起)x、y 和 z 的平方和的平方根,而不会在计算的中间阶段有过度的上溢或下溢。标准库提供所有以无 cv 限定的浮点数类型作为形参 x、y 和 z 的类型的 std::hypot 重载。(C++23 起)此函数的双参数版本所计算的是直角边长度分别是 x 和 y 的直角三角形的斜边长,或点 (x,y) 到原点 (0,0) 的距离,或复数 x+iy 的绝对值。
此函数的三参数版本所计算的值是点 (x,y,z) 到原点 (0,0,0) 的距离。
参数
| x, y, z | - | 浮点数或整数 |
返回值
+y2
。
+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等价于以非零实参调用 std::fabs - 如果实参之一是 ±∞,那么
std::hypot返回 +∞ ,即使另一实参是 NaN - 否则,如果任何实参是 NaN,那么返回 NaN
注解
实现通常保证小于 1 ulp(最后位置单位—最低精度单位)的精度:GNU、BSD。
std::hypot(x, y) 等价于 std::abs(std::complex<double>(x,y))。
POSIX 指定只有在两个实参都非正规且正确结果也非正规时才可以出现下溢(这导致不能以朴素方法实现)。
|
能以 |
(C++17 起) |
额外重载不需要以 (A,B) 的形式提供。它们只需要能够对它们的第一个实参 num1,第二个实参 num2 和可能有的第三个实参 num3 满足以下要求:
|
(C++23 前) |
|
如果
其中 如果不存在等级和子等级最高的浮点数类型,那么在重载决议时不会从提供的重载中产生可用的候选。 |
(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 (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 Numerical result out of range
发生 FE_OVERFLOW
参阅
(C++11)(C++11) |
求某数的给定次幂(xy) (函数) |
(C++11)(C++11) |
计算平方根(√x) (函数) |
(C++11)(C++11)(C++11) |
计算立方根(3√x) (函数) |
| 返回复数的模 (函数模板) | |
hypot 的 C 文档
| |