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

Casting problem

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



Joined: 16 Aug 2005
Posts: 37
Location: Fredericton,NB

View user's profile Send private message Send e-mail MSN Messenger

Casting problem
PostPosted: Wed Aug 24, 2005 6:44 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 24, 2005 8:05 pm     Reply with quote

The floating point display routine is buggy, since vs. 2.231.
The new vs. 3.232 did not fix the problem.
See my post near the end of this thread.
http://www.ccsinfo.com/forum/viewtopic.php?t=24052&highlight=floating+point
joshkeys



Joined: 16 Aug 2005
Posts: 37
Location: Fredericton,NB

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Aug 24, 2005 8:10 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 24, 2005 8:34 pm     Reply with quote

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

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Aug 24, 2005 8:45 pm     Reply with quote

What does the 4.3f mean?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 24, 2005 9:06 pm     Reply with quote

Scroll down near the bottom of this page, where it has a section
called "The printf Function":
http://uhaweb.hartford.edu/jmhill/suppnotes/creview/opening.htm
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