C++ Program to calculate the logarithm of a given number based on a given base


In almost any modern programming languages, we find the few logarithm functions like natural logs, log with base 2, log with base 10 etc. But sometimes we need to calculate logarithm with different base which are not available in the given library functions. To achieve this goal we can use simple logarithmic formula. In this article we will see how to calculate logarithm values with a given number and a given base in C++.

Formula to calculate logarithm with given base

Let we have taken a number x, and its base is k which is also given. The formula is like follow −

$$\mathrm{log_{k}\left ( x \right )=\frac{log_{m}\left ( x \right )}{log_{m}\left ( k \right )}}$$

Where m is any known (available base)

Using log10() where m = 10.

C++ cmath library provides log10() method to find log with base 10 for a given number. We can use this same function to calculate the log with given base k. The syntax for using log10() is shown below −

Syntax

#include < cmath >
Log10( <number> )

Algorithm

  • read two numbers x and k

  • res := (find log x for base 10 using log10( x )) / (find log x for base 10 using log10( k ))

  • return res

Example

#include <iostream>
#include <cmath>
using namespace std;
float solve( int x, int k){
   float answer;
   answer = log10( x ) / log10( k );
   return answer;
}
int main(){
   cout << "Log base 8 for input x = 512 is: " << solve( 512, 8 ) <<
       endl;
   cout << "Log base 9 for input x = 59049 is: " << solve( 59049, 9 )
       << endl;
   cout << "Log base 2 for input x = 1024 is: " << solve( 1024, 2 ) <<
       endl;
   cout << "Log base 4 for input x = 256 is: " << solve( 256, 4 ) <<
       endl;
}

Output

Log base 8 for input x = 512 is: 3
Log base 9 for input x = 59049 is: 5
Log base 2 for input x = 1024 is: 10
Log base 4 for input x = 256 is: 4

Using log2() where m = 2.

The log2() method in the C++ cmath library allows users to find the log with base 2 of a given number. The log with a specified base k can be calculated using the same function. The following syntax is for using log2() −

Syntax

#include < cmath >
Log2( <number> )

Algorithm

  • read two numbers x and k

  • res := (find log x for base 2 using log2( x )) / (find log x for base 2 using log2( k ))

  • return res

Example

#include <iostream>
#include <cmath>
using namespace std;
float solve( int x, int k){
   float answer;
   answer = log2( x ) / log2( k );
   return answer;
}
int main(){
   cout << "Log base 8 for input x = 512 is: " << solve( 512, 8 ) <<
       endl;
   cout << "Log base 9 for input x = 59049 is: " << solve( 59049, 9 )
       << endl;
   cout << "Log base 2 for input x = 1024 is: " << solve( 1024, 2 ) <<
       endl;
   cout << "Log base 4 for input x = 256 is: " << solve( 256, 4 ) <<
       endl;
}

Output

Log base 8 for input x = 512 is: 3
Log base 9 for input x = 59049 is: 5
Log base 2 for input x = 1024 is: 10
Log base 4 for input x = 256 is: 4

Using log() where m = e.

The natural logarithm log () method in the C++ cmath library allows users to find the log with base ‘e’ of a given number. The log with a specified base k can be calculated using the same function. The following syntax is for using log() −

Syntax

#include < cmath >
log( <number> )

Algorithm

  • read two numbers x and k

  • res := (find log x for base e using log( x )) / (find log x for base e using log( k ))

  • return res

Example

#include <iostream>
#include <cmath>
using namespace std;
float solve( int x, int k){
   float answer;
   answer = log( x ) / log( k );
   return answer;
}
int main(){
   cout << "Log base 8 for input x = 512 is: " << solve( 512, 8 ) <<
       endl;
   cout << "Log base 9 for input x = 59049 is: " << solve( 59049, 9 )
       << endl;
   cout << "Log base 2 for input x = 1024 is: " << solve( 1024, 2 ) <<
       endl;
   cout << "Log base 4 for input x = 256 is: " << solve( 256, 4 ) <<
       endl;
}

Output

Log base 8 for input x = 512 is: 3
Log base 9 for input x = 59049 is: 5
Log base 2 for input x = 1024 is: 10
Log base 4 for input x = 256 is: 4

Conclusion

To get the results of a logarithm with given base, simple logarithm formula can be used, where we use any known base logarithm method for the given number x, and divide this with the log-base that known value for that new base as input. In this article we have used three existing known log functions which are log10(), log2() and log() [natural log] to generate the results for given number and their given bases.

Updated on: 07-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements