C programming math.h acos , acosf and acosl


In C programming the <math.h> acos ,acosf and acosl function compute the arc cosine of the argument.The declaration of the function is given below.

1 float acos(float x);
2 double acos(double x);
3 long double acos(long double x);
4 float acosf(float x);
5 long double acosl(long double x)

Parameters:
x -A floating point value.

Return type
floating point value -The arc cosine of the argument passed.

All the functions are the same,they compute the arc cosine of the argument.The 1st , 2nd and the 3rd function have the same name i.e. ‘acos’.The 4th and 5th function have different names.What does having a different names means is explained in detail below.

Some points to note:

i-The argument ‘x’ should be in the interval [-1,1].If the argument is not in the said interval a domain error occurs and so ‘nan‘ is returned.

ii-The returned value is in the interval [0 , π] radians.

iii-The second version is the default version that is called when acos is called in our program.

iv-When the argument passed is int type the default version is called i.e the 2nd function is called.

Note arc cosine means inverse of cos,so acos(x) is same as cos^-1(x) .

Link: C++ nan

Code example

double d1 = 0.12, d2 = 12;

float f1 = 0.345;

long double ld = 0.555 ;

printf(“%lf”, acos( d1 ) ) ; //calls the 2nd version
printf(“\n%f”, acos( f1 ) ) ; //calls the 1st version
printf(“\n%Lf”, acos(ld) ) ; //calls the 3rd version

printf(“\n%lf”, acos(d2) ) ;
printf(“\n%lf”, acos(INFINITY) ) ;

Output in Visual Studio,

1.450506
1.218558
0.982433
-nan(ind)
-nan(ind)

The ‘-nan(ind)’ means the computed value is not representable or it simply stands for NaN.






acosf

The ‘acosf‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘acosf‘ stands for float and it signify the parameter and return type of the function.

Code example

float f = 0.77 ;

printf(“%f”, acos( f ) ) ; //Calls the 1st version
printf(“\n%f”, acosf( 0.77 ) ) ; //same as above

Output,

0.691955
0.691955


acosl

The ‘acosl‘ function is same as the 3rd function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘acosl‘ stands for long double and it signify the parameter and return type of the function.

Code example

long double ld = 0.671 ;

printf(“%Lf”, acos( ld ) ) ; //Call the 3rd version
printf(“\n%Lf”, acosl( 0.671 ) ) ; //same as above

Output,

0.835240
0.835240


 


Getting degree instead of radian with acos

The C acos,acosf and acosl return the value in radian,to get the value as degree apply the simply operation given below.

Now,

1 radian = (180/3.141592) degree

So multiplying the output value with (180/3.141592) will change the value to degree type.

Code example

printf(“acosl(0.345)=%Lf%c”, acosl( 0.345 ) , (int)248 );

Output

acosl(0.345)=69.818219°

The character of the integer value 248 is ‘°’-the degree sign.