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 CCS Technical Support

Printf floating point problem

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



Joined: 03 Mar 2004
Posts: 1

View user's profile Send private message

Printf floating point problem
PostPosted: Wed Mar 03, 2004 11:06 am     Reply with quote

I am programing a pic16f877 and having problems with printf

I am using PCM Version 3.148

Stats on compiling are

ROM used: 4668 (57%)
2048 (25%) including unused fragments

RAM used: 93 (25%) at main() level
114 (31%) worst case


Segment Used Free
--------- ---- ----
00000-00003 4 0
00004-00032 47 0
00033-007FF 1985 12
00800-00FFF 966 1082
01000-017FF 1666 382
01800-01FFF 0 2048




The following lines are extracted from the program

float ch1;
long time_low,time_high;

ch1 = time_high/(float)(time_high+time_low);
printf ("X%1.4f \r" ,ch1);

the output should be like “X .5132”

sometimes I get an output like “X0000000000000000000.513243367537522345678”

as I was developing the program some times the program would print floating point numbers correctly and some times it would insert a long line of zeros and extra decimal places.

By accident I found that if I declared an int32 variable or 2 int16 variables the printf statement would work properly. Is there an addressing option that has to be set in the compiler. I think the problem has something to do with memory paging.

Any ideas would be appreciated.
Charlie U



Joined: 09 Sep 2003
Posts: 183
Location: Somewhere under water in the Great Lakes

View user's profile Send private message

PostPosted: Wed Mar 03, 2004 12:49 pm     Reply with quote

time_high is declared as a long. Have you tried casting it to a float also?

Such as:

ch1 = (float)time_high/(float)(time_high+time_low);
Guest








PostPosted: Wed Mar 03, 2004 1:47 pm     Reply with quote

Charlie U wrote:
time_high is declared as a long. Have you tried casting it to a float also?

Such as:

ch1 = (float)time_high/(float)(time_high+time_low);




Yes i have tried casting both time_high and time_low as float
with no success. other variables that are float are not being formated
correctly.
Ttelmah
Guest







PostPosted: Thu Mar 04, 2004 3:47 am     Reply with quote

Anonymous wrote:
Charlie U wrote:
time_high is declared as a long. Have you tried casting it to a float also?

Such as:

ch1 = (float)time_high/(float)(time_high+time_low);




Yes i have tried casting both time_high and time_low as float
with no success. other variables that are float are not being formated
correctly.

You are confusing the compiler, by your format. The %x.y format, has 'x' as the 'minimum output width', and 'y' as the characters after the decimal point. You are treating it as if 'x' was to specify the characters before the decimal point. Unfortunately, the CCS C, has problems with the 'minimum width' handling, and doesn't allways expand the fields as required. Asking for 4 digits after the decimal point on a field that is only one character wide, drives it up the wall!...
Either use:
%5.4
which should give the output you expect, or consider using the integer handling to reformat the output, with something like:

int32 ch1;

ch1 = time_high*10000l/(time_high+time_low);
printf ("X%1d.%04ld \r" ,(int)ch1/10000l,ch1%10000l);

This gives data laid out as:

.xxxx
1.xxxx

etc..
Using the integer outputs like this, generally gives faster code than using the float arithmetic.

Best Wishes
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