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 problem with float. ex. fahrenheit to celsius...

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



Joined: 20 Feb 2004
Posts: 4

View user's profile Send private message

printf problem with float. ex. fahrenheit to celsius...
PostPosted: Fri Feb 27, 2004 11:35 am     Reply with quote

using 18F876.
I copied from page 12 in the K&R book to convert fahrenheit to celsius:

void main(void)
{
float fahr, celsius;
int lower, upper, step;

lower = 0;
upper = 300;
step = 20;

printf("\r\n\r\n\r\n\r\n");
fahr = lower;
while (fahr <= upper)
{
celsius = (5.0/9.0) * (fahr - 32.0);
printf ("%6.1f %6.1f\r\n", fahr, celsius);
fahr = fahr + step;
}
}


HOWEVER, when i do a printf to hyperterminal, it freezes and only gives me a partial printout:

.0 -17.7
19.9 -6.6
39.9 4.4

But when I change "int lower, upper, step" to a float, then i get the desired printout:

.0 -17.7
19.9 -6.6
39.9 4.4
59.9 15.5
79.9 26.6
99.9 37.7
119.9 48.8
139.9 60.0
159.9 71.1
179.9 82.2
199.9 93.3
219.9 104.4
239.9 115.5
259.9 126.6
279.9 137.7
299.9 148.8

Does the CCS compiler have anything against adding a float and an int?

p.s. the actual celsius equivalent to 0F is -17.77. K&R's example output on a different platform will print -17.8. However, the PIC prints -17.7. How can I avoid this truncation and instead round up to the next tenths?

Thanks,
Mike
Thanks,
Mike
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: printf problem with float. ex. fahrenheit to celsius...
PostPosted: Fri Feb 27, 2004 11:54 am     Reply with quote

MikeV wrote:
p.s. the actual celsius equivalent to 0F is -17.77. K&R's example output on a different platform will print -17.8. However, the PIC prints -17.7. How can I avoid this truncation and instead round up to the next tenths?


Hey Mike it did round up from -17.77 to -17.7. Up is a relative term when your looking at negitave numbers.
chux
Guest







int problem
PostPosted: Wed Mar 17, 2004 1:51 pm     Reply with quote

Quote:

int upper; and upper = 300 results in upper set to 44 as upper is only 8 bits.
Guest








PostPosted: Wed Mar 17, 2004 4:16 pm     Reply with quote

just cast the int to afloat to avoid confussion
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