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:
// 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;} |
Output:
logb(-10) = log(|-10|) = 3 logb(10) = log(|10|) = 3
Program2:
// 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;} |
Output:
logb(70.56) = log(|70.56|) = 6 logb(17.6) = log(|17.6|) = 4
Program3:
// 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;} |
Output:
logb(0) = log(|0|) = -inf
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


