std::tanh, std::tanhf, std::tanhl
来自 cppreference.cn
定义于头文件 <cmath> |
||
(1) | ||
float tanh ( float num ); double tanh ( double num ); |
(直到 C++23) | |
/*floating-point-type*/ tanh ( /*floating-point-type*/ num ); |
(自 C++23 起) (constexpr 自 C++26 起) |
|
float tanhf( float num ); |
(2) | (自 C++11 起) (constexpr 自 C++26 起) |
long double tanhl( long double num ); |
(3) | (自 C++11 起) (constexpr 自 C++26 起) |
SIMD 重载 (自 C++26 起) |
||
定义于头文件 <simd> |
||
template< /*math-floating-point*/ V > constexpr /*deduced-simd-t*/<V> |
(S) | (自 C++26 起) |
附加重载 (自 C++11 起) |
||
定义于头文件 <cmath> |
||
template< class Integer > double tanh ( Integer num ); |
(A) | (constexpr 自 C++26 起) |
1-3) 计算 num 的双曲正切。 库为所有 cv 限定的非浮点类型提供了
std::tanh
的重载作为参数类型。(自 C++23 起)
S) SIMD 重载对 v_num 执行元素级的
std::tanh 。
|
(自 C++26 起) |
A) 为所有整数类型提供了额外的重载,它们被视为 double。
|
(自 C++11 起) |
内容 |
[编辑] 参数
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 的实现定义值。
附加的重载不需要完全按照 (A) 的方式提供。它们只需要足以确保对于整数类型的参数 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)-cosh(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)-cosh(2*x) = -1 tanh(+0) = +0 tanh(-0) = -0
[编辑] 参见
(C++11)(C++11) |
计算双曲正弦 (sinh(x)) (函数) |
(C++11)(C++11) |
计算双曲余弦 (cosh(x)) (函数) |
(C++11)(C++11)(C++11) |
计算反双曲正切 (artanh(x)) (函数) |
计算复数的双曲正切 (tanh(z)) (函数模板) | |
将函数 std::tanh 应用于 valarray 的每个元素 (函数模板) | |
C 文档 关于 tanh
|