命名空间
变体
操作

cospi、cospif、cospil、cospid32、cospid64、cospid128

来自 cppreference.cn
< c‎ | numeric‎ | math
 
 
 
常用数学函数
函数
基本操作
(C99)
(C99)
(C99)
(C99)(C99)(C99)(C23)
最大值/最小值操作
(C99)
(C99)
指数函数
(C23)
(C99)
(C99)
(C23)
(C23)

(C99)
(C99)(C23)
(C23)
(C23)
幂函数
(C99)
(C23)
(C23)

(C99)
(C23)
(C23)
三角函数和双曲函数
(C23)
cospi
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
(C99)
(C99)
(C99)
最近整数浮点
(C99)(C99)(C99)
(C99)

(C99)(C99)(C99)
(C23)(C23)(C23)(C23)
浮点操作
(C99)(C99)
(C99)(C23)
(C99)
窄化操作
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
量子和量子指数
十进制重编码函数
总阶数和有效载荷函数
分类
(C99)
(C99)
(C99)
(C23)
误差和伽玛函数
(C99)
(C99)
(C99)
(C99)
类型
宏常量
特殊浮点值
(C99)(C23)
参数和返回值
错误处理
快速操作指示符
 
定义于头文件 <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)。

当且仅当实现预定义了 __STDC_IEC_60559_DFP__ 时,才声明函数 (4-6) (即,实现支持十进制浮点数)。

(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
// A naive implementation of a subset of cospi family
double cospi(double arg)
{
    return cos(arg * (double)3.1415926535897932384626433);
}
#endif
 
int main(void)
{
    const double pi = acos(-1);
 
    // typical usage
    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));
 
    // special values
    printf("cospi(+0) = %f\n", cospi(0.0));
    printf("cospi(-0) = %f\n", cospi(-0.0));
 
    // error handling
    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)
计算余弦 (cos(x))
(函数) [编辑]