cos(), cosf(), cosl() — Calculate cosine

Standards

Standards / Extensions C or C++ Dependencies
ISO C
POSIX.1
XPG4
XPG4.2
ISO/ANSI C++
C99
Single UNIX Specification, Version 3
C++ TR1 C99
both  

Format

#include <math.h>

double cos(double x);
float cos(float x);                  /* C++ only */
long double cos(long double x);      /* C++ only */
float cosf(float x);
long double cosl(long double x);

General description

Calculates the cosine of x. The value x is expressed in radians.
Note: These functions work in both IEEE Binary Floating-Point and hexadecimal floating-point formats. See IEEE binary floating-point for more information about IEEE Binary Floating-Point.

Returned value

Returns the calculated value.

If x is outside prescribed limits, the value is not calculated. Instead, the function returns 0 and sets the errno to ERANGE. If the correct value would cause an underflow, zero is returned and the value ERANGE is stored in errno.

Special behavior for XPG4.2: The following error is added:
Error Code
Description
EDOM
The argument exceeded an internal limit for the function (approximately 250).

Example

CELEBC26
/* CELEBC26                                      

   This example calculates y to be the cosine of                                
   x.                                                                           
                                                                                
 */                                                                             
#include <math.h>                                                               
#include <stdio.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   double x, y;                                                                 
                                                                                
   x = 7.2;                                                                     
   y = cos(x);                                                                  
                                                                                
   printf("cos( %lf ) = %lf\n", x, y);                                          
}                                                                               
Output
cos( 7.200000 ) = 0.608351

Related information