CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

printf() odd behavior with floats

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
VanHauser



Joined: 03 Oct 2005
Posts: 88
Location: Ploiesti, Romania

View user's profile Send private message

printf() odd behavior with floats
PostPosted: Fri May 19, 2006 4:39 am     Reply with quote

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 Shocked Some advice, anyone?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 19, 2006 11:50 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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