Code: Select all

`int lollog(long i)`

{

/* returns log base 10 of i */

long ans = 0;

int n;

for (n = 0; n < 10000; n++)

{

ans += (1 / (2 * n + 1)) * lolpow((double)(i - 1) / (double)(i + 1), 2 * n + 1);

}

printf("ans: %d\n", ans);

return ans/1.15129255; // ans / (.5*ln(10))

}

Now, for some horrible reason this is returning 0 every time. You see that I'm printing out ans before dividing it by that number (ln(10)/2), but ans is 0 at that point according to the printout.

Now, when I remove the part where it's multiplied by lolpow() ans is not 0, which leads me to believe that lolpow() is returning zero. This is lolpow():

Code: Select all

`long lolpow(double i, long w)`

{

/* find i^w ... only works for natural numbers*/

long n = 0, result = 1;

for (n = 0; n < w; n++)

{

result *= i;

}

return result;

}

Now I tested lolpow with the arguments 2 and 3 and it returned 8 as expected, so that works. I think I might have some kind of casting error somewhere, but I'm not very good at finding those. Does anything jump out at anybody?

And yes, there is a good reason that I'm rewriting all of these functions. I'm going to modify lollog() a little bit later to suit my problem (sacrificing accuracy for efficiency) and math.h didn't want to link for me so I decided to quickly rewrite the power function to suit my needs.

So, does anybody see an obvious problem with this? Thanks.

EDIT: Oh and if anybody is interested my logarithm function is based on this tidbit from Wikipedia: