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


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


tanh()是C++ STL中的内置函数,可返回以弧度表示的角度的双曲线正切值。

用法:

tanh(data_type x)

参数:该函数接受一个强制性参数x,该参数指定弧度中的双曲角。该参数可以是double,float或long double数据类型。


返回值:该函数返回参数的双曲正切值。

以下示例程序旨在说明上述方法:

示例1:

// CPP program to demonstrate the 
// tanh() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    double x = 4.1, result; 
  
    result = tanh(x); 
    cout << "tanh(4.1) = " << result << endl; 
  
    // x in Degrees 
    double xDegrees = 90; 
    x = xDegrees * 3.14159 / 180; 
  
    result = tanh(x); 
    cout << "tanh(90 degrees) = " << result << endl; 
  
    return 0; 
}
输出:
tanh(4.1) = 0.999451
tanh(90 degrees) = 0.917152

示例2:

// CPP program to demonstrate the 
// tanh() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    int x = -4; 
    double result; 
  
    result = tanh(x); 
    cout << "tanh(-4) = " << result << endl; 
  
    // x in Degrees 
    double xDegrees = 90; 
    x = xDegrees * 3.14159 / 180; 
  
    result = tanh(x); 
    cout << "tanh(90 degrees) = " << result << endl; 
  
    return 0; 
}
输出:
tanh(-4) = -0.999329
tanh(90 degrees) = 0.761594

错误和异常:当字符串或字符作为参数传递时,该函数不返回任何匹配函数来调用错误。

示例3:

// CPP program to demonstrate the tanh() 
// function when a string is passed as argument  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    string x = "gfg"; 
    double result; 
  
    result = tanh(x); 
    cout << "tanh(x) = " << result << endl; 
  
    return 0; 
}

输出:

prog.cpp:14:20: error: no matching function for call to 'tanh(std::__cxx11::string&)'
result = tanh(x);


相关用法


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