log2() function in C++ with examples

Hey, guys today we are going to learn about log2() function in C++. It is a mathematical function which returns the value of logarithmic base 2 value of the argument passed. Header file for log2() function in C++ is <math.h> also known as <cmath>.Its syntax is :

log2(x)

log2() function accepts argument (x) value in the range of Zero to infinity  i.e Its domain is [0,∞]. log2() returns nan which means ‘Not a Number’ when an argument with a negative value is passed to the function. log2() function has return types ‘float’, ‘double’ and ‘long double’ it returns the logarithmic value as ‘float’ , ‘double’ and ‘long double’  as shown:

float y = log2(x)//x is of float datatype
double y = log2(x)//x is of double datatype
long double y = log2(x)//x is of long double datatype

Please note that ‘double’ datatype is used to return the logarithmic base 2 value of the integer values  i.e when the argument (x) is of integer datatype
as shown:

double y = log2(x)//x is of integer datatype

log2() function return the base 2 logarithmic values as:
If x<0, log2() will return nan i.e(Not a Number).
x=0, log2() will return negative infinity i.e(-∞).
If 0<x<1, log2() will return negative value.
If x=1, log2() will return Zero i.e(0).
If x>1, log2() will return positive value.

 Using log2() function in C++ by passing the argument(x) of different datatypes

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int x=64;
    float x1=15.092;
    double x2=90.1567834;
    long double x3=121.1309151313;
    cout<<"value of log base 2(x)="<<log2(x)<<endl;
    cout<<"value of log base 2(x1)="<<log2(x1)<<endl;
    cout<<"value of log base 2(x2)="<<log2(x2)<<endl;
    cout<<"value of log base 2(x3)="<<log2(x3)<<endl;
    return 0;
}

Output:

value of log base 2(x)=6                                                                                                                             

value of log base 2(x1)=3.91571                                                                                                                      

value of log base 2(x2)=6.49436                                                                                                                      

value of log base 2(x3)=6.92042

Using log2() to show different output results depending on the value of argument passes (x)

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    float x=-15;
    float x1=0;
    float x2=0.135;
    float x3=1;
    float x4=9.15;
    cout<<"value of log base 2(-15)="<<log2(x)<<endl;
    cout<<"value of log base 2(0)="<<log2(x1)<<endl;
    cout<<"value of log base 2(0.135)="<<log2(x2)<<endl;
    cout<<"value of log base 2(1)="<<log2(x3)<<endl;
    cout<<"value of log base 2(9.15)="<<log2(x4)<<endl;
    return 0;
}

Output:

value of log base 2(-15)=nan                                                                                                                         

value of log base 2(0)=-inf                                                                                                                          

value of log base 2(0.135)=-2.88897                                                                                                                  

value of log base 2(1)=0                                                                                                                             

value of log base 2(9.15)=3.19377

Other logarithmic functions:

  • log(): It returns the natural logarithmic of the argument passed.
  • log10(): It returns the logarithmic base 10 of the argument passed.

Also, refer

 

Leave a Reply