Namespaces
Variants

cos, cosf, cosl

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 cosf ( float arg ) ;
(1) (seit C99)
double cos ( double arg ) ;
(2)
long double cosl ( long double arg ) ;
(3) (seit C99)
_Decimal32  cosd32 ( _Decimal32 arg ) ;
(4) (seit C23)
_Decimal64  cosd64 ( _Decimal64 arg ) ;
(5) (seit C23)
_Decimal128 cosd128 ( _Decimal128 arg ) ;
(6) (seit C23)
Definiert in Header <tgmath.h>
#define cos( arg )
(7) (seit C99)
1-6) Berechnet den Kosinus von arg (gemessen in Radiant).
7) Typgenerisches Makro: Wenn das Argument den Typ long double hat, (3) ( cosl ) wird aufgerufen. Andernfalls, wenn das Argument einen ganzzahligen Typ oder den Typ double hat, (2) ( cos ) wird aufgerufen. Andernfalls (1) ( cosf ) wird aufgerufen. Wenn das Argument komplex ist, ruft das Makro die entsprechende komplexe Funktion auf ( ccosf , ccos , ccosl ).

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

(seit C23)

Inhaltsverzeichnis

Parameter

arg - Fließkommawert, der den Winkel im Bogenmaß darstellt

Rückgabewert

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

Das Ergebnis kann wenig oder keine Bedeutung haben, wenn die Größenordnung von arg groß ist.

(bis C99)

Wenn ein Domänenfehler auftritt, wird ein implementierungsdefinierter Wert zurückgegeben (NaN, sofern unterstützt).

Wenn ein Bereichsfehler aufgrund von Unterlauf auftritt, wird das korrekte Ergebnis (nach Rundung) 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.

Hinweise

Der Fall, in dem das Argument unendlich ist, ist in C nicht als Domänenfehler spezifiziert, aber es ist definiert als ein Domänenfehler in POSIX .

Beispiel

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#ifndef __GNUC__
#pragma STDC FENV_ACCESS ON
#endif
int main(void)
{
    const double pi = acos(-1);
    // typische Verwendung
    printf("cos(pi/3) = %f\n", cos(pi / 3));
    printf("cos(pi/2) = %f\n", cos(pi / 2));
    printf("cos(-3*pi/4) = %f\n", cos(-3 * pi / 4));
    // spezielle Werte
    printf("cos(+0) = %f\n", cos(0.0));
    printf("cos(-0) = %f\n", cos(-0.0));
    // Fehlerbehandlung
    feclearexcept(FE_ALL_EXCEPT);
    printf("cos(INFINITY) = %f\n", cos(INFINITY));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

Mögliche Ausgabe:

cos(pi/3) = 0.500000
cos(pi/2) = 0.000000
cos(-3*pi/4) = -0.707107
cos(+0) = 1.000000
cos(-0) = 1.000000
cos(INFINITY) = -nan
    FE_INVALID raised

Referenzen

  • C23-Standard (ISO/IEC 9899:2024):
  • 7.12.4.5 Die cos-Funktionen (S.: TBD)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S.: TBD)
  • F.10.1.5 Die cos-Funktionen (S.: TBD)
  • C17-Standard (ISO/IEC 9899:2018):
  • 7.12.4.5 Die cos-Funktionen (S. 174)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S. 272-273)
  • F.10.1.5 Die cos-Funktionen (S. 378)
  • C11-Standard (ISO/IEC 9899:2011):
  • 7.12.4.5 Die cos-Funktionen (S: 239)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S: 373-375)
  • F.10.1.5 Die cos-Funktionen (S: 519)
  • C99-Standard (ISO/IEC 9899:1999):
  • 7.12.4.5 The cos functions (S. 220)
  • 7.22 Type-generic math <tgmath.h> (S. 335-337)
  • F.9.1.5 The cos functions (S. 456)
  • C89/C90 Standard (ISO/IEC 9899:1990):
  • 4.5.2.5 Die cos-Funktion

Siehe auch

(C99) (C99)
berechnet Sinus ( sin(x) )
(Funktion)
(C99) (C99)
berechnet Tangens ( tan(x) )
(Funktion)
(C99) (C99)
berechnet Arkuskosinus ( arccos(x) )
(Funktion)
(C99) (C99) (C99)
berechnet den komplexen Kosinus
(Funktion)