名稱空間
變體
操作

cospi, cospif, cospil, cospid32, cospid64, cospid128

來自 cppreference.com
< c‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
(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)。

函式 (4-6) 僅當實現預定義 __STDC_IEC_60559_DFP__(即實現支援十進位制浮點數)時才宣告。

(自 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))
(函式) [編輯]