C++ cmath log,logf and logl function

The C++ <cmath> log,logf and logl function compute the base-e logarithm of the parameter.The declaration of the functions are given below.

1 float log(float x);
2 double log(double x);
3 long double log(long double x);
4 float logf(float x);
5 long double logl(long double x);

x -The floating point value for which the logarithm is to be computed.

Return type
floating point -The base-e logarithm of ‘x’.

Some points to note:

i) log(x) is same as loge(x).

ii) A domain error occur if the argument is negative.MinGW returns ‘nan’ ,VS returns ‘-nan’.

iii) A range error may occur if the argument is 0.MinGW returns ‘-inf’ ,VS returns ‘-inf’.

iii) A NAN is passed ‘nan’ is returned,if INFINITY is passed ‘inf’ is returned.

Code example:The output is base on Code::blocks(MinGW compiler).

/*Passing floating point value*/
cout<< “log(3)=” << log(3) << endl;

/*Passing -ve value */
cout<< “log(-89)=” << log(-89) << endl;

/*Passing 0 */
cout<< “log(0)=” << log(0) << endl;

/*Passing NAN */
cout<< “log(NAN)=” << log(NAN) << endl;

/*Passing INFINITY */
cout<< “log(INFINITY)=” << log(INFINITY) << endl;


log(3)= 1.09861
log(-89)= nan
log(0)= -inf
log(NAN)= nan
log(INFINITY)= inf


logf and logl


i)The 4th version logf is same as the 1st version float log(float).The ‘f‘ character stands for ‘float’ which signify the argument and return type of the function.

ii)The 5th version logl is same as the 3rd version long double log(long double).The ‘l‘ character stands for ‘long double’ which signify the argument and return type of the function.

Code example

#include <typeinfo>

/*logf */
cout<< “*logf\n”;
float f=2;
cout<< typeid( logf(2) ).name() << endl /*identify type of logf retunred value */
<< typeid( log(f) ).name() << endl; /*identify type of log(float) returned value */

cout<< “*logl\n”;
long double ld=2;
cout<< typeid( logl(2) ).name() << endl /*indentify type of logl returned value */
<< typeid( log(ld) ).name() << endl; /*identify type of log(long double) returned value*/


e (means ‘long double’)
e (means ‘long double’)