The cos() function computes the cosine of an angle. In C, the angle must be in radians. The result is always between −1 and 1—perfect for waves, rotations, and graphics.
01
Cosine
Trig function.
02
math.h
Link with -lm.
03
Radians in
Not degrees.
04
Range
[−1, 1].
05
Unit circle
x-coordinate.
06
vs cosh
Different fn.
Fundamentals
Definition and Usage
Cosine relates an angle to a ratio on a right triangle: adjacent ÷ hypotenuse. On the unit circle (radius 1), cos(θ) is simply the x-coordinate at angle θ.
In C, cos(x) expects x in radians. One full turn is 2π radians (360°). Half a turn is π radians (180°).
💡
Beginner Tip
60° is M_PI / 3.0 radians, and cos(M_PI / 3.0) is 0.5. Do not pass 60 directly—that is 60 radians, not 60 degrees.
At 0°, cosine is 1 and sine is 0. At 90°, they swap. At 180°, cosine is −1. Remember: cos = x, sin = y on the unit circle.
Applications
🚀 Common Use Cases
Graphics and games — rotate sprites, orbit cameras, place objects on circles.
Signal processing — model periodic waves (sound, AC voltage).
Physics — resolve forces into horizontal components.
Robotics — compute joint positions from angles.
Pair with sin() — full 2D position from a single angle and radius.
🧠 How cos() Works
1
Receive radians
Angle x in radians (convert from degrees if needed).
Input
2
Evaluate cosine
Library computes cos using optimized polynomial or hardware instructions.
Compute
3
Return in [−1, 1]
Result is a double representing the x-component on a unit circle.
Output
=
🔢
Cosine value
Multiply by radius for real-world distances: x = r * cos(θ).
Important
📝 Notes
Input is radians, not degrees—convert with degrees * M_PI / 180.0.
Output is always in [−1, 1].
cos() is an even function: cos(-x) == cos(x).
Link with -lm when using GCC/Clang on Unix-like systems.
Do not confuse cos() with cosh() (hyperbolic cosine).
For very large angles, floating-point precision can affect results—consider reducing the angle modulo 2π if needed.
Performance
⚡ Optimization
cos() is implemented in the platform math library and is typically fast enough for most applications. If you call it thousands of times per frame in a game loop, profile first—often the bottleneck is elsewhere. For fixed small angle sets, a lookup table can help, but prefer the library function until profiling proves otherwise.
Wrap Up
Conclusion
cos() computes the cosine of an angle in radians and returns a value between −1 and 1. Convert degrees before calling, pair it with sin() for circular motion, and remember it is not the same as cosh().
Continue with cosh() for hyperbolic math, or explore sin() for the vertical component on the unit circle.
Forget that cos(π/2) may be a tiny non-zero due to floating-point error
Reimplement cosine unless profiling demands it
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about cos()
Trigonometric cosine in C, explained simply.
5
Core concepts
🔢01
Radians in
Not degrees.
Basics
📚02
Range
[−1, 1].
Output
📈03
Unit circle
x = cos(θ).
Geometry
📄04
Even fn
cos(−x)=cos(x).
Property
🔄05
vs cosh
Different.
Compare
❓ Frequently Asked Questions
cos() returns the cosine of an angle given in radians. On a unit circle, cos(theta) is the x-coordinate at angle theta. For example, cos(0) is 1.0 and cos(pi/3) is 0.5 (60 degrees).
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double cos(double x).
Radians. If your angle is in degrees, convert first: radians = degrees * (M_PI / 180.0). Passing 60 directly to cos() is not the same as cos(60 degrees).
The result is always between -1 and 1 inclusive. cos(0) is 1, cos(pi/2) is 0, and cos(pi) is -1.
cos() is a trigonometric cosine (angles in radians, result in [-1, 1]). cosh() is the hyperbolic cosine (uses exponential math, result >= 1). They are different functions despite similar names.
Both take radians. cos(theta) gives the horizontal (x) component on a unit circle; sin(theta) gives the vertical (y) component. cos(pi/2) is 0 while sin(pi/2) is 1.
Did you know?
cos() is an even function: cos(-x) equals cos(x). That symmetry is why a cosine wave looks the same whether time runs forward or backward—unlike sin(), which flips sign for negative angles.