std::tanh, std::tanhf, std::tanhl
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| 在标头 <cmath> 定义
|
||
| (1) | ||
float tanh ( float num ); double tanh ( double num ); long double tanh ( long double num ); |
(C++23 前) | |
/* 浮点数类型 */ tanh ( /* 浮点数类型 */ num ); |
(C++23 起) (C++26 起 constexpr) |
|
float tanhf( float num ); |
(2) | (C++11 起) (C++26 起为 constexpr) |
long double tanhl( long double num ); |
(3) | (C++11 起) (C++26 起为 constexpr) |
| SIMD 重载 (C++26 起) |
||
| 在标头 <simd> 定义
|
||
template< /*math-floating-point*/ V > constexpr /*deduced-simd-t*/<V> tanh ( const V& v_num ); |
(S) | (C++26 起) |
| 额外重载 (C++11 起) |
||
| 在标头 <cmath> 定义
|
||
template< class Integer > double tanh ( Integer num ); |
(A) | (C++26 起为 constexpr) |
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'
// 特殊值:
<< "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 的每个元素 (函数模板) | |
tanh 的 C 文档
| |