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
) が呼び出されます。
|
関数
(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 // 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)
)
(関数) |