sin() / cos() / tan() / asin() / acos() / atan2()
Trigonometric and inverse trigonometric functions defined in <math.h>. All arguments and return values use radians. You may need to pass the -lm linker option at compile time.
Syntax
// Returns the sine of x. Argument is in radians. double sin(double x); // Returns the cosine of x. Argument is in radians. double cos(double x); // Returns the tangent of x. Argument is in radians. double tan(double x); // Returns the arc sine of x. Return value is in radians, in the range [-π/2, π/2]. // The argument must be in the range [-1.0, 1.0]. double asin(double x); // Returns the arc cosine of x. Return value is in radians, in the range [0, π]. // The argument must be in the range [-1.0, 1.0]. double acos(double x); // Returns the arc tangent of y/x. Return value is in radians, in the range [-π, π]. // Correctly handles x=0 and determines the correct quadrant. double atan2(double y, double x);
Function List
| Function | Description | Argument Range |
|---|---|---|
| sin() | Returns the sine of the argument. | Unrestricted (radians). |
| cos() | Returns the cosine of the argument. | Unrestricted (radians). |
| tan() | Returns the tangent of the argument. | Unrestricted (approaches ±infinity near ±π/2). |
| asin() | Returns the arc sine of the argument. | [-1.0, 1.0] |
| acos() | Returns the arc cosine of the argument. | [-1.0, 1.0] |
| atan2() | Returns the arc tangent using two arguments. Correctly determines the quadrant. | Unrestricted (x=0 is allowed). |
Sample Code
#include <stdio.h>
#include <math.h>
// Macro to convert degrees to radians.
#define DEG_TO_RAD(deg) ((deg) * M_PI / 180.0)
int main(void) {
// Basic usage of sin, cos, and tan at 30 degrees.
double deg = 30.0;
double rad = DEG_TO_RAD(deg); // Convert to radians.
printf("sin(30°) = %.6f\n", sin(rad)); // Outputs a value close to 0.5.
printf("cos(30°) = %.6f\n", cos(rad)); // Outputs √3/2 ≈ 0.866025.
printf("tan(30°) = %.6f\n", tan(rad)); // Outputs 1/√3 ≈ 0.577350.
// Use atan2 to get an angle from coordinates (useful for vector direction).
double x = 1.0, y = 1.0; // A vector pointing at 45 degrees.
double angle_rad = atan2(y, x);
double angle_deg = angle_rad * 180.0 / M_PI; // Convert radians back to degrees.
printf("atan2(1, 1) = %.1f°\n", angle_deg); // Outputs "45.0°".
// Reverse calculation with asin: since sin(30°) ≈ 0.5, asin(0.5) ≈ 30°.
double val = 0.5;
double result_deg = asin(val) * 180.0 / M_PI;
printf("asin(0.5) = %.1f°\n", result_deg); // Outputs "30.0°".
// Find the hypotenuse and angle of a 3-4-5 right triangle.
double a = 3.0, b = 4.0;
double hyp = sqrt(a * a + b * b); // Hypotenuse = 5.0
double angle = atan2(b, a) * 180.0 / M_PI; // Angle relative to side a
printf("hyp = %.1f, angle = %.2f°\n", hyp, angle);
// Outputs "hyp = 5.0, angle = 53.13°".
return 0;
}
Notes
All trigonometric functions in C use radians. If you have an angle in degrees, convert it to radians first using the formula deg × π / 180. To convert back from radians to degrees, use rad × 180 / π. The constant M_PI (available in most environments) provides the value of π.
atan2(y, x) differs from the single-argument atan(y/x) in that it correctly handles the case where x is 0 and determines the correct quadrant across the full 0–360° range. Use atan2() instead of atan() when calculating vector angles.
The argument to asin() and acos() must be in the range [-1.0, 1.0]. Passing a value outside this range returns NaN. Floating-point rounding errors can push a value slightly outside this range, so validation before calling may be necessary.
For other math functions, see sqrt() / pow() / fabs() and log() / exp().
If you find any errors or copyright issues, please contact us.