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!
Printable View
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!
#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;
}
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.
My Bad, use the McLauren Approximation.
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.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;
}
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?
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.
natural logs can be found in any unhygenic public lavatories!
rofl!
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 ;).
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.
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?