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; } |
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; } |
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; } |
logb(0) = log(|0|) = -inf
Recommended Posts:
- Function Overloading vs Function Overriding in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- exp() function C++
- log() function in C++
- div() function in C++
- fma() function in C++
- wmemcpy() function in C/C++
- ios bad() function in C++ with Examples
- ios eof() function in C++ with Examples
- valarray tan() function in C++
- valarray abs() function in C++
- valarray end() function in C++
- map rbegin() function in C++ STL
- valarray sin() function in C++
- valarray cos() function in C++
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.