cospi、cospif、cospil、cospid32、cospid64、cospid128
From 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 の型に基づいて適切な関数を呼び出します。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 The cospi functions (p: 247)
- 7.27 Type generic math <tgmath.h> (p: 387)
[編集] 関連項目
| (C99)(C99) |
コサインを計算する (cos(x)) (関数) |