C++ 中的 hypot( ), hypotf( ), hypotl( )


在這篇文章中,我們將討論 C++ 中 hypot( ), hypotf( ), hypotl( ) 函式的工作原理、語法和示例。

hypot( ) 函式

此函式用於計算直角三角形的斜邊。此函式返回兩個變數的平方和的平方根。它是 `` 標頭檔案的函式。

什麼是斜邊?

斜邊是直角三角形的最長邊。下圖是直角三角形中斜邊的圖形表示。

在上圖中,三角形的 AC 邊是斜邊。

計算斜邊的公式為:

$$H = \sqrt{x^2+Y^2}$$

語法

Data type hypot(data type X, data type Y);

引數

hypot( ) 接受兩個或三個引數 X、Y。

示例

Inputs: X=3 Y=4
Output: 5
Input: X=12 Y=5
Output: 13

返回值

(X2 + Y2) 的平方根

可遵循的方法

  • 首先,我們初始化兩個變數。

  • 然後我們定義 hypot( ) 函式。

  • 然後我們列印平方根。

使用上述方法,我們可以計算兩個變數的平方和的平方根。它是根據公式 h=sqrt(x2+y2) 計算的。

示例

// c++ program to demonstrate the working of hypot( ) function
#include<cmath.h>
#include<iostream.h>
Using namespace std;
int main( ){
   // initialize the two values
   int a=3, b=4, c;
   cout<< “ A= ”<< a << “B= ” << b;
   // define the hypot( ) function
   c = hypot(a, b);
   cout << “C= “ <<c<<endl;
   double x, y, z;
   x=12;
   y=5;
   cout<< “X=”<<x<< “Y=”<<y;
   z = hypot(x, y);
   cout<< “Z= “<<z;
   return 0;
}

輸出

如果我們執行上面的程式碼,它將生成以下輸出

OUTPUT - A=3 B=4
   C= 5
OUTPUT - X=12 Y=5
   Z=13

hypotf( ) 函式

hypotf( ) 函式執行與 hypot 函式相同的任務。但不同之處在於 hypotf( ) 函式返回浮點型資料。引數也是浮點型。它是 `` 標頭檔案的函式。

語法

float hypotf(float x);

示例

Output – X= 9.34 Y=10.09
   Z= 13.75
Output – X= 12.75 Y=5.56
   Z= 13.90956

可遵循的方法

  • 首先,我們用浮點型資料初始化兩個變數。

  • 然後我們定義 hypotf( ) 函式。

  • 然後我們列印平方根。

透過以上方法,我們可以計算平方根。

示例

// c++ program to demonstrate the working of hypotf( ) function
#include<iostream.h>
#include<cmath.h>
Using namespace std;
int main( ){
   float x = 12.75, y = 5.56, z;
   cout<< “X= “<<x<< “Y= “ <<y;
   z = hypotf(x, y);
   cout << “Z= “<<z;
   return 0;
}

輸出

如果我們執行上面的程式碼,它將生成以下輸出

OUTPUT – X= 12.75 Y=5.56
   Z=13.90956
OUTPUT – X=9.34 Y=10.09
   Z= 13.75

hypotl( ) 函式

hypotl( ) 函式執行與 hypotl( ) 函式相同的任務,但不同之處在於 hypotl( ) 函式返回長雙精度型資料。引數也是長雙精度型資料。它是 `` 標頭檔案的函式。

語法

長雙精度型 hypotl(長雙精度型 z)

示例

Output – X= 9.34 Y=10.09
Z= 13.75
Output – X= 12.75 Y=5.56
Z= 13.90956

可遵循的方法

  • 首先,我們用長雙精度型資料初始化兩個變數。

  • 然後我們定義 hypotl( ) 函式。

  • 然後我們列印平方根。

透過以上方法,我們可以計算平方根。

示例

// c++ program to demonstrate the working of hypotl( ) function
#include<iostream.h>
#include<cmath.h>
Using namespace std;
int main( ){
   long double x = 9.342553435, y = 10.0987456456, z;
   cout<< “X= “<<x<< “Y= “ <<y;
   z = hypotl(x, y);
   cout<< “Z= “<<z;
   return 0;
}

輸出

如果我們執行上面的程式碼,它將生成以下輸出

OUTPUT – X= 9.3425453435 Y=10.0987456456
   Z=13.7575
OUTPUT – X= 12.5854555 Y=5.125984
   Z= 184.6694021107363

更新於:2020年2月28日

瀏覽量 158 次

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.