cospi, cospif, cospil, cospid32, cospid64, cospid128
來自 cppreference.com
定義於標頭檔案 <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) 僅當實現預定義 |
(自 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)) (函式) |