C Math acos() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

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.

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.

📝 Syntax

Standard C declaration:

C
double acos(double x);

Related variants

C
float acosf(float x);           /* <math.h> */
long double acosl(long double x); /* <math.h> */

Parameters

  • x — cosine value; must be in the range [−1, 1].

Return Value

  • Angle in radians in the range [0, π].
  • acos(1) returns 0; acos(-1) returns π.
  • If |x| > 1, returns NaN and may set errno to EDOM.

Headers and linking

  • #include <math.h>
  • Compile: gcc acos.c -std=c11 -o acos -lm

⚡ Quick Reference

CallMeaningResult (approx.)
acos(1)cos(0°) = 10 radians
acos(0)cos(90°) = 01.5708 (π/2)
acos(0.5)cos(60°) = 0.51.0472 (π/3)
acos(-1)cos(180°) = −13.1416 (π)
acos(x) * 180 / M_PIConvert to degreesAngle in °
Inverse
acos(x)

Angle from cos

Forward
cos(angle)

Cos from angle

To degrees
rad * 180 / M_PI

Convert

Triangle
acos(adj/hyp)

Geometry

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. The -lm flag links the math library on Linux and macOS.

📚 Getting Started

Compute arc cosine and print the result in radians.

Example 1 — Basic Arc Cosine

Find the angle whose cosine is 0.5 (60° in radians).

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 0.5;
    double angle_rad = acos(x);

    printf("The arccosine of %.1f is: %.6f radians\n", x, angle_rad);

    return 0;
}

How It Works

acos(0.5) returns π/3 radians because cos(π/3) = 0.5. This matches the reference example, with clearer formatting.

Example 2 — Endpoints of the Domain

acos(1) is 0; acos(-1) is π.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("acos( 1) = %.6f radians\n", acos(1.0));
    printf("acos( 0) = %.6f radians\n", acos(0.0));
    printf("acos(-1) = %.6f radians\n", acos(-1.0));

    return 0;
}

How It Works

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;
}

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.

Example 4 — Angle in a Right Triangle

Adjacent = 3, hypotenuse = 5 → cos(θ) = 3/5.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double adjacent = 3.0;
    double hypotenuse = 5.0;
    double cosine_ratio = adjacent / hypotenuse;
    double angle_deg = acos(cosine_ratio) * 180.0 / M_PI;

    printf("cos(theta) = %.2f / %.0f = %.2f\n",
           adjacent, hypotenuse, cosine_ratio);
    printf("theta = %.1f degrees\n", angle_deg);

    return 0;
}

How It Works

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;
}

How It Works

cos(acos(x)) should equal x when x is in [−1, 1]. Tiny floating-point differences may appear beyond six decimal places.

🚀 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.

📝 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.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Include <math.h> and link with -lm
  • Keep x in [−1, 1] before calling
  • 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)

Key Takeaways

Knowledge Unlocked

Five things to remember about acos()

Inverse cosine in C, explained simply.

5
Core concepts
📚 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.

Explore C Math Functions

Continue with acosh() or browse the full math function reference.

Math Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

4 people found this page helpful