命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | numeric‎ | math
 
 
 
常用数学函数
函数
基本操作
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指数函数
(C++11)
(C++11)

(C++11)
(C++11)
幂函数
(C++11)
(C++11)
三角
双曲函数
tanh
(C++11)
(C++11)
(C++11)

误差函数和伽马函数
(C++11)
(C++11)
(C++11)
(C++11)
取整浮点运算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮点操纵函数
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分类和比较
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型
(C++11)
(C++11)
(C++11)
宏常量
分类
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定义于头文件 <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< /*数学浮点类型*/ V >

constexpr /*推导 SIMD 类型*/<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-pointdeduced-simd-t。)
(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)。它们只需足以确保对于整数类型的参数 numstd::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