Namespaces
Variants

cos, cosf, cosl

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 cosf ( float arg ) ;
(1) (C99以降)
double cos ( double arg ) ;
(2)
long double cosl ( long double arg ) ;
(3) (C99以降)
_Decimal32  cosd32 ( _Decimal32 arg ) ;
(4) (C23以降)
_Decimal64  cosd64 ( _Decimal64 arg ) ;
(5) (C23以降)
_Decimal128 cosd128 ( _Decimal128 arg ) ;
(6) (C23以降)
ヘッダーで定義 <tgmath.h>
#define cos( arg )
(7) (C99以降)
1-6) 引数 arg の余弦を計算します(ラジアン単位で測定)。
7) 型総称マクロ: 引数の型が long double の場合、 (3) ( cosl )が呼び出される。それ以外の場合、引数が整数型または double 型の場合、 (2) ( cos )が呼び出される。それ以外の場合、 (1) ( cosf )が呼び出される。引数が複素数の場合、マクロは対応する複素数関数( ccosf ccos ccosl )を呼び出す。

関数 (4-6) は、実装が __STDC_IEC_60559_DFP__ を事前定義する場合にのみ宣言される(すなわち、実装が十進浮動小数点数をサポートする場合)。

(C23以降)

目次

パラメータ

arg - ラジアン単位の角度を表す浮動小数点値

戻り値

エラーが発生しない場合、 arg の余弦( cos(arg) )が範囲 [-1 ; +1] 内で返されます。

arg の大きさが大きい場合、結果はほとんど意味を持たないか、全く意味を持たない可能性があります。

(C99まで)

定義域エラーが発生した場合、実装定義の値が返されます(NaNがサポートされている場合はNaN)。

アンダーフローによる範囲エラーが発生した場合、正しい結果(丸め後)が返されます。

エラーハンドリング

エラーは math_errhandling で指定された通りに報告されます。

IEEE浮動小数点演算(IEC 60559)がサポートされている実装の場合:

  • 引数が±0の場合、結果は 1.0 となる;
  • 引数が±∞の場合、NaNが返され、 FE_INVALID が発生する;
  • 引数がNaNの場合、NaNが返される。

注記

引数が無限大の場合の扱いはC言語では定義域エラーと規定されていませんが、 POSIXでは定義域エラー として定義されています。

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#ifndef __GNUC__
#pragma STDC FENV_ACCESS ON
#endif
int main(void)
{
    const double pi = acos(-1);
    // 典型的な使用例
    printf("cos(pi/3) = %f\n", cos(pi / 3));
    printf("cos(pi/2) = %f\n", cos(pi / 2));
    printf("cos(-3*pi/4) = %f\n", cos(-3 * pi / 4));
    // 特殊な値
    printf("cos(+0) = %f\n", cos(0.0));
    printf("cos(-0) = %f\n", cos(-0.0));
    // エラー処理
    feclearexcept(FE_ALL_EXCEPT);
    printf("cos(INFINITY) = %f\n", cos(INFINITY));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

出力例:

cos(pi/3) = 0.500000
cos(pi/2) = 0.000000
cos(-3*pi/4) = -0.707107
cos(+0) = 1.000000
cos(-0) = 1.000000
cos(INFINITY) = -nan
    FE_INVALID raised

参考文献

  • C23規格 (ISO/IEC 9899:2024):
  • 7.12.4.5 cos関数群 (p: TBD)
  • 7.25 総称数学 <tgmath.h> (p: TBD)
  • F.10.1.5 cos関数群 (p: TBD)
  • C17規格 (ISO/IEC 9899:2018):
  • 7.12.4.5 cos関数群 (p: 174)
  • 7.25 型総称数学 <tgmath.h> (p: 272-273)
  • F.10.1.5 cos関数群 (p: 378)
  • C11標準 (ISO/IEC 9899:2011):
  • 7.12.4.5 cos関数 (p: 239)
  • 7.25 総称数学 <tgmath.h> (p: 373-375)
  • F.10.1.5 cos関数 (p: 519)
  • C99規格 (ISO/IEC 9899:1999):
  • 7.12.4.5 cos関数群 (p: 220)
  • 7.22 型総称数学 <tgmath.h> (p: 335-337)
  • F.9.1.5 cos関数群 (p: 456)
  • C89/C90標準 (ISO/IEC 9899:1990):
  • 4.5.2.5 cos関数

関連項目

(C99) (C99)
正弦を計算する ( sin(x) )
(関数)
(C99) (C99)
正接を計算する ( tan(x) )
(関数)
(C99) (C99)
逆余弦を計算する ( arccos(x) )
(関数)
(C99) (C99) (C99)
複素余弦を計算する
(関数)