|
|
View previous topic :: View next topic |
Author |
Message |
pcmgogo
Joined: 25 Dec 2004 Posts: 9
|
Mutiplication error in for loop with 16bit variable |
Posted: Wed Jan 14, 2009 10:34 am |
|
|
I can't understand my problem but I could not fix it.
I'm using below function for calculating a value,
gecici is unsigned 16bit variable and it defined with
Code: |
unsigned int16 gecici;
int8 i;
for(i=0;i<5;i++)
{
gecici=i*100;
#use delay (clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_D5,rcv=PIN_D4,bits=8)
printf("\r\nGecici=%Lu"gecici);
}
|
On computer display, It says
Gecici=0
Gecici=100
Gecici=200
Gecici=44
Gecici=144
Then, I tried another code for multiplication error.
Code: |
unsigned int16 gecici;
int8 i;
gecici=4*100;
#use delay (clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_D5,rcv=PIN_D4,bits=8)
printf("\r\nGecici=%Lu"gecici);
|
Computer says,
Gecici=400
I could not understand this problem.
My compiler version is 4.030 |
|
|
pcmgogo
Joined: 25 Dec 2004 Posts: 9
|
Problem solved |
Posted: Wed Jan 14, 2009 10:50 am |
|
|
I solved multiplication problem.
I tried to change type of i variable,
I changed i from unsigned int8 to unsigned int16 then problem solved. |
|
|
Ttelmah Guest
|
|
Posted: Wed Jan 14, 2009 4:02 pm |
|
|
You could also 'cast' the arithmetic.
so:
Code: |
#use delay (clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_D5,rcv=PIN_D4,bits=8)
unsigned int16 gecici;
int8 i;
for(i=0;i<5;i++)
{
gecici=(int16)i*100;
printf("\r\nGecici=%Lu"gecici);
}
|
or:
Code: |
#use delay (clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_D5,rcv=PIN_D4,bits=8)
unsigned int16 gecici;
int8 i;
for(i=0;i<5;i++)
{
gecici=i*100L;
printf("\r\nGecici=%Lu"gecici);
}
|
and leave i as an int8.
The problem is being caused, because both 'i', and the '100', are _int8_ values in the original sum. Using the cast, tells the compiler to convert the int8, to int16, before performing the arithmetic, while adding the 'L', tells he compiler that the '100' constant, is a 'long' (int16) constant, giving the same effect.
The advantage of casting here is that the increment operations on 'i', can still use int8 arithmetic.
Put your #use delay, and #use RS232 statements in front of all your code. The delay in particular, wants to apply to the whole program (unless you are doing things like oscillator switching inside the code).
Best Wishes |
|
|
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Thu Jan 15, 2009 5:35 am |
|
|
Or instead of casting, you could use _mul, which either takes two int8 parameters and returns an int16, or two int16's returning an int32: Code: | gecini = _mul(i, 100); | The compiler may produce slightly more optimal code, too. _________________ Andrew |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|