Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C Language Dictionary

  1. Home
  2. C Language Dictionary
  3. sin() / cos() / tan() / asin() / acos() / atan2()

sin() / cos() / tan() / asin() / acos() / atan2()

Since: C89(1989)

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

double sin(double x);

double cos(double x);

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

FunctionDescriptionArgument 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

sample_sin_cos_tan.c
#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;
}

Run the following command:

gcc sin_cos_tan.c -o sin_cos_tan -lm
./sin_cos_tan
sin(30°) = 0.500000
cos(30°) = 0.866025
tan(30°) = 0.577350
atan2(1, 1) = 45.0°
asin(0.5) = 30.0°
hyp = 5.0, angle = 53.13°

Common Mistakes

Common Mistake: Passing Degrees Directly

The arguments of sin(), cos(), and tan() are in radians. Passing a degree value directly computes the wrong result.

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

int main(void) {
    /* NG: passing 90 (degrees) computes sin(90 radians), not sin(90°) */
    printf("NG: sin(90) = %.6f\n", sin(90.0)); /* 0.893997... */

    /* OK: convert degrees to radians first */
    printf("OK: sin(90°) = %.6f\n", sin(90.0 * M_PI / 180.0)); /* 1.000000 */

    return 0;
}

Run the following command:

gcc sin_degree_ng.c -o sin_degree_ng -lm
./sin_degree_ng
NG: sin(90) = 0.893997
OK: sin(90°) = 1.000000

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 .