| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| pozzari 
 
 
 Joined: 30 Oct 2010
 Posts: 3
 
 
 
			    
 
 | 
			
				| float 1 div by 2 == 0 ?? |  
				|  Posted: Mon May 28, 2012 4:09 pm |   |  
				| 
 |  
				| Very single program running on pic18f452 
 This displays 0.00 on my debug... why??
 
 
  	  | Code: |  	  | void main()
 {
 float div_result;
 int part1;
 int part2;
 
 part1 = 1;
 part2 = 2;
 
 div_result = part1 / part2;
 
 fprintf(usart_debug,"Ini=%f\r\n",div_result);
 
 
 while(1){//loop forever
 
 }
 
 }
 
 
 | 
 |  |  
		|  |  
		| dyeatman 
 
 
 Joined: 06 Sep 2003
 Posts: 1968
 Location: Norman, OK
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon May 28, 2012 4:19 pm |   |  
				| 
 |  
				| Try this... 
 div_result = (float) part1 / part2;
 _________________
 Google and Forum Search are some of your best tools!!!!
 |  |  
		|  |  
		| pozzari 
 
 
 Joined: 30 Oct 2010
 Posts: 3
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon May 28, 2012 4:59 pm |   |  
				| 
 |  
				| div_result = (float)part1 / (float)part2 worked fine... thanks a lot |  |  
		|  |  
		| RF_Developer 
 
 
 Joined: 07 Feb 2011
 Posts: 839
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue May 29, 2012 1:19 am |   |  
				| 
 |  
				| Pozzari, do you understand why that worked? 
 RF Developer
 |  |  
		|  |  
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19962
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue May 29, 2012 1:32 am |   |  
				| 
 |  
				| Worth also explaining 'why'. In C, the maths operations, when compiled, look at the 'types' of the value/values _involved in each part of the calculation_ (not the result), and select the maths 'type', required by the 'highest type' of these. Types are ordered:
 
  	  | Code: |  	  | Float, signed int32, int32, signed int16, int16, signed int8, int8.
 
 | 
 So in the original sum, the compiler says "int8/int8, use int8 arithmetic".
 
 Only one of the values (either), actually needs to be 'cast' to the higher type, since the highest type involved is selected. So:
 
  	  | Code: |  	  | div_result = (float)part1 / part2;
 div_result = part1 / (float)part2;
 div_result = (float)part1 / (float)part2;
 
 | 
 will all give the same result. Once one value in the sum is raised to the higher type, the others are automatically raised as needed.
 
 Now, this is very useful, since it means you can elect to use 'lower' maths types (which are much faster/smaller), until a higher type is needed.
 
 So (for instance),
 
  	  | Code: |  	  | result = (int16)part1*part2;
 
 | 
 Uses int16 arithmetic to multiply the two int8 values - takes about 17 instructions, versus 180 for the 'float' version.
 Then:
 
  	  | Code: |  	  | result=((int16)part1*part2)/(float)part3;
 
 | 
 Uses int16 for the multiplication, and then switches to float for the division. Still saving perhaps 150 instructions.....
 
 Historically, C was written to do this, since it allowed efficient use of the chip's resources. However on things like the PC, with the addition of a hardware maths co-processor, this has become slightly 'bypassed', with all the sums using this hardware feature, and therefore not worrying about the extra performance involved....
 
 Best Wishes
 |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |