math_cosh
math_cosh (쌍곡 코사인)
std::cosh는 num의 쌍곡 코사인을 계산하는 함수예요. 수학적으로 (e^num + e^-num)/2와 같아요.
출처: cppreference
본문
<cmath> 헤더에 정의되어 있고, 시그니처는 다음과 같아요.
float cosh ( float num );
double cosh ( double num );
long double cosh ( long double num );
(1) (until C++23)
/*floating-point-type*/
cosh ( /*floating-point-type*/ num );
(since C++23) (constexpr since C++26)
float coshf( float num );
(2) (since C++11) (constexpr since C++26)
long double coshl( long double num );
(3) (since C++11) (constexpr since C++26)
SIMD 오버로드 (since C++26): <simd> 헤더에 정의돼요.
template< /*math-floating-point*/ V >
constexpr /*deduced-simd-t*/<V>
cosh ( const V& v_num );
(S) (since C++26)
추가 오버로드 (since C++11): <cmath> 헤더에 정의돼요.
template< class Integer >
double cosh ( Integer num );
(A) (constexpr since C++26)
- 1-3) num의 쌍곡 코사인을 계산해요. 라이브러리는 매개변수의 타입으로 모든 cv 한정되지 않은 부동소수점 타입에 대한
std::cosh오버로드를 제공해요. (C++23부터) - S) SIMD 오버로드는 v_num에 요소별
std::cosh을 수행해요. - A) 모든 정수 타입에 대해 추가 오버로드가 제공되는데, double로 취급돼요.
(C++11부터)
매개변수 (Parameters)
- num — 부동소수점 또는 정수 값
반환값 (Return value)
오류가 없다면 num의 쌍곡 코사인(cosh(num), 또는 (e^num+e^-num)/2)을 반환해요.
오버플로로 인한 범위 오류가 발생하면 +HUGE_VAL, +HUGE_VALF, 또는 +HUGE_VALL을 반환해요.
오류 처리 (Error handling)
오류는 math_errhandling에 지정된 대로 보고돼요.
구현이 IEEE 부동소수점 연산(IEC 60559)을 지원한다면:
- 인자가 ±0이면 1을 반환해요.
- 인자가 ±∞이면 +∞를 반환해요.
- 인자가 NaN이면 NaN을 반환해요.
참고 (Notes)
IEEE 호환 타입 double의 경우 |num| > 710.5이면 std::cosh(num)이 오버플로돼요.
추가 오버로드는 정확히 (A) 형태로 제공될 필요는 없어요. 정수 타입의 인자 num에 대해 std::cosh(num)이 std::cosh(static_cast<double>(num))과 같은 효과만 있으면 충분해요.
예제 (Example)
이 코드를 실행해 봐요.
#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'
<< "cosh(0) = " << std::cosh(0) << '\n';
// 오버플로 검사
std::feclearexcept(FE_ALL_EXCEPT);
errno = 0;
std::cout << "cosh(1000) = " << std::cosh(1000) << '\n';
if (errno == ERANGE)
std::cout << " errno == ERANGE\n";
if (std::fetestexcept(FE_OVERFLOW))
std::cout << " FE_OVERFLOW raised\n";
}