C++ std::sin() Function

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

Definition

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

The angle must be in radians. A full circle is 2 * pi radians, which is 360 degrees. If you have degrees, convert them first. The function is used for waves, circles, rotation, and the height of a slope.

std::sin() 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::sin. Start with these lines.

C++
double a = std::sin(0.0);                    // 0
double b = std::sin(std::numbers::pi / 2);   // 1 (90 degrees)
float  c = std::sin(0.5f);                   // float version
CallWhat it does
std::sin(0.0)The sine of 0 radians. Returns 0.
std::sin(std::numbers::pi / 6)pi / 6 radians is 30 degrees. Returns about 0.5.
std::sin(30 * std::numbers::pi / 180)The usual way to start from degrees. Also about 0.5.

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       sin(float x);
double      sin(double x);
long double sin(long double x);
double      sin(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
SineA value between -1 and 1 for the angle x.
NaNReturned when x is infinity or NaN.
-0.0sin(-0.0) returns -0.0, keeping the sign of the input.

C++ Examples

Example 1 — Sine of Common Angles

Convert each angle from degrees to radians, then print the sine 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, 30, 45, 90}) {
        double rad = deg * std::numbers::pi / 180.0;
        std::cout << "sin(" << deg << " deg) = " << std::sin(rad) << '\n';
    }
    return 0;
}

How It Works

  1. deg * std::numbers::pi / 180.0 changes degrees into radians. For 30 degrees the angle is about 0.5236 radians.
  2. std::sin() then gives 0.5000 for 30 degrees and 0.7071 for 45 degrees. These are the well-known values 1/2 and sqrt(2)/2.
  3. sin(0) is 0.0000 and sin(90) is 1.0000, the smallest and the largest value of a sine near the first quarter turn. std::fixed hides tiny rounding errors.

Example 2 — The Most Common Mistake: Degrees vs Radians

Compare passing 90 straight in with passing 90 degrees after conversion.

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

int main() {
    double wrong = std::sin(90.0);
    double right = std::sin(90.0 * std::numbers::pi / 180.0);

    std::cout << "sin(90) as radians:  " << wrong << '\n';
    std::cout << "sin(90) as degrees:  " << right << '\n';
    return 0;
}

How It Works

  1. std::sin(90.0) treats 90 as an angle in radians. That is about 14.3 full turns, so the sine is the odd-looking 0.893997.
  2. The second line converts 90 degrees to pi / 2 radians first. Now the sine is 1, the answer you expected.
  3. The compiler cannot warn you about the wrong version, because both are valid numbers. Make a habit of naming variables rad and deg.

Example 3 — Height of a Ramp

A ramp is 10 metres long. Its height is length * sin(angle). Find the height for three slopes.

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 : {15, 30, 45}) {
        double height = length * std::sin(angle * toRad);
        std::cout << angle << " degrees: height " << height << " m\n";
    }
    return 0;
}

How It Works

  1. The sine is the height divided by the length of the ramp, so multiplying the sine by 10 gives the height.
  2. toRad is computed once and reused, which keeps the loop short. A 15 degree ramp rises 2.59 m.
  3. The steeper the angle, the higher the ramp. At 30 degrees the height is 5.00 m, half the length, and at 45 degrees it is 7.07 m.

Frequently Asked Questions

std::sin(x) returns the sine of the angle x. The angle must be in radians, not degrees. The result is always between -1 and 1.
The sine of x as float, double, or long double, a value from -1 to 1. If x is infinity or NaN, the result is NaN.
Radians. To use degrees, multiply by std::numbers::pi / 180 first. A full circle is 2 * pi radians, or 360 degrees.
A double cannot store pi exactly, so sin(std::numbers::pi) is about 1.22465e-16, a tiny number. Print with std::fixed and a small setprecision, or compare with a tolerance such as std::abs(x) < 1e-9.
Include <cmath> and write std::sin(). That always works and picks the C++ overloads. The global name ::sin often works too, but the standard does not promise it, and it may only take a double.

Next: sinh()

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

sinh() 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.

7 people found this page helpful