English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ log2Function usage and example

C++ Library Function <cmath>

C ++in log2function returns the logarithm of the parameter2logarithm to the base.

The function returns<cmath>defined in the header file.

log2x = log2(x)

log2function prototype [from C ++ 11Standard library begins]

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.

log2parameter

log2function uses the range[0, ∞]is a single required parameter.
If the value is less than zero, then log2function returns NaN (Not a Number).

log2Returns

log2function returns the logarithm of the number with2logarithm to the base.

log2Returns
Parameter (x)Return value
x > 1Positive
x = 1Zero
0 > x > 1Negative
x = 0-∞ (-infinity)
x < 0NaN (Not a Number)

Example1: C2in log2How does the function work?

#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

Example2: with integer type log2function

#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

C++ Library Function <cmath>