TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ Math log2() Function

C++ Math log2() Function with tutorial for beginners and professionals with examples on isgreaterequal(), less(), islessequal(), islessgreater(), isunordered(), exp(), frexp(), ldexp(), log(), log10(), modf(), exp2(), expm1(), log1p(), log2(), logb(), scalbn(), scalbln() etc.

<< Back to CPP

C++ Math log2()

The function computes the base 2 logarithm of a given number.

Suppose a number is 'x':

log2(x) = log2x;

Syntax

float log2(float x);
double log2(double x);
long double log2(long double x);
double log2(integral x);

Note: The return_type can be float, double or long double.

Parameter

x: The value whose logarithm is to be calculated.

Return value

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

Example 1

Let's see the simple example when the value of x is greater than one.

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x=2;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log2(x) = "<<log2(x);
     return 0;}

Output:

Value of x is : 2
log2(x) = 1

In this example, log2() function computes the logarithm value of base 2 when the value of x is greater than one

Example 2

Let's see the simple example when the value of x is equal to 1.

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x=1;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log2(x) = "<<log2(x);
     return 0;
}.

Output:

Value of x is : 1
log2(x) = 0

In this example, log2() function computes the logarithm value of base 2 when the value of x is equal to one.

Example 3

Let's see the simple example when the value of x lies between 0 and 1.

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     float x=0.2;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log2(x) = "<<log2(x);
     return 0;
}

Output:

Value of x is : 0.2
log2(x) = -2.32193

In this example, log2() function computes the logarithm value of base 2 when the value of x is equal to 0.2.

Example 4

Let's see the simple example when the value of x is equal to zero.

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x=0;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log2(x) = "<<log2(x);
     return 0;
}

Output:

Value of x is : 0
log2(x) = -inf

In this example, log2() function computes the logarithm value of base 2 when the value of x is equal to 0.

Example 5

Let's see the simple example when the value of x is less than zero.

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     float x= -1.50;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log2(x) = "<<log2(x);
     return 0;
}

Output:

Value of x is : -1.5
log2(x) = nan

In this example, log2() function computes the logarithm value of base 2 when the value of x is less than zero.


Next TopicC++ Math Functions




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf