| View previous topic :: View next topic   | 
	
	
	
		| Author | 
		Message | 
	
	
		
			assaad
 
 
  Joined: 09 Dec 2009 Posts: 37
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| multiplication and division in ccs | 
			 
			
				 Posted: Sat May 29, 2010 12:26 am     | 
				     | 
			 
			
				
  | 
			 
			
				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 
			
			 
			 
			
			 
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat May 29, 2010 12:52 am     | 
				     | 
			 
			
				
  | 
			 
			
				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: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
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat May 29, 2010 1:12 am     | 
				     | 
			 
			
				
  | 
			 
			
				| Thank you I will try that now, but what about the usage of _mul() ? | 
			 
		  | 
	
	
		  | 
	
	
		
			assaad
 
 
  Joined: 09 Dec 2009 Posts: 37
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat May 29, 2010 2:16 am     | 
				     | 
			 
			
				
  | 
			 
			
				 	  | 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: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: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat May 29, 2010 2:30 am     | 
				     | 
			 
			
				
  | 
			 
			
				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 
			
			 
			 
			
			 
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat May 29, 2010 10:25 am     | 
				     | 
			 
			
				
  | 
			 
			
				 	  | 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
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Mon May 31, 2010 2:58 am     | 
				     | 
			 
			
				
  | 
			 
			
				Thank you both   it is ok now . 
 
I will be back with other questions    | 
			 
		  | 
	
	
		  | 
	
	
		 |