当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ hypot()用法及代码示例


C++ hypot() 函数

hypot() 函数是 cmath 头文件的库函数,用于求给定数字的斜边,它接受两个数字并返回斜边的计算结果,即sqrt(x*x + y*y)

hypot() 函数的语法:

    hypot(x, y);

参数: x, y– 要计算斜边的数字(或sqrt(x*x + y*y))

返回值: double– 它返回作为表达式结果的双精度值sqrt(x*x + y*y)

例:

    Input:
    float x = 10.23;
    float y = 2.3;

    Function call:
    hypot(x, y);

    Output:
    10.4854

C++代码演示hypot()函数的例子

// C++ code to demonstrate the example of 
// hypot() function

#include <iostream>
#include <cmath>
using namespace std;

// main code section
int main()
{
    float x = 10.23;
    float y = 2.3;

    // calculate the hypotenuse 
    float result = hypot(x, y);
    cout<<"hypotenuse of "<<x<<" and "<<y<<" is = "<<result;
    cout<<endl;
    
    //using sqrt() function
    result = sqrt((x*x + y*y));
    cout<<"hypotenuse of "<<x<<" and "<<y<<" is = "<<result;
    cout<<endl;
    
    return 0;
}

输出

hypotenuse of 10.23 and 2.3 is = 10.4854
hypotenuse of 10.23 and 2.3 is = 10.4854


相关用法


注:本文由纯净天空筛选整理自 hypot() function with example in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。