Dear Eike

I think I have fixed the bugs in my code. Remember is c-like and not c (I used 
= instead of ==)
Since I am not a pro programmer I would like to explain to you the logic:

double pow (double number,int exponent)
{
int i=0;
double result=0;
// if the number is zero the result will be zero
if (number==0) return 0; // the bug you were talking about. remember that the 
code is c-like not c. i replaced = with ==
// if the number is not zero and the exponent is zero the result will be 1
if (exponent==0) return 1; // another bug

// while the exponent is greater than zero
result=1;
while (exponent)
{
// if exponent modulo 2 is not zero (1) then multiply the result by the number
if (exponent%2) result=result*number;
// set number to it's square
number=number*number;
// divide the exponent with 2
exponent=exponent/2;
}
return result;
}
I will give you a sample of the code inside the loop number=2 and exponent=50 
for the loop
0. result=1
1. 1000%2=0, Result=1, Number = Number^2
2. 500%2=0, Result=1, Number = Number^4 
3. 250%2=0, Result=1, Number = Number^8 
4. 125%2=1, Result=Number^8, Number = Number^16 
5. 62%2=0, Result=Number^8, Number = Number^32
6. 31%2=1, Result=Number^40, Number = Number^64 
7. 15%2=1, Result=Number^104, Number = Number^128 
8. 7%2=1, Result=Number^232, Number = Number^256 
9. 3%2=1, Result=Number^488, Number = Number^512 
10. 1%2=0, Result=Number^1000, Number = Number^1024 

As you can see it need's to run the loop 10 times instead of 1000 and as the 
exponent get bigger it seems that it will  have less overhead.It's logic 
support's only integer exponent's.

Thanks for your time,

Nikos


----- Original Message ----
From: Eike Rathke <[EMAIL PROTECTED]>
To: dev@openoffice.org
Cc: Nikos Trivlis <[EMAIL PROTECTED]>
Sent: Tuesday, October 16, 2007 2:49:08 PM
Subject: Re: [dev] Sample code


Hi Nikos,

On Sunday, 2007-10-14 12:04:10 -0700, Nikos Trivlis wrote:

> I have a sample c-like code about how to improve the POW (power
> function) and you could maybe find it useful for calc.

I don't see in what aspect that would improve the POWER code. Au
contraire, the exponent is only integer and doesn't handle floating
point, the loop would introduce a runtime penalty for large exponent
values, but the code is buggy and the loop is never executed. If it was
it wouldn't work either. As is, the function returns 0 for every
combination of arguments.

  Eike

P.S.: As you're not subscribed to the mailing list you were posting to,
you will miss replies that are directed to the list only. When
 answering,
please reply only to the list (Reply-To header is set), not to my
personal account. Thanks.

-- 
 OOo/SO Calc core developer. Number formatter stricken i18n
 transpositionizer.
 SunSign   0x87F8D412 : 2F58 5236 DB02 F335 8304  7D6C 65C9 F9B5 87F8
 D412
 OpenOffice.org Engineering at Sun: http://blogs.sun.com/GullFOSS
 Please don't send personal mail to this [EMAIL PROTECTED] account, which I
 use for
 mailing lists only and don't read from outside Sun. Use [EMAIL PROTECTED]
 Thanks.






       
____________________________________________________________________________________
Moody friends. Drama queens. Your life? Nope! - their life, your story. Play 
Sims Stories at Yahoo! Games.
http://sims.yahoo.com/  

Reply via email to