std::hypot, std::hypotf, std::hypotl
定义于头文件 <cmath> |
||
(1) | ||
float hypot ( float x, float y ); double hypot ( double x, double y ); |
(since C++11) (until C++23) |
|
/*floating-point-type*/ hypot ( /*floating-point-type*/ x, |
(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 ); |
(since C++17) (until C++23) |
|
/*floating-point-type*/ hypot ( /*floating-point-type*/ x, |
(since C++23) (constexpr since C++26) |
|
定义于头文件 <cmath> |
||
template< class Arithmetic1, Arithmetic2 > /*common-floating-point-type*/ |
(A) | (since C++11) (constexpr since C++26) |
template< class Arithmetic1, Arithmetic2, Arithmetic3 > /*common-floating-point-type*/ |
(B) | (since C++17) |
std::hypot
的重载,作为参数 x 和 y 的类型。(自 C++23 起)std::hypot
的重载,作为参数 x、y 和 z 的类型。(自 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。
- 如果其中一个参数是 ±∞,则 std::hypot(x, y) 返回 +∞,即使另一个参数是 NaN。
- 否则,如果任何参数是 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
|
(until C++23) |
如果 num1、num2 和 num3 具有算术类型,则
其中 /*common-floating-point-type*/ 是浮点类型,在 浮点转换等级 和 浮点转换子等级 中,是 num1、num2 和 num3 类型中最高的,整数类型被认为具有与 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) |
计算立方根 (3√x) (函数) |
返回复数的模 (函数模板) | |
C 文档 关于 hypot
|