View previous topic :: View next topic |
Author |
Message |
joshkeys
Joined: 16 Aug 2005 Posts: 37 Location: Fredericton,NB
|
Casting problem |
Posted: Wed Aug 24, 2005 6:44 pm |
|
|
Hi there. I am having a problem with casting. Here is a sample case
Code: |
int16 integer;
float floater;
integer = 2147;
floater = (float)integer;
//The result of this is floater = 2147.00064
|
Now here comes the real problem. If i make integer one number higher (i.e. 2148) then floater becomes -2146.96729.
I know there is some sort of signed-unsigned problem. I am really hoping someone knows the problem and how to fix it as i need to cast a int16 to a float.
Thanks
Josh |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
joshkeys
Joined: 16 Aug 2005 Posts: 37 Location: Fredericton,NB
|
|
Posted: Wed Aug 24, 2005 8:10 pm |
|
|
So do you mean the only problem is displaying it via RS232 and that infact the correct value has been attained? Because in the long run i dont care if i can see the value, as long as the value is correct. I just wanted to view it to see if it was correct. So if i take an int and cast it to a float, the float value is correct, but is just not displayed properly using the printf statement, therefore implying printf is the problem? or did i completely misread your post?
Thanks
Josh |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 24, 2005 8:34 pm |
|
|
I think it's OK internally, because the program below gives the
following results. Note that the "%f" results are bad, but the
final number is the correct sum. Based on these results, I think
the problem is just with "%f".
If you want to encourage CCS to fix the problem, I think it would
help if you reported it to their support department.
-1095.967232
-2146.967296
1052.032512
5347.000
Code: |
#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()
{
float f;
int16 integer;
float floater;
f = 3199.0;
printf("%f\n\r", f);
integer = 2148;
floater = (float)integer;
printf("%f\n\r", floater);
printf("%f\n\r", floater + f);
printf("%4.3f\n\r", floater + f);
while(1);
} |
|
|
|
joshkeys
Joined: 16 Aug 2005 Posts: 37 Location: Fredericton,NB
|
|
Posted: Wed Aug 24, 2005 8:45 pm |
|
|
What does the 4.3f mean? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|