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 )。

当且仅当实现预定义了 __STDC_IEC_60559_DFP__ (即实现支持十进制浮点数)时,函数 (4-6) 才会被声明。

(since 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函数(页码:220)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.1.5 cos函数(页码: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)
计算复数余弦值
(函数)
C++ 文档 关于 cos