Namespaces
Variants

cospi, cospif, cospil, cospid32, cospid64, cospid128

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
헤더 파일에 정의됨 <math.h>
float cospif ( float arg ) ;
(1) (C23부터)
double cospi ( double arg ) ;
(2) (C23부터)
long double cospil ( long double arg ) ;
(3) (C23부터)
_Decimal32  cospid32 ( _Decimal32 arg ) ;
(4) (C23부터)
_Decimal64  cospid64 ( _Decimal64 arg ) ;
(5) (C23부터)
_Decimal128 cospid128 ( _Decimal128 arg ) ;
(6) (C23부터)
헤더 파일에 정의됨 <tgmath.h>
#define cospi( arg )
(7) (C23부터)
1-6) 라디안으로 측정된 π·arg 의 코사인을 계산합니다. 따라서 arg 를 반바퀴 회전 단위의 측정값으로 간주합니다.
7) 타입-제네릭 매크로: arg 의 타입에 따라 올바른 함수를 호출합니다. 인자가 정수 타입을 가지는 경우, (2) ( cospi )가 호출됩니다.

함수 (4-6) 은 구현이 __STDC_IEC_60559_DFP__ 를 미리 정의하는 경우에만 선언됩니다 (즉, 구현이 10진 부동 소수점 숫자를 지원하는 경우).

(C23부터)

목차

매개변수

arg - π 와의 곱이 라디안 단위의 각도를 나타내는 부동 소수점 값

반환값

오류가 발생하지 않으면, π·arg ( cos(π×arg) )의 코사인 값이 [-1, +1] 범위에서 반환됩니다.

오류 처리

오류는 math_errhandling 에 명시된 대로 보고됩니다.

구현체가 IEEE 부동 소수점 연산(IEC 60559)을 지원하는 경우:

  • 인자가 ±0인 경우, 결과는 1.0 입니다;
  • 인자가 ±∞인 경우, NaN이 반환되고 FE_INVALID 가 발생합니다;
  • 인자가 NaN인 경우, NaN이 반환됩니다.

예제

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#ifndef __GNUC__
#pragma STDC FENV_ACCESS ON
#endif
#if __STDC_VERSION__ < 202311L
// cospi 계열 함수의 부분 집합을 구현한 단순한 구현체
double cospi(double arg)
{
    return cos(arg * (double)3.1415926535897932384626433);
}
#endif
int main(void)
{
    const double pi = acos(-1);
    // 일반적인 사용법
    printf("cospi(1) = %f, cos(pi) = %f\n", cospi(1), cos(pi));
    printf("cospi(0.5) = %f, cos(pi/2) = %f\n", cospi(0.5), cos(pi / 2));
    printf("cospi(-0.75) = %f, cos(-3*pi/4) = %f\n", cospi(-0.75), cos(-3 * pi / 4));
    // 특수 값
    printf("cospi(+0) = %f\n", cospi(0.0));
    printf("cospi(-0) = %f\n", cospi(-0.0));
    // 오류 처리
    feclearexcept(FE_ALL_EXCEPT);
    printf("cospi(INFINITY) = %f\n", cospi(INFINITY));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

가능한 출력:

cospi(1) = -1.000000, cos(pi) = -1.000000
cospi(0.5) = 0.000000, cos(pi/2) = 0.000000
cospi(-0.75) = -0.707107, cos(-3*pi/4) = -0.707107
cospi(+0) = 1.000000
cospi(-0) = 1.000000
cospi(INFINITY) = -nan
    FE_INVALID raised

참고문헌

  • C23 표준 (ISO/IEC 9899:2024):
  • 7.12.4.12 cospi 함수 (p: 247)
  • 7.27 Type generic math <tgmath.h> (p: 387)

참고 항목

(C99) (C99)
코사인을 계산함 ( cos(x) )
(함수)