C ++中的hypot()函數(shù)返回傳遞的參數(shù)平方和的平方根(根據(jù)勾股定理)。
double hypot(double x, double y); float hypot(float x, float y); long double hypot(long double x, long double y); Promoted pow(Type1 x, Type2 y); double hypot(double x, double y, double x); //(從 C++17 開始) float hypot(float x, float y, float z); // (從 C++17 開始) long double hypot(long double x, long double y, long double z); // (從 C++17 開始) Promoted pow(Type1 x, Type2 y, Type2 y); //(從 C++17 開始)
從C ++ 11開始,如果傳遞給hypot()的參數(shù)為long double,則返回類型Promoted為long double。如果不是,則返回類型Promoted為double。
h = √(x2+y2
在數(shù)學(xué)上相當(dāng)于
h = hypot(x, y);
在C ++編程中。
如果傳遞了三個(gè)參數(shù):
h = √(x2+y2+z2))
在數(shù)學(xué)上相當(dāng)于
h = hypot(x, y);
在C ++編程中。
此函數(shù)在<cmath>頭文件中定義。
hytpot()可以使用2或3個(gè)整數(shù)或浮點(diǎn)類型的參數(shù)。
hypot()返回:
如果傳遞了兩個(gè)參數(shù),則為直角三角形的斜邊,即?!?x2+y2)
如果傳遞了三個(gè)參數(shù),則從原點(diǎn)到(x,y,x)的距離?!?x2+y2+z2)
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 2.1, y = 3.1, result;
result = hypot(x, y);
cout << "hypot(x, y) = " << result << endl;
long double yLD, resultLD;
x = 3.52;
yLD = 5.232342323;
// 在本示例中,該函數(shù)返回long double
resultLD = hypot(x, yLD);
cout << "hypot(x, yLD) = " << resultLD;
return 0;
}運(yùn)行該程序時(shí),輸出為:
hypot(x, y) = 3.74433 hypot(x, yLD) = 6.30617
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 2.1, y = 3.1, z = 23.3, result;
result = hypot(x, y, z);
cout << "hypot(x, y, z) = " << result << endl;
return 0;
}注意:該程序僅在支持C ++ 17的新編譯器中運(yùn)行。