|
-
Sep 28th, 2000, 04:30 AM
#1
Thread Starter
Junior Member
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 (  )!
-
Sep 30th, 2000, 08:52 AM
#2
Frenzied Member
#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;
}
-
Sep 30th, 2000, 09:08 AM
#3
Monday Morning Lunatic
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
-
Sep 30th, 2000, 10:04 AM
#4
Frenzied Member
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.
-
Sep 30th, 2000, 10:30 AM
#5
Monday Morning Lunatic
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
-
Sep 30th, 2000, 02:23 PM
#6
Frenzied Member
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
#7
natural logs can be found in any unhygenic public lavatories!
rofl!
-
Sep 30th, 2000, 04:56 PM
#8
Monday Morning Lunatic
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
-
Sep 30th, 2000, 06:35 PM
#9
Frenzied Member
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.
-
Oct 1st, 2000, 04:21 AM
#10
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|