Namespaces
Variants

cospi, cospif, cospil, cospid32, cospid64, cospid128

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
ヘッダーで定義 <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以降)

目次

翻訳内容: - 「Contents」→「目次」 - その他のテキスト(Parameters、Return value、Error handling、Example、References、See also)はC++関連の専門用語として翻訳せず、原文のまま保持

パラメータ

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)
余弦を計算する ( cos(x) )
(関数)