English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ sin() Function Usage and Example

C++ Library Function <cmath>

C ++returns the sine value of the angle (parameter) represented in radians.

This function is in<cmath>header file definition.

[Mathematics] sin x = sin(x) [C++]

sin() prototype (from C ++ 11standard beginning)

double sin(double x);
float sin(float x);
long double sin(long double x);
double sin(T x); //is of integer type

sin() parameter

sin() function takes a mandatory parameter in radians.

sin() return value

sin() function returns[-1,1]range of values. The returned value is double, float, or long double.

Example1:How sin() works in C ++What is working in the middle?

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
  double x = 0.439203, result;
  
  result = sin(x);
  cout << "sin(x) = " << result << endl;
  
  double xDegrees = 90.0;
  
  //Convert degrees to radians
  x = xDegrees*3.14159/180;
  result = sin(x);
  
  cout << "sin(x) = " << result << endl;
  return 0;
}

The output when running the program is:

sin(x) = 0.425218
sin(x) = 1

Example2: The sin() function with integer type

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
  int x = -1;
  double result;
  result = sin(x);
  cout << "sin(x) = " << result << endl;
  
  return 0;
}

The output when running the program is:

sin(x) = -0.841471

  C++ Library Function <cmath>