logb() function in C++ STL

The logb() is a builtin function in C++ STL which returns the logarithm of |x|, using FLT_RADIX as base for the logarithm. In general, the value of FLT_RADIX is 2, so logb() is equivalent to log2()(for positive values only).

Syntax:

logb(val)

Parameter: The function accepts a single mandatory parameter val which specifies the val whose logb() is to be calculated. The data-type can be of double, float, long double or int.



Return Type: The function returns the logarithm of |x|.

Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// to implement logb() function
// when data-type is integer
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
    double result;
    int x = -10;
  
    result = logb(x);
    cout << "logb(" << x << ") = "
         << "log(|" << x << "|) = " << result << endl;
  
    x = 10;
  
    result = logb(x);
    cout << "logb(" << x << ") = "
         << "log(|" << x << "|) = " << result << endl;
  
    return 0;
}

chevron_right


Output:

logb(-10) = log(|-10|) = 3
logb(10) = log(|10|) = 3

Program2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// to implement logb() function
// when data-type is double
#include <cmath>
#include <iostream>
  
using namespace std;
  
int main()
{
    double x = 70.56, result;
  
    result = logb(x);
    cout << "logb(" << x << ") = "
         << "log(|" << x << "|) = " << result << endl;
  
    x = 17.6;
  
    result = logb(x);
    cout << "logb(" << x << ") = "
         << "log(|" << x << "|) = " << result << endl;
  
    return 0;
}

chevron_right


Output:

logb(70.56) = log(|70.56|) = 6
logb(17.6) = log(|17.6|) = 4

Program3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// to implement logb() function
// when input is 0
#include <cmath>
#include <iostream>
  
using namespace std;
  
int main()
{
    double result;
    int x = 0;
  
    result = logb(x);
    cout << "logb(" << x << ") = "
         << "log(|" << x << "|) = " << result << endl;
  
    return 0;
}

chevron_right


Output:

logb(0) = log(|0|) = -inf


My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.