Name

cos

Synopsis

Calculates the cosine of an angle

#include <math.h>
doublecos( double x );
double cosf( float x );        (C99)
double cosl( long double x );        (C99)

The cos() function returns the cosine of its argument, which is an angle measure in radians. The return value is in the range -1cos(x) ≤ 1.

Example

/*
 * Calculate the sloping width of a roof
 * given the horizontal width
 * and the angle from the horizontal.
 */
#define PI 3.141593
#define DEG_PER_RAD (180.0/PI)

double roof_pitch = 20.0;            // In degrees
double floor_width = 30.0;           // In feet, say.
double roof_width = 1.0 / cos( roof_pitch / DEG_PER_RAD ) * floor_width;

printf( "The sloping width of the roof is %4.2f ft.\n", roof_width );

This code produces the following output:

The sloping width of the roof is 31.93 ft.

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.