C++ std::tanh 计算双曲正切:tanh(x) = sinh(x) / cosh(x)。结果落在 (-1, 1),在原点附近近似于 x,向两端饱和接近 ±1。
原型
double tanh(double x);参数
x:实数。
返回值
双曲正切,范围 (-1, 1)。
示例
#include <cmath>
#include <iostream>
int main() {
std::cout << std::tanh(0.0) << std::endl;
std::cout << std::tanh(1.0) << std::endl;
return 0;
}输出:
0
0.761594
注意事项
tanh(1) 约为 0.761594。直接写 sinh(x)/cosh(x) 在 |x| 很大时可能溢出,应调用 tanh。反双曲正切 atanh 的定义域为 (-1, 1)。