Cosh
定义于头文件 <cmath>
中。
描述
计算 num
的双曲余弦。库为所有 cv-unqualified 浮点类型提供了 std::cosh 的重载作为参数 num
的类型。 (自 C++23 起)
附加重载 为所有整数类型提供,它们被视为 double。 (自 C++11 起)
声明
- C++23
- C++11
// 1)
/* floating-point-type */ cosh( /* floating-point-type */ num );
// 2)
float coshf( float num );
// 3)
long double coshl( long double num );
// 4)
template< class Integer >
double cosh ( Integer num );
// 1)
float cosh ( float num );
// 2)
double cosh ( double num );
// 3)
long double cosh ( long double num );
// 4)
float coshf( float num );
// 5)
long double coshl( long double num );
// 6)
template< class Integer >
double cosh ( Integer num );
参数
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)
溢出。
额外的重载不需要完全按照额外重载提供。它们只需要足以确保对于其整数类型的参数 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';
// 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