tanh()是C++ STL中的内置函数,它返回以弧度给出的角度的双曲线正切值。
句法 :
tanh(data_type x)
参数:该函数接受一个强制性参数x ,该参数以弧度指定双曲线角。该参数可以是double,float或long double数据类型。
返回值:该函数返回参数的双曲正切值。
下面的程序说明了上述方法:
程序1:
// CPP program to demonstrate the
// tanh() function
#include
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
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
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);
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。