名稱空間
變體
操作

std::hypot, std::hypotf, std::hypotl

來自 cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
常用數學函式
函式
基本操作
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指數函式
(C++11)
(C++11)

(C++11)
(C++11)
冪函式
(C++11)
hypot
(C++11)
三角
雙曲函式
(C++11)
(C++11)
(C++11)

誤差函式和伽馬函式
(C++11)
(C++11)
(C++11)
(C++11)
取整浮點運算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮點操縱函式
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分類和比較
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
型別
(C++11)
(C++11)
(C++11)
宏常量
分類
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定義於標頭檔案 <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 起)
1-3) 計算 x 和 y 平方和的平方根,在計算的中間階段沒有不適當的溢位或下溢。庫為所有 cv 非限定浮點型別作為引數 x 和 y 的型別提供了 std::hypot 的過載。(C++23 起)
4) 計算 x、y 和 z 平方和的平方根,在計算的中間階段沒有不適當的溢位或下溢。庫為所有 cv 非限定浮點型別作為引數 x、y 和 z 的型別提供了 std::hypot 的過載。(C++23 起)
A,B) 為所有其他算術型別組合提供了額外的過載。

此函式的兩個引數版本計算的值是直角三角形斜邊的長度,其兩邊長度分別為 xy,或是點 (x,y) 到原點 (0,0) 的距離,或是複數 x+iy 的模。

此函式的三引數版本計算的值是點 (x,y,z) 到原點 (0,0,0) 的距離。

目錄

[編輯] 引數

x, y, z - 浮點數或整數值

[編輯] 返回值

1-3,A) 如果沒有發生錯誤,則返回直角三角形的斜邊,x2
+y2
4,B) 如果沒有發生錯誤,則返回 3D 空間中到原點的距離,x2
+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(最後一位單位——最小精度單位):GNUBSD

std::hypot(x, y) 等價於 std::abs(std::complex<double>(x, y))

POSIX 指定,只有當兩個引數都是非正規數且正確結果也是非正規數時,才會發生下溢(這禁止了簡單的實現)。

三維空間中兩點 (x1, y1, z1)(x2, y2, z2) 之間的距離可以使用 std::hypot 的三引數過載計算為 std::hypot(x2 - x1, y2 - y1, z2 - z1)

(C++17 起)

不需要完全按照 (A,B) 提供額外的過載。它們只需要足以確保其第一個引數 num1、第二個引數 num2 和可選的第三個引數 num3 的型別得到處理。

  • 如果 num1num2num3 的型別是 long double,則
  • std::hypot(num1, num2) 的效果與 std::hypot(static_cast<long double>(num1),
               static_cast<long double>(num2))
    相同,並且
  • std::hypot(num1, num2, num3) 的效果與 std::hypot(static_cast<long double>(num1),
               static_cast<long double>(num2),
               static_cast<long double>(num3))
    相同。
  • 否則,如果 num1num2 和/或 num3 的型別是 double 或整數型別,則
  • std::hypot(num1, num2) 的效果與 std::hypot(static_cast<double>(num1),
               static_cast<double>(num2))
    相同,並且
  • std::hypot(num1, num2, num3) 的效果與 std::hypot(static_cast<double>(num1),
               static_cast<double>(num2),
               static_cast<double>(num3))
    相同。
  • 否則,如果 num1num2num3 的型別是 float,則
  • std::hypot(num1, num2) 的效果與 std::hypot(static_cast<float>(num1),
               static_cast<float>(num2))
    相同,並且
  • std::hypot(num1, num2, num3) 的效果與 std::hypot(static_cast<float>(num1),
               static_cast<float>(num2),
               static_cast<float>(num3))
    相同。
(直至 C++23)

如果 num1num2num3 具有算術型別,則

  • std::hypot(num1, num2) 的效果與 std::hypot(static_cast</*common-floating-point-type*/>(num1),
               static_cast</*common-floating-point-type*/>(num2))
    相同,並且
  • std::hypot(num1, num2, num3) 的效果與 std::hypot(static_cast</*common-floating-point-type*/>(num1),
               static_cast</*common-floating-point-type*/>(num2),
               static_cast</*common-floating-point-type*/>(num3))
    相同,

其中 /*common-floating-point-type*/ 是在 num1num2num3 的型別中,具有最高浮點轉換等級和最高浮點轉換次等級的浮點型別,整數型別的引數被認為與 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)
計算立方根(3x
(函式) [編輯]
返回複數的模
(函式模板) [編輯]
C 文件 用於 hypot