X^Y without pow(x,y)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

X^Y without pow(x,y)

753 Views
alexismercier
Contributor III


Hello everybody,

 

Does anyone know to calculate x^y without the pow(x,y) function?

 

Thanks for your answers

 

Alexis.

Labels (1)
6 Replies

537 Views
johnturnur
Contributor III

You need to run

val =x;

for(i =0; i < y ; i++) { val = x * val}

your answer is val

0 Kudos

537 Views
alexismercier
Contributor III

Thanks a lot i'll try this tommorow

Regards

Alexis

Le 6 juin 2013 14:53, "John Turnur" <admin@community.freescale.com> a

écrit :

**

image: Freescale Community<https://community.freescale.com/index.jspa> X^Y without pow(x,y) created by John

Turnur <https://community.freescale.com/people/johnturnur> in *8-bit

Microcontrollers* - View the full discussion<https://community.freescale.com/message/333430#333430>

0 Kudos

537 Views
Monica
Senior Contributor III

Alexis, how did it go?

Did you get to try any of those out?

We'd like to know :smileywink:

Thanks for sharing, regards!

Monica.

0 Kudos

537 Views
alexismercier
Contributor III

hi ,

sorry for the delay .

in my program i want to divide a number into several digits so this method didn't work .

i used this :

valeur = nombre;

  if(i==0)

  {

   

   digit1 = nombre/1000;

  

   nombre=valeur-(digit1*1000);

   i++;

  

  

   }

       if(i==1)

       {

   

   digit2=nombre/100;

  

   //if (Flash_Program(address,carac)){

   //}

   nombre =valeur-(digit1*1000+(digit2*100));

   i++;

  

   }

     if(i==2)

     {

   

   digit3=nombre/10;

  

  

   //if (Flash_Program(address,carac)){

   //}

   nombre=valeur- (digit1*1000+(digit2*100+(digit3*10))) ;

   i++;

  

  

   }  

     if(i==3){

   

   digit4=nombre;

thanks for your help

regards

Alexis MERCIER

537 Views
alexismercier
Contributor III

Hi Monica,

I didn't have enought time to try this function because I've also lot of work on the clock (DS1307 if you have any information abou this you're welcome :smileywink:) .I'll keep you informed as soon as possible

Regards

Alexis

537 Views
bigmac
Specialist III

Hello,

John Turnur wrote:

You need to run

val = x;

for(i =0; i < y ; i++) { val = x * val}

your answer is val

The initialisation for val should be val = 1;
For this simple method, y must be an integer value, and with this implementation, the value of y must be positive and non-zero. To handle y = 0, use a "while" loop, rather than a "for" loop.

val = 1;

while (y) { val *= x; y--; }

If x is an integer value, overflow can easily occur, so checking for this condition might be desireable.  Overflow would be less of a problem if x is a floating point value.  It should also be possible to accurately handle negative values of y.

val = 1.0;

if (y >= 0) {

  while (y) { val *= x; y--; }

}

else {

  while (y) { val /= x; y++; }

}

Regards,

Mac