cos, cosf, cosl
来自 cppreference.cn
定义于头文件 <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) 仅当实现预定义 |
(自 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); // 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
[编辑] 参考
- 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) |
计算复余弦 (函数) |
C++ 文档 for cos
|