English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++in log2function returns the logarithm of the parameter2logarithm to the base.
The function returns<cmath>defined in the header file.
log2x = log2(x)
double log2(double x); float log2(float x); long double log2(long double x); double log2(T x); //for integer
LOG2function has only one parameter and returns a value of type double, float, or long double.
log2function uses the range[0, ∞]is a single required parameter.
If the value is less than zero, then log2function returns NaN (Not a Number).
log2function returns the logarithm of the number with2logarithm to the base.
Parameter (x) | Return value |
---|---|
x > 1 | Positive |
x = 1 | Zero |
0 > x > 1 | Negative |
x = 0 | -∞ (-infinity) |
x < 0 | NaN (Not a Number) |
#include <iostream> #include <cmath> using namespace std; int main () { double x = 13.056, result; result = log2(x); cout << "log2(x) = " << result << endl; x = -3.591; result = log2(x); cout << "log2(x) = " << result << endl; return 0; }
The output when running the program is:
log2(x) = 3.70664 log2(x) = nan
#include <iostream> #include <cmath> using namespace std; int main () { double result; int x = 2201; result = log2(x); cout << "log2(" << x << ") = " << result << endl; return 0; }
The output when running the program is:
log2(2201) = 11.1039