Namespaces
Variants

std:: tanh, std:: tanhf, std:: tanhl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float tanh ( float num ) ;

double tanh ( double num ) ;

long double tanh ( long double num ) ;
(C++23 前)
/*floating-point-type*/
tanh ( /*floating-point-type*/ 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 运算。
(参见 math-floating-point deduced-simd-t 了解其定义。)
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型被视为 double
(since C++11)

目录

参数

num - 浮点数或整数值

返回值

If no errors occur, the hyperbolic tangent of num ( tanh(num) , or
e num
-e -num
e num
+e -num
) is returned.

如果由于下溢发生范围错误,将返回正确结果(舍入后)。

错误处理

错误报告方式遵循 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) )
(函数模板)
对 valarray 的每个元素应用函数 std::tanh
(函数模板)
C 文档 关于 tanh