We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,307 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Find the logrithm of a real number

0
By Sandun Dhammika Perera on May 6th, 2009 10:08 pm

This is not the fastest way and not the one that used inside the
calculators and even inside the C++ math library. But this is
just another way to find logrithm of base 10.

/*
 * Finds a logirhm of a give input real
 * 
 *
 */
#include <iostream>
using std::cout ;
using std::endl;

double FindLogirthm ( double x )
{
  int n =0 ;
  int array [100];
  while ( x >= 10 )
    {
      x = x /10;
      n++;
    }
  

  for ( int k = 0 ; k < 100 ; k++)
    {
      // if ( x == 1 )
      //	break;
      if ( x*x  >= 10)
	{
	  array[k] = 1;
	  x = x*x /10 ;
	}
      else
	{
	  array[k]=0;
	  x = x*x;
	}
    }
  
  // conver that n value and array of binary places to
  // double.
  double output = n;
  double factor = 0.5;
  for ( int k = 0  ; k < 100 ; k++, factor/= 2 )
    {
      if ( array[k]== 1)
	{
	  output += factor;
	}
    }
  return output ;

} 


int main( )
{
  cout << " Logirithm of 254.23 is " << FindLogirthm ( 254.23) << endl;
  return 0;
}

Very nice!

ddanbe
Industrious Poster
4,375 posts since Oct 2008
Reputation Points: 2,126
Solved Threads: 738
Skill Endorsements: 26

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0766 seconds using 2.8MB