cospi, cospif, cospil, cospid32, cospid64, cospid128
来自cppreference.com
| 在标头 <math.h> 定义
|
||
| |
(1) | (C23 起) |
| |
(2) | (C23 起) |
| |
(3) | (C23 起) |
| |
(4) | (C23 起) |
| |
(5) | (C23 起) |
| |
(6) | (C23 起) |
| 在标头 <tgmath.h> 定义
|
||
| |
(7) | (C23 起) |
1-6) 计算
π·arg (以弧度计量)的余弦,arg 按半周计。7) 泛型宏:基于
arg 的类型调用正确的函数。若实参具有整数类型,则调用 (2)(cospi)。|
仅当实现预定义了 |
(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 The cospi functions (第 247 页)
- 7.27 Type generic math <tgmath.h> (第 387 页)
参阅
(C99)(C99) |
计算余弦(cos(x)) (函数) |