Home > Backend Development > C++ > C++ program to calculate the logarithm of a given number based on a given base

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

WBOY
Release: 2023-08-27 08:57:22
forward
1326 people have browsed it

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

In almost all modern programming languages, we can find some logarithmic functions such as natural logarithm, base 2 logarithm, base 10 logarithm, etc. But sometimes we need to calculate logarithms of different bases that are not in the given library functions. To achieve this we can use a simple logarithmic formula. In this article, we will see how to calculate logarithmic value in C using a given number and a given base.

Formula for calculating logarithm given base

Suppose we have taken a number x, its base is k, which has also been given. The formula is as follows: The translation of follow −

is: 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)

Use log10(), where m = 10.

C The cmath library provides the log10() method for finding the base 10 logarithm of a given number. us The same function can be used to calculate the logarithm of a given base k. The syntax used is: An example of log10() is as follows −

grammar

#include < cmath >
Log10( <number> )
Copy after login

algorithm

  • Read two numbers x and k

  • res := (use log10(x) to find the base 10 logarithm of x) / (use log10(k) to find the base 10 logarithm of 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;
}
Copy after login

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
Copy after login
Copy after login
Copy after login

Use log2(), where m = 2.

In the cmath library of C, the log2() method allows users to find the logarithm with base 2 given number. The same function can be used to compute the logarithm of a specified base k The following syntax is used to use log2() −

grammar

#include < cmath >
Log2( <number> )
Copy after login

algorithm

  • Read two numbers x and k

  • res := (use log2( x ) to find the base 2 logarithm of x) / (use log2( k ) to find the base 2 logarithm of x)

  • 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;
}
Copy after login

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
Copy after login
Copy after login
Copy after login

Use log() when m=e.

In the C cmath library, the natural logarithm log() method allows the user to find the logarithm Use base 'e' for the given number. Logarithms can be calculated using a specified base k Same functionality. The following syntax is used to use the log() function −

grammar

#include < cmath >
log( <number> )
Copy after login

algorithm

  • Read two numbers x and k

  • res := (use log(x) to find the logarithm of x with base e) / (use log(k) to find the logarithm of x with base e)

  • 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;
}
Copy after login

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
Copy after login
Copy after login
Copy after login

in conclusion

Using the logarithmic formula of a given base can get the logarithmic result, We use any known base logarithm method on a given number x and divide it by The logarithmic base using a known value as the new base as input. In this article we use Three known existing logarithmic functions are log10(), log2() and log() [natural logarithm] Generates results for the given numbers and their given base.

The above is the detailed content of C++ program to calculate the logarithm of a given number based on a given base. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template