6.11.35 cos Function

Calculates the trigonometric cosine function of a double precision floating-point value.

Include

<math.h>

Prototype

double cos(double x);

Argument

x
value for which to return the cosine

Return Value

Returns the cosine of x specified in radians in the range [-1, 1] . NaN is returned if x is ±∞ .

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

#include <math.h>
#include <stdio.h>
#include <errno.h>

int main(void)
{
  double x,y;

  errno = 0;
  x = -1.0;
  y = cos (x);
  if (errno)
    perror("Error");
  printf("The cosine of %f is %f\n", x, y);

  errno = 0;
  x = 0.0;
  y = cos (x);
  if (errno)
    perror("Error");
  printf("The cosine of %f is %f\n", x, y);
}

Example Output

The cosine of -1.000000 is 0.540302
The cosine of 0.000000 is 1.000000