C++ Program Calculate Logarithm
Posted by Samath
Last Updated: January 01, 2021

This is a basic C++ program that use a templete function to calculate log, this is a generic function that will calculate log of a float or integer. Logarithm is the inverse operation to exponentiation. That means the logarithm of a number is the exponent to which another fixed number, the base, must be raised to produce that number. In simple cases the logarithm counts factors in multiplication.

#include <iostream>
#include <cmath> 
using namespace std;
  
  
  template <class base_type, class num_type> double log(base_type base, num_type num);

int main()
{  
    cout << "log base 6 of 3.2 = " << log(6,3.2) << endl;
    cout << "log base 8 of 2 = " << log(8,2) << endl;
    cout << "log base 2.6 of 2.5 = " << log(2.6,2.5) << endl;
    cout << "log base 7 of 5 = " << log(7,5) << endl;
  
  return 0;
}
 
template <class base_type, class num_type> double log(base_type base, num_type num)
{
  return log(num) / log(base); 
}

 

 

Related Content