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
|