std::hypot, std::hypotf, std::hypotl
定义于头文件 <cmath> |
||
(1) | ||
float hypot ( float x, float y ); double hypot ( double x, double y ); |
(C++11 起) (直至 C++23) |
|
/*floating-point-type*/ hypot ( /*floating-point-type*/ 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) |
|
/*floating-point-type*/ hypot ( /*floating-point-type*/ x, |
(C++23 起) (C++26 起为 constexpr) |
|
定义于头文件 <cmath> |
||
template< class Arithmetic1, Arithmetic2 > /*common-floating-point-type*/ |
(A) | (C++11 起) (C++26 起为 constexpr) |
template< class Arithmetic1, Arithmetic2, Arithmetic3 > /*common-floating-point-type*/ |
(B) | (C++17 起) |
std::hypot
的重载。(C++23 起)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(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 指定,只有当两个参数都是非正规数且正确结果也是非正规数时,才会发生下溢(这禁止了简单的实现)。
三维空间中两点 |
(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() { // 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) |
计算立方根(3√x) (函数) |
返回复数的模 (函数模板) | |
C 文档 用于 hypot
|