C
数值 | Numerics

cosl

cos, cosf, cosl

在头文件中定义
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

笔记

参数无限的情况在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.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

参考

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