Results 1 to 10 of 10

Thread: calculating logarithm

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Posts
    30

    Cool

    hi all!

    does anybody out there know how to calc a logarithm with pure c/c++ code? without using any functions etc? inline assembly is also okay!
    thankx!
    Currently using VB6 Enterprise, but VC++ is much better for API ( )!

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    #include <math.h> //This is where the log function is
    #include <iostream.h> //this is for the cout to display the log


    int main(int argc, char* argv[])
    {

    double Number;
    double ItsLog;

    cout << "enter a number" << endl;
    cin >> Number;

    ItsLog = log(number);

    cout "The Natrual Log of " << Number << " = " << ItsLog;

    }

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I don't think that's what he meant. He needs NO FUNCTIONS.

    wolfrog - try looking in an algorithms book, and there may be something elsewhere on the Net.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    My Bad, use the McLauren Approximation.


    Code:
    #define ITERATIONS 1000
    
    //raises a number to a power
    double GetPower(double Number, int Power)
    {
    	int i;
    	double retval = 1;
    
    	//anything raised to the 0th power is 1
    	if (Power == 0)
    	{
    		return 1;
    	}
    
    	
    	//multipy 1 by the number n times
    	for (i=1;i<=Power;i++)
    	{
    		retval *= Number;
    	}
    
    	return retval;
    }
    
    double ln(double number)
    {
    
    	int i;
    	double retval = 0;
    
    	for (i=1;i<=ITERATIONS;i++)
    	{
    		retval = retval +  GetPower(-1,i-1) * GetPower(number-1,i) / (double)i; 
    	//	cout << i << endl;
    	}
    
    	return retval;
    }
    
    
    
    
    
    
    int main(int argc, char* argv[])
    {
    
    double Number; 
    double ItsLog; 
    
    cout << "enter a number" << endl; 
    cin >> Number; 
    
    
    ItsLog = ln(Number);
    cout << "The Natrual Log of " << Number << " = " << ItsLog << endl; 
    
        return 0;
    }
    The higher the value of the ITERATIONS constant the more accurate it will be, the lower the faster it will be, just find the right balance between speed and accuracy.


  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I thought that would come up somewhere, but had a power cut and couldn't finish the code. PS, Sam - do you know what the general equation is for natural logs?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    yeah, it's that.

    Before Euler(I think) twigged that one people just calculated all the powers of e and put them in a big table then read them off backwards to get logarithms.

  7. #7
    Guest
    natural logs can be found in any unhygenic public lavatories!

    rofl!

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Talking LOL

    Just read through my Stats 2 book...and found that McLauren expansion thingie. (It was in among some fish...as it were).
    Yeah, I think it was the Greeks who had 7s.f. tables - must have taken a while to work out .
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    There was me thinking that the greeks didn't use the arabic number system and used roman numberals instead. I'm pretty sure that e wasn't discovered until the 19th century(Euler again, what a guy) So I don't know what the greecs would ohave thought of decimal log tables.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Me confused...

    Okay...the Greeks had cosine tables. I think logs were Napier? (I have no idea...)

    If only the textbooks actually told you something. I don't know what number system the greeks used...but I doubt it was Roman. Weren't they a couple of hundred years before?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width