  | 
	  | 
		 
	 
	
		| View previous topic :: View next topic   | 
	 
	
	
		| Author | 
		Message | 
	 
	
		
			VanHauser
 
 
  Joined: 03 Oct 2005 Posts: 88 Location: Ploiesti, Romania 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| printf() odd behavior with floats | 
			 
			
				 Posted: Fri May 19, 2006 4:39 am     | 
				     | 
			 
			
				
  | 
			 
			
				I'm using PCH 3.249. The printf() function does not seem to format %f fields correctly. This is my test code, on 18F252:
 
 	  | Code: | 	 		     printf("%f\r\n", 0.0001);
 
   printf("%f\r\n", 12.0);
 
   printf("%f\r\n", 9999.0);
 
   printf("%f\r\n", -13.25);
 
   printf("%f\r\n", 13.45);
 
   printf("%f\r\n", -1999.0);
 
   printf("%f\r\n", -0.000035);
 
   printf("%f\r\n", -9999.0); | 	  
 
 
And this is what I get:
 
 	  | Code: | 	 		  0.000100
 
12.000000
 
1409.065987
 
-13.250000
 
13.450000
 
-1999.000064
 
-0.000035
 
-1409.065987 | 	  
 
 
9999.0 and -9999.0 are not displayed correctly    Some advice, anyone? | 
			 
		  | 
	 
	
		  | 
	 
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri May 19, 2006 11:50 am     | 
				     | 
			 
			
				
  | 
			 
			
				This is a known bug with the "%f" format specifier.    To fix it, you need
 
to put in the width and precision fields.   Also, the width field must be
 
greater than the precision field.    
 
 
It's best if you always put in the width and precision fields.
 
Don't use the plain "%f" specifier. 
 
 
 	  | Quote: | 	 		  
 
#include <16F877.H>
 
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
 
#use delay(clock=4000000) 
 
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
 
 
//============================	
 
void main()
 
{
 
printf("%f\r\n", 0.0001); 
 
printf("%f\r\n", 12.0); 
 
printf("%7.5f\r\n", 9999.0); 
 
printf("%f\r\n", -13.25); 
 
printf("%f\r\n", 13.45); 
 
printf("%f\r\n", -1999.0); 
 
printf("%f\r\n", -0.000035); 
 
printf("%7.5f\r\n", -9999.0);
 
 
while(1); 
 
}  | 	  
 
 
 
References:
 
http://www.ccsinfo.com/forum/viewtopic.php?t=26059&start=6
 
http://www.ccsinfo.com/forum/viewtopic.php?t=24719&start=1 | 
			 
		  | 
	 
	
		  | 
	 
	
		 | 
	 
 
  
	 
	    
	   | 
	
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
  
		 |