C++ std::cos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::cos(x) calculates the cosine of an angle. In a right triangle, the cosine is the side next to the angle divided by the long side (hypotenuse). It always gives a value from -1 to 1.

Like std::sin(), the angle must be in radians. The cosine of 0 is 1, it falls to 0 at a quarter turn, and it reaches -1 at a half turn. It is used to find the horizontal part of a movement and to rotate points.

std::cos() is declared in <cmath> and is part of the group Trigonometry. Trigonometric functions use radians, not degrees. To convert degrees, multiply by std::numbers::pi / 180 (C++20).

Syntax

Include <cmath> and call std::cos. Start with these lines.

C++
double a = std::cos(0.0);                    // 1
double b = std::cos(std::numbers::pi);       // -1 (180 degrees)
float  c = std::cos(0.5f);                   // float version
CallWhat it does
std::cos(0.0)The cosine of 0 radians. Returns 1.
std::cos(std::numbers::pi / 3)pi / 3 radians is 60 degrees. Returns about 0.5.
std::cos(180 * std::numbers::pi / 180)A degree angle after conversion. Returns -1.

Angles are in radians. Convert degrees with deg * std::numbers::pi / 180. std::numbers::pi needs <numbers> and C++20. With older compilers write const double pi = 3.14159265358979323846; yourself.

Library declarations
C++
float       cos(float x);
double      cos(double x);
long double cos(long double x);
double      cos(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. The angle in radians. It can be float, double, long double, or an integer.

Return Value

Return ValueDescription
CosineA value between -1 and 1 for the angle x.
NaNReturned when x is infinity or NaN.
1cos(0.0) and cos(-0.0) both return exactly 1.

C++ Examples

Example 1 — Cosine of Common Angles

Convert each angle from degrees to radians, then print the cosine with four decimals.

C++
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>

int main() {
    std::cout << std::fixed << std::setprecision(4);

    for (int deg : {0, 60, 90, 180}) {
        double rad = deg * std::numbers::pi / 180.0;
        std::cout << "cos(" << deg << " deg) = " << std::cos(rad) << '\n';
    }
    return 0;
}

How It Works

  1. Each angle in degrees is changed to radians by deg * std::numbers::pi / 180.0 before cos() sees it.
  2. cos(0) is the biggest value, 1.0000. At 60 degrees the cosine drops to 0.5000.
  3. At 90 degrees it is almost 0, and at 180 degrees it reaches -1.0000. The cosine goes down as the angle opens up.

Example 2 — The Tiny Number Instead of Zero

See why cos(90 degrees) is not exactly zero, and how to test for it safely.

C++
#include <cmath>
#include <iostream>
#include <numbers>

int main() {
    double c = std::cos(90.0 * std::numbers::pi / 180.0);

    std::cout << "cos(90 deg) = " << c << '\n';
    std::cout << "c == 0:       " << (c == 0.0) << '\n';
    std::cout << "abs(c) < 1e-9: " << (std::abs(c) < 1e-9) << '\n';
    return 0;
}

How It Works

  1. The value of pi stored in a double is a tiny bit off, so the angle is not exactly a quarter turn. The cosine is 6.12323e-17.
  2. The test c == 0.0 is false, so the program prints 0, even though the answer is zero for all practical purposes.
  3. Comparing std::abs(c) with a small limit like 1e-9 gives 1. This is the safe way to ask "is it zero?".

Example 3 — Horizontal Distance of a Slope

A ramp is 10 metres long. The distance it covers along the ground is length * cos(angle).

C++
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>

int main() {
    double length = 10.0;
    const double toRad = std::numbers::pi / 180.0;

    std::cout << std::fixed << std::setprecision(2);
    for (int angle : {0, 30, 60}) {
        double ground = length * std::cos(angle * toRad);
        std::cout << angle << " degrees: covers " << ground << " m\n";
    }
    return 0;
}

How It Works

  1. The cosine is the ground distance divided by the length, so multiplying the cosine by 10 gives the ground distance.
  2. A flat ramp at 0 degrees covers the full 10.00 m. At 30 degrees it covers 8.66 m.
  3. At 60 degrees the ramp is steep and covers only 5.00 m along the ground. Compare this with the height from sin().

Frequently Asked Questions

std::cos(x) returns the cosine of the angle x. The angle must be in radians, not degrees. The result is always between -1 and 1.
The cosine of x as float, double, or long double, a value from -1 to 1. If x is infinity or NaN, the result is NaN.
They are the same wave shifted by a quarter turn. In a right triangle, sin is opposite over hypotenuse and cos is adjacent over hypotenuse. cos(0) is 1, while sin(0) is 0.
A double cannot hold pi exactly, so the angle is slightly off and cos returns about 6.12323e-17. Compare with a small tolerance and do not use ==.
Include <cmath> and write std::cos(). That always works and picks the C++ overloads. The global name ::cos often works too, but the standard does not promise it, and it may only take a double.

Next: cosh()

Keep going through the <cmath> functions. Hyperbolic cosine of a number.

cosh() tutorial →

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.

9 people found this page helpful