View previous topic :: View next topic |
Author |
Message |
MikeV
Joined: 20 Feb 2004 Posts: 4
|
printf problem with float. ex. fahrenheit to celsius... |
Posted: Fri Feb 27, 2004 11:35 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: printf problem with float. ex. fahrenheit to celsius... |
Posted: Fri Feb 27, 2004 11:54 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
chux Guest
|
int problem |
Posted: Wed Mar 17, 2004 1:51 pm |
|
|
Quote: |
int upper; and upper = 300 results in upper set to 44 as upper is only 8 bits.
|
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Mar 17, 2004 4:16 pm |
|
|
just cast the int to afloat to avoid confussion |
|
![](templates/subSilver/images/spacer.gif) |
|