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
Definiert in Header <math.h>
float cospif ( float arg ) ;
(1) (seit C23)
double cospi ( double arg ) ;
(2) (seit C23)
long double cospil ( long double arg ) ;
(3) (seit C23)
_Decimal32  cospid32 ( _Decimal32 arg ) ;
(4) (seit C23)
_Decimal64  cospid64 ( _Decimal64 arg ) ;
(5) (seit C23)
_Decimal128 cospid128 ( _Decimal128 arg ) ;
(6) (seit C23)
Definiert in Header <tgmath.h>
#define cospi( arg )
(7) (seit C23)
1-6) Berechnet den Kosinus von π·arg gemessen in Radiant, wodurch arg als Messung in Halbdrehungen betrachtet wird.
7) Typgenerische Makro: ruft die korrekte Funktion basierend auf dem Typ von arg auf. Wenn das Argument einen Ganzzahltyp hat, (2) ( cospi ) wird aufgerufen.

Die Funktionen (4-6) werden genau dann deklariert, wenn die Implementierung __STDC_IEC_60559_DFP__ vordefiniert (d.h. die Implementierung unterstützt dezimale Gleitkommazahlen).

(seit C23)

Inhaltsverzeichnis

Parameter

arg - Fließkommawert, dessen Produkt mit π einen Winkel im Bogenmaß darstellt

Rückgabewert

Wenn keine Fehler auftreten, wird der Kosinus von π·arg ( cos(π×arg) ) im Bereich [-1, +1] zurückgegeben.

Fehlerbehandlung

Fehler werden gemeldet, wie in math_errhandling festgelegt.

Wenn die Implementierung IEEE-Gleitkommaarithmetik (IEC 60559) unterstützt:

  • wenn das Argument ±0 ist, ist das Ergebnis 1.0 ;
  • wenn das Argument ±∞ ist, wird NaN zurückgegeben und FE_INVALID ausgelöst;
  • wenn das Argument NaN ist, wird NaN zurückgegeben.

Beispiel

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#ifndef __GNUC__
#pragma STDC FENV_ACCESS ON
#endif
#if __STDC_VERSION__ < 202311L
// Eine naive Implementierung einer Teilmenge der cospi-Familie
double cospi(double arg)
{
    return cos(arg * (double)3.1415926535897932384626433);
}
#endif
int main(void)
{
    const double pi = acos(-1);
    // Typische Verwendung
    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));
    // Spezielle Werte
    printf("cospi(+0) = %f\n", cospi(0.0));
    printf("cospi(-0) = %f\n", cospi(-0.0));
    // Fehlerbehandlung
    feclearexcept(FE_ALL_EXCEPT);
    printf("cospi(INFINITY) = %f\n", cospi(INFINITY));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

Mögliche Ausgabe:

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

Referenzen

  • C23-Standard (ISO/IEC 9899:2024):
  • 7.12.4.12 The cospi functions (p: 247)
  • 7.27 Type generic math <tgmath.h> (p: 387)

Siehe auch

(C99) (C99)
berechnet Kosinus ( cos(x) )
(Funktion)