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.
Time Complexity: O(1)
Space complexity: O(1)
Program 1:
CPP
#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:
CPP
#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:
CPP
#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
Last Updated :
14 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...