| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| uni_student 
 
 
 Joined: 01 Aug 2007
 Posts: 38
 Location: AUckland, NEW ZEALAND
 
 
			        
 
 | 
			
				| 2 lines of code greedy on ROM |  
				|  Posted: Fri Oct 09, 2009 10:45 pm |   |  
				| 
 |  
				| Pic 16f716 
 Before adding these 2 lines ROm usage is 78%
 
 
  	  | Code: |  	  | dist=dist + (atof(temp)/3600);
 printf("%1.2fkm  ", dist);
 
 | 
 
 After "Out of ROM" Error mesage.
 
 How can I fix this? Any help appreciated.
 |  | 
	
		|  | 
	
		| andrewg 
 
 
 Joined: 17 Aug 2005
 Posts: 316
 Location: Perth, Western Australia
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Fri Oct 09, 2009 11:46 pm |   |  
				| 
 |  
				| Floating point code takes up a lot of space. Stick to integer math wherever possible. 
 From the code you've given, you're only needing to track distance in kilometers to 2 decimal places, i.e. to 10m. So count in multiples of 10m.
 
  	  | Code: |  	  | dist = dist + temp / 36; printf("%ld0m", dist);
 | 
 There are other things to try. You could shift the "/36" to the printf to maintain precision. You could print to a buffer (see sprintf), allowing you to shuffle characters around and insert a decimal place for kms before displaying.
 _________________
 Andrew
 |  | 
	
		|  | 
	
		| uni_student 
 
 
 Joined: 01 Aug 2007
 Posts: 38
 Location: AUckland, NEW ZEALAND
 
 
			        
 
 | 
			
				|  |  
				|  Posted: Sat Oct 10, 2009 12:18 am |   |  
				| 
 |  
				| andrewg 
 I used a less precise measurement in conjunction with atoi instead of atof which has certainly fixed the problem.
 
 Cheers
 |  | 
	
		|  | 
	
		|  |