cos, cosf, cosl

Header: <math.h>

1-6) Computes the cosine of arg (measured in radians).

# Declarations

float cosf( float arg );

(since C99)

double cos( double arg );
long double cosl( long double arg );

(since C99)

_Decimal32 cosd32( _Decimal32 arg );

(since C23)

_Decimal64 cosd64( _Decimal64 arg );

(since C23)

_Decimal128 cosd128( _Decimal128 arg );

(since C23)

#define cos( arg )

(since C99)

# Parameters

# Return value

If no errors occur, the cosine of arg (cos(arg)) in the range [-1 ; +1], is returned.

# Notes

The case where the argument is infinite is not specified to be a domain error in C, but it is defined as a domain error in POSIX.

# Example

#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);
 
    // typical usage
    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));
 
    // special values
    printf("cos(+0) = %f\n", cos(0.0));
    printf("cos(-0) = %f\n", cos(-0.0));
 
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("cos(INFINITY) = %f\n", cos(INFINITY));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

# See also