名稱空間
變體
操作

hypot, hypotf, hypotl

來自 cppreference.com
< c‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
(C99)
(C99)
(C99)
(C99)(C99)(C99)(C23)
最大值/最小值操作
(C99)
(C99)
指數函式
(C23)
(C99)
(C99)
(C23)
(C23)

(C99)
(C99)(C23)
(C23)
(C23)
冪函式
(C99)
(C23)
(C23)

hypot
(C99)
(C23)
(C23)
三角函式和雙曲函式
(C23)
(C23)
(C23)
(C23)
(C99)
(C99)
(C99)
最近整數浮點數
(C99)(C99)(C99)
(C99)

(C99)(C99)(C99)
(C23)(C23)(C23)(C23)
浮點數操作
(C99)(C99)
(C99)(C23)
(C99)
窄化操作
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
量子與量子指數
十進位制重新編碼函式
總序和載荷函式
分類
(C99)
(C99)
(C99)
(C23)
誤差函式和伽馬函式
(C99)
(C99)
(C99)
(C99)
型別
宏常量
特殊浮點值
(C99)(C23)
引數和返回值
錯誤處理
快速操作指示符
 
定義於標頭檔案 <math.h>
float       hypotf( float x, float y );
(1) (C99 起)
double      hypot( double x, double y );
(2) (C99 起)
long double hypotl( long double x, long double y );
(3) (C99 起)
定義於標頭檔案 <tgmath.h>
#define hypot( x, y )
(4) (C99 起)
1-3) 計算 x 和 y 的平方和的平方根,在計算的中間階段沒有不必要的溢位或下溢。
4) 泛型宏:如果任何引數具有型別 long double,則呼叫該函式的 long double 版本。否則,如果任何引數具有整數型別或型別 double,則呼叫該函式的 double 版本。否則,呼叫該函式的 float 版本。

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

目錄

[編輯] 引數

x - 浮點值
y - 浮點值

[編輯] 返回值

如果沒有錯誤發生,則返回直角三角形的斜邊,x2
+y2

如果因溢位而發生範圍錯誤,則返回 +HUGE_VAL+HUGE_VALF+HUGE_VALL

如果發生因下溢導致的範圍錯誤,返回正確的結果(舍入後)。

[編輯] 錯誤處理

錯誤按 math_errhandling 中指定的方式報告。

如果實現支援 IEEE 浮點運算 (IEC 60559),

  • hypot(x, y)hypot(y, x)hypot(x, -y) 是等價的。
  • 如果其中一個引數為 ±0,則 hypot 等價於呼叫 fabs 並傳入非零引數。
  • 如果其中一個引數為 ±∞,即使另一個引數為 NaN,hypot 也返回 +∞。
  • 否則,如果任何引數為 NaN,則返回 NaN。

[編輯] 注意

實現通常保證精度小於 1 ulp(最後一位的單位):GNUBSD

hypot(x, y) 等價於 cabs(x + I*y)

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

hypot(INFINITY, NAN) 返回 +∞,但 sqrt(INFINITY * INFINITY + NAN * NAN) 返回 NaN。

[編輯] 示例

#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
 
int main(void)
{
    // typical usage
    printf("(1,1) cartesian is (%f,%f) polar\n", hypot(1,1), atan2(1, 1));
 
    // special values
    printf("hypot(NAN,INFINITY) = %f\n", hypot(NAN, INFINITY));
 
    // error handling
    errno = 0;
    feclearexcept(FE_ALL_EXCEPT);
    printf("hypot(DBL_MAX,DBL_MAX) = %f\n", hypot(DBL_MAX, DBL_MAX));
    if (errno == ERANGE)
        perror("    errno == ERANGE");
    if (fetestexcept(FE_OVERFLOW))
        puts("    FE_OVERFLOW raised");
}

可能的輸出

(1,1) cartesian is (1.414214,0.785398) polar
hypot(NAN,INFINITY) = inf
hypot(DBL_MAX,DBL_MAX) = inf
    errno == ERANGE: Numerical result out of range
    FE_OVERFLOW raised

[編輯] 參考

  • C23 標準 (ISO/IEC 9899:2024)
  • 7.12.7.3 hypot 函式(p: 待定)
  • 7.25 型別通用數學 <tgmath.h> (p: TBD)
  • F.10.4.3 hypot 函式(p: 待定)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.12.7.3 hypot 函式(p: 181)
  • 7.25 型別通用數學 <tgmath.h> (p: 272-273)
  • F.10.4.3 hypot 函式(p: 382)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.12.7.3 hypot 函式(p: 248)
  • 7.25 型別通用數學 <tgmath.h> (p: 373-375)
  • F.10.4.3 hypot 函式(p: 524)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.12.7.3 hypot 函式(p: 229)
  • 7.22 型別通用數學 <tgmath.h> (p: 335-337)
  • F.9.4.3 hypot 函式(p: 461)

[編輯] 另請參閱

(C99)(C99)
計算給定冪的數字 (xy)
(函式) [編輯]
(C99)(C99)
計算平方根 (x)
(函式) [編輯]
(C99)(C99)(C99)
計算立方根 (3x)
(函式) [編輯]
(C99)(C99)(C99)
計算複數的模
(函式) [編輯]
C++ 文件 用於 hypot