PDA

Click to See Complete Forum and Search --> : calculating logarithm


wolfrog
Sep 28th, 2000, 04:30 AM
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!

Sam Finch
Sep 30th, 2000, 08:52 AM
#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;

}

parksie
Sep 30th, 2000, 09:08 AM
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.

Sam Finch
Sep 30th, 2000, 10:04 AM
My Bad, use the McLauren Approximation.




#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.

parksie
Sep 30th, 2000, 10:30 AM
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?

Sam Finch
Sep 30th, 2000, 02:23 PM
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.

Sep 30th, 2000, 02:42 PM
natural logs can be found in any unhygenic public lavatories!

rofl!

parksie
Sep 30th, 2000, 04:56 PM
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 ;).

Sam Finch
Sep 30th, 2000, 06:35 PM
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.

parksie
Oct 1st, 2000, 04:21 AM
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?