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


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

C++ 中的tanh() 函数返回以弧度给出的角度的双曲正切。

该函数在<cmath> 头文件中定义。

[Mathematics] tanh x = tanh(x) [In C++ Programming]

tanh() 原型 [从 C++ 11 标准开始]

double tanh(double x);
float tanh(float x);
long double tanh(long double x);
double tanh(T x); // For integral type

tanh() 函数接受一个以弧度表示的参数,并以 double , floatlong double 类型返回该角度的双曲正切。

x 的双曲正切由下式给出,

C++ tanh() function
tanh()的公式

参数:

tanh() 函数采用一个强制参数,以弧度表示双曲角。

返回:

tanh() 函数返回参数的双曲正切值。

示例 1:tanh() 函数在 C++ 中如何工作?

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double x = 0.50, result;
    result = tanh(x);
    cout << "tanh(x) = " << result << endl;

    // x in Degrees
    double xDegrees = 90;
    x = xDegrees * 3.14159/180;

    result = tanh(x);
    cout << "tanh(x) = " << result << endl;

    return 0;
}

运行程序时,输出将是:

tanh(x) = 0.462117
tanh(x) = 0.917152

示例 2:tanh() 具有整数类型的函数

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int x = 5;
    double result;

    result = tanh(x);
    cout << "tanh(x) = " << result << endl;
    
    return 0;
}

运行程序时,输出将是:

tanh(x) = 0.999909

相关用法


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