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

来自cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
在标头 <cmath> 定义
(1)
float       cosh ( float num );

double      cosh ( double num );

long double cosh ( long double num );
(C++23 前)
/* 浮点数类型 */
            cosh ( /* 浮点数类型 */ num );
(C++23 起)
(C++26 起 constexpr)
float       coshf( float num );
(2)(C++11 起)
(C++26 起为 constexpr)
long double coshl( long double num );
(3)(C++11 起)
(C++26 起为 constexpr)
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)(C++26 起为 constexpr)
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) 的形式提供。它们只需要能够对它们的整数类型实参 num 确保 std::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';
 
    // 特殊值
    std::cout << "cosh(+0) = " << std::cosh(0.0) << '\n'
              << "cosh(-0) = " << std::cosh(-0.0) << '\n';
 
    // 错误处理
    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\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

[编辑] 参阅

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