C++ cmath sin , sinf and sinl function


The C++ cmath sin,sinf,sinl function computes the sine of the given value.The declaration of teh functions are given below:

1 float sin(float x);
2 double sin(double x);
3 long double sin(long double x);
4 float sinf(float x);
5 long double sinl(long double);

Some points to note

i-The argument ‘x‘ is measured in radians.

ii-The second function double sin(double) is the default function called when sine is computed in our program.

Code example

#include <typeinfo>

cout<< sin(89) << endl ;

cout<< typeid( sin(89) ).name( ) ; //checking the type of the computed value

Output

0.860069
d (means ‘double’)



sinf and sinl

sinf function

The sinf function is same as the 1st version sin function-float sin(float),the parameter and argument is float type.The character ‘f’ appended to sin stands for float,this in turn means a sin function with float as parameter and return type.

Code example

float f=97;

cout<< sin(f) << endl ///calls the 1st version
<< sinf(9) ;

cout<< endl ;

cout<< typeid( sin(f) ).name() << endl
<< typeid( sinf(97) ).name() ;

Output

0.379607
0.379607
f
f

The computed values are the same so are the return type.

sinl function

The sinl is similar to the 3rd version sin function.The 5th version has the parameter and return type as long double.The ‘l’ in sinl means long double so it accept long double parameter and also returns long double type value.





Making sin compute any value in degree

The sin function compute the sine of the value which is of radian type.In mathematical program you may want to compute the sine of some values which is given in degree.In such case we can convert the degree to radian type by applying a simple conversion rule and compute the sine of the degree.

The relation between degree and radian is given below,

1 degree =(π/180) radian

Multiplying the argument-which is in degree- with (π/180) will yield the corresponding radian value.Computing the sin of this value will yield the sine of the given degree.

Code example

double degree=60;

cout<< “sin(60°)=” << sin( (degree*3.141592)/180 );

Output

sin(60°)=0.866025