目录搜索
文字
分享

在头文件<math.h>中定义



float       cosf( float arg );

(1)

(since C99)

double      cos( double arg );

(2)


long double cosl( long double arg );

(3)

(since C99)

Defined in header <tgmath.h>



#define cos( arg )

(4)

(since C99)

1-3)计算余弦arg(以弧度测量)。

4)类型 - 通用宏:如果参数具有类型long doublecosl则被调用。否则,如果参数具有整数类型或类型doublecos则调用该参数。否则,cosf被调用。如果参数是复杂的,则宏调用相应的复变函数(ccosfccosccosl)。

参数

arg

-

以弧度表示角度的浮点值

返回值

返回值

如果没有错误发生,arg(cos(arg))的余弦在-1范围内; +1,返回。

如果arg的大小很大,结果可能几乎没有意义。

(直到C ++ 11)

如果发生域错误,则返回实现定义的值(NaN,如果支持)。

如果由于下溢而发生范围错误,则返回正确的结果(舍入后)。

错误处理

按照math_errhandling中的指定报告错误。

如果实现支持IEEE浮点运算(IEC 60559),

  • 如果参数为±0,则结果为1.0

  • 如果参数是±∞,则返回并FE_INVALID提升NaN

  • 如果参数是NaN,则返回NaN

笔记

参数无限的情况在C中没有被指定为域错误,但是在POSIX中被定义为域错误。

#include <stdio.h>#include <math.h>#include <errno.h>#include <fenv.h>
 #pragma STDC FENV_ACCESS ON
int main(void){
    double pi = acos(-1);    // typical usage    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));    // special values    printf("cos(+0) = %f\n", cos(0.0));    printf("cos(-0) = %f\n", cos(-0.0));    // error handling     feclearexcept(FE_ALL_EXCEPT);    printf("cos(INFINITY) = %f\n", cos(INFINITY));    if(fetestexcept(FE_INVALID)) puts("    FE_INVALID raised");}

可能的输出:

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

参考

  • C11标准(ISO / IEC 9899:2011):

    • 7.12.4.5余弦函数(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余弦函数(p:220)

    • 7.22类型通用数学<tgmath.h>(p:335-337)

    • F.9.1.5余弦函数(p:456)

  • C89 / C90标准(ISO / IEC 9899:1990):

    • 4.5.2.5 cos函数

上一篇:copysignl下一篇:cosf