这个 hypot() 函数在C++中返回传递的参数平方和的平方根。它找到斜边, 斜边 是直角三角形的最长边。其计算公式如下:
h = sqrt(x2+y2)
其中x和y是三角形的另两条边。
Syntax: double hypot(double x, double y); float hypot(float x, float y); long double hypot(long double x, long double y);
例如:
Input : x=3, y=4 Output :5 Input :x=9, y=10 Output :13.4536
解释
头文件 :cmath 参数 :hypopt()接受2或3个整型或浮点型参数。 退换商品 : 1.传递两个参数时直角三角形的斜边。 2.如果传递了三个参数,从原点到(x,y,x)的距离
例外或错误 1.降压(x,y)、降压(y,x)和降压(x,-y)是等效的。 2.如果其中一个参数为0,则hypot(x,y)等于 晶圆厂 用非零参数调用 3.如果其中一个论点是 极大的 或undefined,hypot(x,y)返回undefined。
示例应用程序: 求一个直角三角形的斜边,给定它的两条边。
// CPP program to illustrate // hypot() function #include <cmath> #include <iostream> using namespace std; // Driver Program int main() { double x = 9, y = 10, res; res = hypot(x, y); // hypot() returns double in this case cout << res << endl; long double a, b, result; a = 4.525252; b = 5.767676; // hypot() returns long double in this case result = hypot(a, b); cout << result; return 0; } |
输出:
13.4536 7.33103
函数
hypotf()函数与hypot函数相同。唯一的区别是函数的参数和返回类型是float类型。附加在’hypotf’后面的’f’字符代表float,表示函数的参数类型和返回类型。
Syntax float hypotf(float x);
一个C++程序的实现 在这里,变量被指定为float type 类型不匹配 发生错误。
// CPP program to illustrate // hypotf() function #include <cmath> #include <iostream> using namespace std; // Driver Program int main() { float x = 9.3425, y = 10.0987, res; // hypotf() takes float values and returns float res = hypotf(x, y); cout << res << endl; return 0; } |
输出:
13.7574
函数
hypotl()函数与hypot函数相同。唯一的区别是函数的参数和返回类型是long double类型。附加在’hypotl’后面的’l’字符代表长双精度,表示函数的参数类型和返回类型。
Syntax long double hypotl(long double x);
程序的C++实现 在这里,变量被指定为长双精度类型 类型不匹配 发生错误。
// CPP program to illustrate // hypotl() function #include <cmath> #include <iostream> using namespace std; // Driver Program int main() { long double x = 9.3425453435, y = 10.0987456456, res; // hypotl() takes long double values and returns long double res = hypotl(x, y); cout << res << endl; return 0; } |
输出:
13.7575