Tanh
定义于头文件 <cmath>
中。
描述
计算 `num` 的双曲正切。该库为所有 cv-unqualified 浮点类型提供了 std::tanh 的重载,作为参数 `num` 的类型 (自 C++23 起).
额外的重载适用于所有整数类型,它们被视为 double (自 C++11 起).
声明
- C++23
- C++11
// 1)
/* floating-point-type */ tanh( /* floating-point-type */ num );
// 2)
float tanhf( float num );
// 3)
long double tanhl( long double num );
// 4)
template< class Integer >
double tanh ( Integer num );
// 1)
float tanh ( float num );
// 2)
double tanh ( double num );
// 3)
long double tanh ( long double num );
// 4)
float tanhf( float num );
// 5)
long double tanhl( long double num );
// 6)
template< class Integer >
double tanh ( Integer num );
参数
`num` - 浮点或整数值
返回值
如果没有错误发生,则返回 `num` 的双曲正弦(tanh(num),或 (enum + e-num)/(enum - e-num))。
错误处理
错误按 math_errhandling 中指定的方式报告。
如果实现支持 IEEE 浮点运算(IEC 60559)
如果参数为 `±0`,则返回 `±0`。如果参数为 `±∞`,则返回 `±1`。如果参数为 NaN,则返回 NaN。
备注
POSIX 规定,在下溢的情况下,`num` 返回未修改的值;如果不支持,则返回不大于 DBL_MIN、FLT_MIN 和 LDBL_MIN 的实现定义值。
不要求额外重载完全按照额外重载提供。它们只需要足以确保对于整数类型的参数 `num`,`std::tanh(num)` 具有与 `std::tanh(static_cast<double>(num))` 相同的效果。
示例
#include <cmath>
#include <iostream>
#include <random>
double get_random_between(double min, double max)
{
std::random_device rd;
std::mt19937 gen(rd());
return std::uniform_real_distribution<>(min, max)(gen);
}
int main()
{
const double x = get_random_between(-1.0, 1.0);
std::cout
<< std::showpos
<< "tanh(+1) = "
<< std::tanh(+1) << '\n'
<< "tanh(-1) = "
<< std::tanh(-1) << '\n'
<< "tanh(x)*sinh(2*x)-cos(2*x) = "
<< std::tanh(x) * std::sinh(2 * x) - std::cosh(2 * x) << '\n'
// special values:
<< "tanh(+0) = "
<< std::tanh(+0.0) << '\n'
<< "tanh(-0) = "
<< std::tanh(-0.0) << '\n';
}
tanh(+1) = +0.761594
tanh(-1) = -0.761594
tanh(x)*sinh(2*x)-cos(2*x) = -1
tanh(+0) = +0
tanh(-0) = -0