C++ cmath log1p,log1pf and log1pl function


The C++ <cmath> log1p,log1pf and log1pl function compute the base-e logarithm of 1 plus the argument value.The declaration of the functions are given below.

1 float log1p(float x);
2 double log1p(double x);
3 long double log1p(long double x);
4 float log1pf(float x);
5 long double log1pl(long double x);

Parameters:
x – The value whose logarithm is to be computed.

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

Some points to note:

i) log1p(x) is same as loge(x+1).

ii) Domain error occur if the passed value is less than -1.

iii) Range error occur if the argument equals -1.

iv) If ‘NAN’ is passed ‘nan’ is returned,if INFINITY is passed ‘infinity’ is returned.

Code example

/*Passing floating point value*/
cout<< “log1p(34)=” << log1p(34) << endl;

/*Passing -ve<-1 value */
cout<< “log1p(-2)=” << log1p(-2) << endl;

/*Passing -1 */
cout<< “log1p(-1)=” << log1p(-1) << endl;

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

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

Output (base on Code::blocks)

log1p(34)= 3.55535
log1p(-2)= nan
log1p(-1)= -inf
log1p(NAN)= nan
log1p(INFINITY)= inf


 


log1pf and log1pl

*Note

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

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

Code example

#include <typeinfo>

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

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

Output

*log1pf
f
f
*log1pl
e (means ‘long double’)
e (means ‘long double’)