cospi, cospif, cospil, cospid32, cospid64, cospid128
From cppreference.net
Common mathematical functions
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<math.h>
|
||
|
float
cospif
(
float
arg
)
;
|
(1) | (C23 起) |
|
double
cospi
(
double
arg
)
;
|
(2) | (C23 起) |
|
long
double
cospil
(
long
double
arg
)
;
|
(3) | (C23 起) |
|
_Decimal32 cospid32
(
_Decimal32 arg
)
;
|
(4) | (C23 起) |
|
_Decimal64 cospid64
(
_Decimal64 arg
)
;
|
(5) | (C23 起) |
|
_Decimal128 cospid128
(
_Decimal128 arg
)
;
|
(6) | (C23 起) |
|
定义于头文件
<tgmath.h>
|
||
|
#define cospi( arg )
|
(7) | (C23 起) |
1-6)
计算以弧度计量的
π·arg
的余弦值,因此将
arg
视为以半周为单位的度量。
7)
类型泛型宏:根据
arg
的类型调用对应函数。若参数为整数类型,则调用
(2)
(
cospi
) 版本。
|
当且仅当实现预定义了
|
(since C23) |
目录 |
参数
| arg | - |
浮点数值,其与
π
的乘积表示以弧度计量的角度
|
返回值
若无错误发生,则返回
π·arg
的余弦值(
cos(π×arg)
),其值域为
[-1, +1]
。
错误处理
错误报告方式遵循
math_errhandling
中的规范。
如果实现支持 IEEE 浮点算术 (IEC 60559):
- 若参数为 ±0,则结果为 1.0 ;
- 若参数为 ±∞,则返回 NaN 并引发 FE_INVALID ;
- 若参数为 NaN,则返回 NaN。
示例
运行此代码
#include <errno.h> #include <fenv.h> #include <math.h> #include <stdio.h> #ifndef __GNUC__ #pragma STDC FENV_ACCESS ON #endif #if __STDC_VERSION__ < 202311L // cospi函数族的子集简单实现 double cospi(double arg) { return cos(arg * (double)3.1415926535897932384626433); } #endif int main(void) { const double pi = acos(-1); // 典型用法 printf("cospi(1) = %f, cos(pi) = %f\n", cospi(1), cos(pi)); printf("cospi(0.5) = %f, cos(pi/2) = %f\n", cospi(0.5), cos(pi / 2)); printf("cospi(-0.75) = %f, cos(-3*pi/4) = %f\n", cospi(-0.75), cos(-3 * pi / 4)); // 特殊值 printf("cospi(+0) = %f\n", cospi(0.0)); printf("cospi(-0) = %f\n", cospi(-0.0)); // 错误处理 feclearexcept(FE_ALL_EXCEPT); printf("cospi(INFINITY) = %f\n", cospi(INFINITY)); if (fetestexcept(FE_INVALID)) puts(" FE_INVALID raised"); }
可能的输出:
cospi(1) = -1.000000, cos(pi) = -1.000000
cospi(0.5) = 0.000000, cos(pi/2) = 0.000000
cospi(-0.75) = -0.707107, cos(-3*pi/4) = -0.707107
cospi(+0) = 1.000000
cospi(-0) = 1.000000
cospi(INFINITY) = -nan
FE_INVALID raised
参考文献
- C23 标准 (ISO/IEC 9899:2024):
-
- 7.12.4.12 cospi 函数 (p: 247)
-
- 7.27 泛型数学 <tgmath.h> (p: 387)
参见
|
(C99)
(C99)
|
计算余弦值 (
\({\small\cos{x} }\)
cos(x)
)
(函数) |