Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

cos — C23 mathematical function

`cos(x)` from `<math.h>` computes the cosine of `x` measured in radians and returns `double`; `cosf` and `cosl` serve `float` and `long double`. For finite arguments, base C23 specifies no domain error.

Version: C23

Syntax

double cos(double x);

cos(x) from <math.h> computes the cosine of x measured in radians and returns double; cosf and cosl serve float and long double. For finite arguments, base C23 specifies no domain error.

The example calls cos for 0.0 and an approximation of π. Formatting with zero fractional digits prints 1 -1, the unit-circle x-coordinates for angles zero and π.

Do not pass degrees without converting them to radians; large arguments require implementation argument reduction and can lose accuracy. The special guarantees cos(±0) = 1 and NaN plus the invalid exception for cos(±∞) are Annex F/IEC 60559 requirements, not unconditional base-C behavior.

Examples

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

int main(void) {
    double pi = 3.14159265358979323846;
    printf("%.0f %.0f\n", cos(0.0), cos(pi));
    return 0;
}

Result

1 -1

The example calls `cos` for `0.0` and an approximation of π. Formatting with zero fractional digits prints `1 -1`, the unit-circle x-coordinates for angles zero and π.

Sources