Re: Integer power function and multidimensiona l arrays as functionparamet ers
Ivan Vecerina wrote:[color=blue]
> "Martin" <lillierom@cti. ecp.fr> wrote in message
> news:dtvclg$l2d $1@smilodon.ecp .fr...
> : Tomás wrote:
> : > Martin posted:
> : >
> : >
> : >>I want to raise a long integer to another long integer and have the
> : >>result as a long integer.
> : >
> : >
> : > Why not just write a function. Potentially buggy code:
> : >
> : >
> : > /*************** ************
> : > Undefined Behaviour if
> : > either argument is zero
> : > *************** ************/
> : >
> : > unsigned long RaisePowerInteg er(unsigned long a, unsigned long b)
> : > {
> : > unsigned long value = a;
> : >
> : > for ( unsigned long i = 0; i < b; ++i )
> : > {
> : > value *= a;
> : > }
> : >
> : > return value;
> : > }
> :
> : Good idea, thanks!
>
> Well, only if you can afford making a lot of multiplications
> ( the above has linear complexity w.r.t. b ).
> Logarithmic complexity can be achieved with little more
> effort:
>
> unsigned long RaisePowerInteg er(unsigned long a, unsigned long b)
> {
> unsigned long result = 1;
> for( ; b > 0 ; b>>=1 )
> {
> if(b&1) result *= a;
> a *= a;
> }
> return result;
> }
>
> Further optimization is possible, but that's an easy start.
>
>
> I hope this helps,
> Ivan[/color]
Great, thanks!
Martin
Ivan Vecerina wrote:[color=blue]
> "Martin" <lillierom@cti. ecp.fr> wrote in message
> news:dtvclg$l2d $1@smilodon.ecp .fr...
> : Tomás wrote:
> : > Martin posted:
> : >
> : >
> : >>I want to raise a long integer to another long integer and have the
> : >>result as a long integer.
> : >
> : >
> : > Why not just write a function. Potentially buggy code:
> : >
> : >
> : > /*************** ************
> : > Undefined Behaviour if
> : > either argument is zero
> : > *************** ************/
> : >
> : > unsigned long RaisePowerInteg er(unsigned long a, unsigned long b)
> : > {
> : > unsigned long value = a;
> : >
> : > for ( unsigned long i = 0; i < b; ++i )
> : > {
> : > value *= a;
> : > }
> : >
> : > return value;
> : > }
> :
> : Good idea, thanks!
>
> Well, only if you can afford making a lot of multiplications
> ( the above has linear complexity w.r.t. b ).
> Logarithmic complexity can be achieved with little more
> effort:
>
> unsigned long RaisePowerInteg er(unsigned long a, unsigned long b)
> {
> unsigned long result = 1;
> for( ; b > 0 ; b>>=1 )
> {
> if(b&1) result *= a;
> a *= a;
> }
> return result;
> }
>
> Further optimization is possible, but that's an easy start.
>
>
> I hope this helps,
> Ivan[/color]
Great, thanks!
Martin
Comment