C++ cmath sin , sinf and sinl function

The C++ cmath sin,sinf and sinl functions compute the sine of the given value.The declaration of the 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);

The first function ‘float sin(float x)’ is called if the argument is float type.

The second ‘double sin(double x)’ is called, if the argument is double type.

The third function is called if the argument is long double type.

The 4th and 5th function are discussed in detail below.

Link : C++ cmath cos ,cosf ,cosl


Some points to note (these points are applicable to all the five ‘sin’ functions):

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

ii-The second function ‘double sin(double)‘ is the default function. This function is called if the argument type is not scpecifically mentioned.

Include <typeinfo> to call the ‘typeid()’ function. This function checks the type of the argument.

float f=1;
long double ld=2;

cout << "sin(f)=" << sin(f) << "\n" //calls the 1st version
 << "sin(ld)=" << sin(ld) << endl ; //calls the 3rd version

cout<< sin(89) << endl ; 
//calls the deafault sin function (the 2nd version)

cout<< typeid( sin(89) ).name( ) ; 
//checking the return type

Output

sin(f)=0.841471
sin(ld)=0.909297
0.860069
d

The ‘d’ at the last output means long double. Which proves that default version is called.


C++ sinf function

The C++ sinf function is same as the ‘float sin(float)’ function. The character ‘f’ appended to ‘sin’ stands for float, which signify the argument and return type.

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.


Property of sinf

If we pass an argument type other than the float type, say double type to ‘sinf’, then this function will always convert the argument type to the float type first and then compute the sine of the float argument value. The return type is also float type.In other words, this function always constrain the parameter type and the return type to the the float type.

cout<< sin( double(560) )
 << "\n" << sinf( double(560) ); //calls sinf( float )

cout<< "\nreturn type:" << typeid( sinf( double(560)) ).name() ;

Output,

0.714919
0.714919
return type:f



C++ sinl function

The C++ sinl is similar to the 3rd version sin function. The ‘l’ in ‘sinl’ means long double, so it accepts the long double type parameter and also returns long double type value.


Property of sinl

If we pass an argument type other than the long double type, say float type to ‘cosl’, then this function will always convert the argument type to the long double type first, and then compute the cosine of the long double argument value. The return type is also long double type. In other words, this function always constrain the parameter type and the return type to the the long double type.

cout<< sin( float(34) )
 << "\n" << sinl( float(34)) ; //calls sinl(long double)

Making sin compute any value in degree

The sin functions compute the sine of the value which is of radian type. In the mathematical programs, 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.

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.

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

Output,

sin(60°)=0.866025




Leave a Reply

Your email address will not be published. Required fields are marked *