| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| jemly 
 
 
 Joined: 13 May 2007
 Posts: 34
 
 
 
			    
 
 | 
			
				| Integer to float problem |  
				|  Posted: Fri Jul 06, 2007 9:36 am |   |  
				| 
 |  
				| I have a bit of code like this: 
 PulseCount = maxMove/yMove;
 
 where all values are currently int16; However I really need pulseCount to be a rounded float. I have tried the following:
 
 float fpCount = (float)maxMove/(float)yMove;
 
 but my system seems to just drop out when I try and step through that line even though it all compiles fine. Can someone help me understand what I should be doing here?!
 
 Thank you.
 |  | 
	
		|  | 
	
		| Ttelmah Guest
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				|  |  
				|  Posted: Fri Jul 06, 2007 9:43 am |   |  
				| 
 |  
				| Probably the 'float' declaration in front of the first variable. You can't declare a variable 'inline' in standard C. The 'cast' (variable type in brackets), is normal, and fine (converts the variable on the fly).
 
 So:
 
  	  | Code: |  	  | //at the start of the function.
 float fpCount;
 
 fpCount = (float)maxMove/(float)yMove;
 
 | 
 In the type declaration (at the start of the function), values asigned, must be 'constants', not 'variables'.
 
 Best Wishes
 |  | 
	
		|  | 
	
		| SherpaDoug 
 
 
 Joined: 07 Sep 2003
 Posts: 1640
 Location: Cape Cod Mass USA
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jul 06, 2007 11:07 am |   |  
				| 
 |  
				| Do you really want a float for the output or would a scaled integer be better?  How about  	  | Code: |  	  | PulseCounttimes100 = (maxMove * 100)/yMove; | 
 Then you can use string functions to insert a decimal point when you print.  Generally avoid using floats if at all possible as they are so slow and code intensive.
 _________________
 The search for better is endless.  Instead simply find very good and get the job done.
 |  | 
	
		|  | 
	
		| jemly 
 
 
 Joined: 13 May 2007
 Posts: 34
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jul 09, 2007 8:16 am |   |  
				| 
 |  
				| Thanks to both of you for your replies - really useful and will be trying out the suggestions later this week. Thanks! |  | 
	
		|  | 
	
		| Ken Johnson 
 
 
 Joined: 23 Mar 2006
 Posts: 197
 Location: Lewisburg, WV
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jul 09, 2007 8:20 am |   |  
				| 
 |  
				| "Generally avoid using floats if at all possible as they are so slow and code intensive." 
 I strongly disagree - this is like saying "generally avoid using C and program in assembler only" - same reasoning.
 
 There are exceptions to every rule, but you can generally work with floats and real-world uints, making your code much easier to write and to later understand (think maintenance). Keep your ISRs short and fast, and let your main loop do whatever is needed. Use printf for user output. I've measured "workload" on many many projects, and generally, the microprocessor is idle far more than one would think.
 
 Happy coding
   
 Ken
 |  | 
	
		|  | 
	
		|  |