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
Call
What 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
Parameter
Description
x
Required. The angle in radians. It can be float, double, long double, or an integer.
Return Value
Return Value
Description
Sine
A value between -1 and 1 for the angle x.
NaN
Returned when x is infinity or NaN.
-0.0
sin(-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.
deg * std::numbers::pi / 180.0 changes degrees into radians. For 30 degrees the angle is about 0.5236 radians.
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.
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;
}
Output
sin(90) as radians: 0.893997
sin(90) as degrees: 1
How It Works
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.
The second line converts 90 degrees to pi / 2 radians first. Now the sine is 1, the answer you expected.
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.
15 degrees: height 2.59 m
30 degrees: height 5.00 m
45 degrees: height 7.07 m
How It Works
The sine is the height divided by the length of the ramp, so multiplying the sine by 10 gives the height.
toRad is computed once and reused, which keeps the loop short. A 15 degree ramp rises 2.59 m.
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.