C++ cmath logb , logbf and logbl


The C++ <cmath> logb,logbf and logbl functions extract the exponent of the argument as a signed integer value in floating point format.

1 float logb(float x);
2 double logb(double x);
3 long double logb(long double x);
4 float logbf(float x);
5 long double logbl(long double x);

Some points to note:

i) The value returned by logb(x) is the value that is raise to the power when ‘x’ is expressed in power form.For instance,logb(90) the value 90 can be written as ‘26 + 16′,the value 6 is raise as the power so it is the returned value.(Note this interpretation of logb is only true for valid values not equal to 0 or INFINITY or NAN).

ii) A domain error or range error may occur if the argument is 0.

iii) If very large or very small value is passed ±∞ is returned.

iv) If ±∞ is passed,∞ is returned.

v) If NaN is passed NaN is returned.

vi) If integer is passed as the parameter the double version is called.

Code example

cout<< “logb(23.67)=” << logb(23.67) << endl ;

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

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

Output in code::blocks

logb(23.67)= 4
logb(INFINITY)= inf
logb(NAN)= nan



logbf and logbl

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

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

Code example

float f=128.90 ;
cout<< logbf( 128.90 ) << endl ///the first version is called
<< logb( f ) << endl ; //Same as above

long double ld=600.657 ;

cout<< logbl( 600.657 ) << endl ///the third version is called
<< logb( ld ) << endl ; // same as above

Output

7
7
9
9


*Side Note

A fact about logb function

  logb(±0) returns −∞ and raises the ‘divide-by-zero’ floating-point exception.