命名空间
变体
操作

std::cosh, std::coshf, std::coshl

来自 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)
三角函数
双曲函数
cosh
(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       cosh ( float num );

double      cosh ( double num );

long double cosh ( long double num );
(直到 C++23)
/*floating-point-type*/
            cosh ( /*floating-point-type*/ num );
(自 C++23 起)
(constexpr 自 C++26 起)
float       coshf( float num );
(2) (自 C++11 起)
(constexpr 自 C++26 起)
long double coshl( long double num );
(3) (自 C++11 起)
(constexpr 自 C++26 起)
SIMD 重载 (自 C++26 起)
定义于头文件 <simd>
template< /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/<V>

            cosh ( const V& v_num );
(S) (自 C++26 起)
附加重载 (自 C++11 起)
定义于头文件 <cmath>
template< class Integer >
double      cosh ( Integer num );
(A) (constexpr 自 C++26 起)
1-3) 计算 num 的双曲余弦。 库为所有 cv 限定的浮点类型提供了 std::cosh 的重载作为参数类型。(自 C++23 起)
S) SIMD 重载对 v_num 执行元素级别的 std::cosh
(有关定义,请参见 math-floating-pointdeduced-simd-t。)
(自 C++26 起)
A) 为所有整数类型提供了附加重载,它们被视为 double 类型。
(自 C++11 起)

内容

[编辑] 参数

num - 浮点值或整数值

[编辑] 返回值

如果没有错误发生,则返回 num 的双曲余弦 (cosh(num),或
enum
+e-num
2
)。

如果发生由于溢出引起的范围错误,则返回 +HUGE_VAL+HUGE_VALF+HUGE_VALL

[编辑] 错误处理

错误报告方式如 math_errhandling 中所指定。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • 如果参数是 ±0,则返回 1。
  • 如果参数是 ±∞,则返回 +∞。
  • 如果参数是 NaN,则返回 NaN。

[编辑] 注释

对于 IEEE 兼容的 double 类型,如果 |num| > 710.5,则 std::cosh(num) 会溢出。

附加重载不需要完全按照 (A) 的形式提供。它们只需要足以确保对于整数类型的参数 numstd::cosh(num) 具有与 std::cosh(static_cast<double>(num)) 相同的效果。

[编辑] 示例

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
 
int main()
{
    const double x = 42;
 
    std::cout << "cosh(1) = " << std::cosh(1) << '\n'
              << "cosh(-1) = " << std::cosh(-1) << '\n'
              << "log(sinh(" << x << ")+cosh(" << x << ")) = "
              << std::log(std::sinh(x) + std::cosh(x)) << '\n';
 
    // special values
    std::cout << "cosh(+0) = " << std::cosh(0.0) << '\n'
              << "cosh(-0) = " << std::cosh(-0.0) << '\n';
 
    // error handling
    errno=0;
    std::feclearexcept(FE_ALL_EXCEPT);
 
    std::cout << "cosh(710.5) = " << std::cosh(710.5) << '\n';
 
    if (errno == ERANGE)
        std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_OVERFLOW))
        std::cout << "    FE_OVERFLOW raised\n";
}

可能的输出

cosh(1) = 1.54308
cosh(-1) = 1.54308
log(sinh(42)+cosh(42)) = 42
cosh(+0) = 1
cosh(-0) = 1
cosh(710.5) = inf
    errno == ERANGE: Numerical result out of range
    FE_OVERFLOW raised

[编辑] 参见

(C++11)(C++11)
计算双曲正弦 (sinh(x))
(函数) [编辑]
(C++11)(C++11)
计算双曲正切 (tanh(x))
(函数) [编辑]
(C++11)(C++11)(C++11)
计算反双曲余弦 (arcosh(x))
(函数) [编辑]
计算复数的双曲余弦 (cosh(z))
(函数模板) [编辑]
将函数 std::cosh 应用于 valarray 的每个元素
(函数模板) [编辑]
C 文档 for cosh