Hello everybody,

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

Thanks for your answers

Alexis.

Hello everybody,

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

Thanks for your answers

Alexis.

- 1 person found this helpful
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

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>

Alexis, how did it go?

Did you get to try any of those out?

We'd like to know

Thanks for sharing, regards!

Monica.

- 1 person found this helpful
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

You need to run

val =x;

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

your answer is val