C Reference ยท ctype.h, math.h and others
C sin(), cos(), tan() and atan2() functions
Reference for C sin(), cos(), tan(), asin(), acos(), atan() and atan2() from math.h: radians versus degrees, polar conversion, domain errors and accuracy.
The trigonometric functions of <math.h> work in radians, not degrees. sin(), cos() and tan() take an angle and return the ratio; asin(), acos() and atan() are their inverses and return an angle. atan2(y, x) is the inverse you usually want: it gives the angle of the point (x, y) in the correct quadrant, from -ฯ to ฯ.
Each has f and l variants for float and long double. There is also a family of hyperbolic functions (sinh, cosh, tanh and inverses) that follows the same pattern.
#include <math.h> double sin(double x); double cos(double x); double tan(double x); double atan2(double y, double x);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
double sin(double x); double cos(double x); double tan(double x); double asin(double x); double acos(double x); double atan(double x); double atan2(double y, double x);
| Name | Description |
|---|---|
x (sin, cos, tan) |
An angle in radians. |
x (asin, acos) |
A value in the range -1 to 1. |
x (atan) |
Any real number. |
y, x (atan2) |
The Cartesian coordinates of a point. Note the order: y first. |
Returns: sin and cos return values from -1 to 1. tan returns any real number. asin returns -ฯ/2 to ฯ/2, acos returns 0 to ฯ, atan returns -ฯ/2 to ฯ/2, and atan2 returns -ฯ to ฯ.
Errors / undefined behavior: none is undefined. asin and acos outside [-1, 1] return NaN with a domain error. sin, cos and tan of an infinity return NaN with a domain error. atan2(0, 0) returns 0 (with signs, atan2(ยฑ0, -0) returns ยฑฯ); no error is raised.
Usage notes
- Radians. Convert with
radians = degrees * PI / 180. PlainM_PIis a POSIX extension rather than ISO C and may be missing under strict-std=c23, which is why the examples definePI. See Math constants. - Rounding noise. ฯ cannot be represented exactly, so
sin(PI)is about 1.2e-16 instead of 0, andcos(PI / 2)is not 0. Compare with a tolerance, and print with%.4frather than%gor%fwith many digits. tannear 90 degrees returns a very large number, not infinity, because the argument is never exactly ฯ/2.- Prefer
atan2overatan(y / x): it handlesx == 0without dividing and returns the right quadrant. - Accuracy. Typical libraries are accurate to within 1 ulp, but the exact last digits differ between platforms and compilers, so avoid comparing results with
==or printing more than ten digits in tests. - Large arguments. The reduction of huge angles is correct in glibc, but the result becomes meaningless to the extent that the input has no fractional precision.
- Speed. A call costs tens of nanoseconds. Precompute tables or use incremental rotation inside tight loops.
- Complex numbers use
csin,ccosand others fromcomplex.h. - Standard version. The
doubleforms are in C89;fandlvariants in C99; C23 addssinpi,cospi,tanpiandasinpistyle functions (which work in half-turns) that GCC 15's glibc 2.28 does not provide.