函式
<cmath> <ctgmath>

hypot

     double hypot  (double x     , double y);      float hypotf (float x      , float y);long double hypotl (long double x, long double y);
     double hypot (double x     , double y);      float hypot (float x      , float y);long double hypot (long double x, long double y);     double hypot (Type1 x      , Type2 y);       // additional overloads
計算斜邊
返回直角三角形兩直角邊為 xy 的斜邊長度。

該函式返回的值相當於 x 平方與 y 平方之和的平方根(根據勾股定理),但避免了中間值溢位或下溢。

標頭檔案 <tgmath.h> 提供了此函式的型別通用宏版本。
此標頭檔案(<cmath>)還提供了其他算術型別的過載(Type1Type2):這些過載實際上會將引數轉換為 double 型別進行計算,除非至少有一個引數是 long double 型別(在這種情況下,兩者都會被轉換為 long double)。

引數

x, y
用於計算斜邊的直角三角形直角邊的浮點數值。

返回值

(x2+y2) 的平方根。
如果結果的絕對值過大,無法用返回型別的值表示,則函式可能會返回具有正確符號的 HUGE_VAL(或 HUGE_VALFHUGE_VALL)(此時會發生溢位範圍錯誤)。

如果發生上溢範圍錯誤
- 並且 math_errhandling 設定了 MATH_ERRNO:全域性變數 errno 被設定為 ERANGE
- 並且 math_errhandling 設定了 MATH_ERREXCEPT:將引發 FE_OVERFLOW

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
/* hypot example */
#include <stdio.h>      /* printf */
#include <math.h>       /* hypot */

int main ()
{
  double leg_x, leg_y, result;
  leg_x = 3;
  leg_y = 4;
  result = hypot (leg_x, leg_y);
  printf ("%f, %f and %f form a right-angled triangle.\n",leg_x,leg_y,result);
  return 0;
}

輸出

3.000000, 4.000000 and 5.000000 form a right-angled triangle.


另見