The acos() function computes the arc cosine (inverse cosine) of a number. Given a cosine value between −1 and 1, it returns the angle in radians whose cosine equals that value—essential for geometry, graphics, and robotics.
01
Inverse cos
Angle from ratio.
02
math.h
Link with -lm.
03
Domain
[−1, 1].
04
Range
[0, π] rad.
05
Radians
Not degrees.
06
NaN
|x| > 1.
Fundamentals
Definition and Usage
acos (arc cosine) answers the question: “What angle has this cosine?” If cos(θ) = x and x is between −1 and 1, then acos(x) = θ (in radians).
On a right triangle, cosine is adjacent ÷ hypotenuse. If you know those side lengths, their ratio is a valid input to acos(), and the result is the angle at the vertex between those sides.
💡
Beginner Tip
C always uses radians, not degrees. After acos(), multiply by 180.0 / M_PI if you need degrees. Remember to compile with -lm.
The output range is always [0, π]. acos(-1) gives π (about 3.141593), which is 180°—the largest angle acos() can return.
📈 Practical Patterns
Degrees conversion, real geometry, and inverse verification.
Example 3 — Convert Radians to Degrees
Display the same angle in both units beginners recognize.
C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 0.5;
double rad = acos(x);
double deg = rad * 180.0 / M_PI;
printf("acos(%.1f) = %.4f rad = %.1f degrees\n", x, rad, deg);
return 0;
}
📤 Output:
acos(0.5) = 1.0472 rad = 60.0 degrees
How It Works
Multiply radians by 180/π to get degrees. On Windows MSVC, define _USE_MATH_DEFINES before <math.h> so M_PI is available; on GCC/Linux, M_PI is often available without the define.
This is a classic 3-4-5 triangle. acos(0.6) recovers the angle at the vertex between the adjacent side and the hypotenuse—a pattern used in game physics and navigation.
Example 5 — Verify acos() with cos()
Inverse and forward functions undo each other on valid inputs.
C
#include <stdio.h>
#include <math.h>
int main(void) {
double values[] = { -1.0, -0.5, 0.0, 0.5, 1.0 };
size_t i;
for (i = 0; i < sizeof values / sizeof values[0]; i++) {
double x = values[i];
double angle = acos(x);
double back = cos(angle);
printf("x=%5.1f acos(x)=%.4f cos(acos(x))=%.6f\n",
x, angle, back);
}
return 0;
}
cos(acos(x)) should equal x when x is in [−1, 1]. Tiny floating-point differences may appear beyond six decimal places.
Applications
🚀 Common Use Cases
Geometry — find an angle when you know two side lengths on a right triangle.
2D graphics — compute rotation angles from direction vectors (after normalizing).
Robotics and physics — recover joint angles from cosine relationships in kinematics.
Signal processing — phase calculations involving cosine components.
Pair with cos() — use cos for forward trig and acos to reverse it.
🧠 How acos() Works
1
Validate input
Check that x is in [−1, 1]; otherwise return NaN.
Domain
2
Invert cosine
Find θ such that cos(θ) = x using the math library.
Compute
3
Pick principal value
Return the angle in [0, π] radians (principal branch).
Range
=
📐
Angle in radians
Ready for cos(), rotation matrices, or degree conversion.
Important
📝 Notes
Input must be in [−1, 1]; values outside that range produce NaN.
Output is always in [0, π] radians—never negative, never above π.
Link with -lm when using GCC/Clang on Unix-like systems.
Use acosf or acosl when working with float or long double.
Check isnan(result) after acos if input might be out of range.
Performance
⚡ Optimization
acos() is implemented in the platform math library and is already highly optimized. Avoid calling it inside tight inner loops when you can cache angles. For many graphics tasks, atan2(y, x) is more convenient than combining acos with manual quadrant fixes—but when you already have a cosine ratio, acos is the direct tool.
Wrap Up
Conclusion
acos() turns a cosine value into an angle in radians. Keep the input between −1 and 1, remember to link -lm, and convert to degrees when your users expect them.
Next up: acosh() for inverse hyperbolic cosine, or explore asin() and atan() for the other inverse trig functions.
Convert radians to degrees when displaying to users
Use acosf for float pipelines
Verify results with cos(acos(x)) while learning
❌ Don’t
Pass angles in degrees to acos()
Assume output can be negative or > π
Forget -lm on Linux builds
Ignore NaN when input comes from user data
Use acos when atan2 fits better (quadrant)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about acos()
Inverse cosine in C, explained simply.
5
Core concepts
📐01
Inverse cos
Angle from x.
Basics
📚02
math.h
Link -lm.
Setup
🔢03
[−1, 1]
Valid input.
Domain
🎯04
[0, π]
Output range.
Range
💰05
Radians
Not degrees.
Units
❓ Frequently Asked Questions
acos() returns the arc cosine (inverse cosine) of x—the angle in radians whose cosine equals x. For example, acos(0.5) is about 1.047 radians (60 degrees) because cos(60°) = 0.5.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double acos(double x).
x must be in the range [-1, 1]. That matches any valid cosine ratio. If |x| > 1, acos() returns NaN (Not a Number).
Radians. C trigonometric functions use radians. Multiply by 180.0 / M_PI to convert to degrees (define _USE_MATH_DEFINES before math.h on MSVC for M_PI, or use acos(-1) for pi).
The result is always between 0 and pi radians (about 0 to 3.14159). acos(1) is 0; acos(-1) is pi; acos(0) is pi/2.
cos() takes an angle in radians and returns its cosine. acos() does the reverse: it takes a cosine value and returns the angle. They are inverse operations on the valid domain.
Did you know?
Mathematically, cosine is not one-to-one over all angles, so acos() returns only the principal value between 0 and π. That is why acos(0.5) gives π/3 (60°) and not 300° or other angles that also have cosine 0.5.