View previous topic :: View next topic |
Author |
Message |
Robert Hunt
Joined: 03 Mar 2004 Posts: 1
|
Printf floating point problem |
Posted: Wed Mar 03, 2004 11:06 am |
|
|
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
|
|
Posted: Wed Mar 03, 2004 12:49 pm |
|
|
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
|
|
Posted: Wed Mar 03, 2004 1:47 pm |
|
|
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
|
|
Posted: Thu Mar 04, 2004 3:47 am |
|
|
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 |
|
|
|