CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

multiplication and division in ccs

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
assaad



Joined: 09 Dec 2009
Posts: 37

View user's profile Send private message

multiplication and division in ccs
PostPosted: Sat May 29, 2010 12:26 am     Reply with quote

Hi
I am a little confused to use multiplication and division correctly in ccs.

For example:
Code:

unsigned int8 x;
unsigned int16 y;
unsigned int32 z;
x=200;
y=58000;

z=x*y;

I am using MPLAB to simulate the result, z gives wrong answer.
I tried to set x to unsigned int16, but still gives wrong answer !

Can anyone guide me to the correct multiplication, also for division ?

Also I use _mul(); but sometimes it gives correct answers sometimes not.

Thank you
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Sat May 29, 2010 12:52 am     Reply with quote

This is a type conversion problem.

x*y is an (int8*int16). The result will be an int32. So x needs to be 'up-converted' into an int32 before multiplication. Try:
Code:
z=(int32)x*y;
For division, 'z' needs to be a float value, otherwise 200/58000 = 0. Use this:
Code:
float z;
z=(float)x/y;


Rohit
assaad



Joined: 09 Dec 2009
Posts: 37

View user's profile Send private message

PostPosted: Sat May 29, 2010 1:12 am     Reply with quote

Thank you I will try that now, but what about the usage of _mul() ?
assaad



Joined: 09 Dec 2009
Posts: 37

View user's profile Send private message

PostPosted: Sat May 29, 2010 2:16 am     Reply with quote

Rohit de Sa wrote:
This is a type conversion problem.

x*y is an (int8*int16). The result will be an int32. So x needs to be 'up-converted' into an int32 before multiplication. Try:
Code:
z=(int32)x*y;
For division, 'z' needs to be a float value, otherwise 200/58000 = 0. Use this:
Code:
float z;
z=(float)x/y;


Rohit


also y is int16 , should I convert up to 32 ?
z=(int32)x*(int32)y ;
right ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sat May 29, 2010 2:30 am     Reply with quote

If you have (say) two int16 values, and you multiply them together, using *, then the arithmetic is done using int16 arithmetic. So, if you take (say), 500*500, then you get 53392, which is the '16bit' result of this multiplication (low 16bits of the result).
To get the proper result, you have two choices:
1) Convert one of the values to int32 before the arithmetic. This forces int32 arithmetic to be used.
2) Use _mul. _mul performs 16bit arithmetic, but 'keeps' the overflow, to give the higher type result. Effectively converting 'up' as needed.

The later is faster than the former.

Best Wishes
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Sat May 29, 2010 10:25 am     Reply with quote

Quote:
also y is int16 , should I convert up to 32
No, you don't have to. As Ttelmah mentioned, int32 arithmetic is used even if one of the variables is an int32.

Rohit
assaad



Joined: 09 Dec 2009
Posts: 37

View user's profile Send private message

PostPosted: Mon May 31, 2010 2:58 am     Reply with quote

Thank you both Smile it is ok now .
I will be back with other questions Shocked
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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