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

automatic typecasting is broken

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



Joined: 21 Sep 2009
Posts: 21

View user's profile Send private message

automatic typecasting is broken
PostPosted: Tue Sep 07, 2010 2:15 pm     Reply with quote

Automatic typecasting is broken in version 4.112 (PIC18)

The following code gives the wrong value:
Code:

long scandex;
int thiskey;

scandex=thiskey*3;



To get the correct result you must do this:
Code:

long scandex;
int thiskey;

scandex=(long)thiskey*3;


If you're pulling your hair out maybe this is why.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Sep 07, 2010 2:25 pm     Reply with quote

Since thiskey is an int and 3 is an int I would expect the output value to wrap at 255 for thiskey values above 85. Is this what it does? What are you expecting? Can you give us some sample inputs and outputs?
_________________
The search for better is endless. Instead simply find very good and get the job done.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

Re: automatic typecasting is broken
PostPosted: Tue Sep 07, 2010 2:52 pm     Reply with quote

equack wrote:
Automatic typecasting is broken in version 4.112 (PIC18)

The following code gives the wrong value:
Code:

long scandex;
int thiskey;

scandex=thiskey*3;



To get the correct result you must do this:
Code:

long scandex;
int thiskey;

scandex=(long)thiskey*3;


If you're pulling your hair out maybe this is why.


I would expect (knowing what I know about C and microcontrollers) that if you take an INT8 and *3, that values over the size of the INT would roll over before they can be assigned to a larger INT.

One way to fix that is exactly what you've done.
The other is breaking down the equation to component steps.

scandex = thiskey;
scandex *= 3;

sometimes with the long stuff, it's easier to read... but anyway.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
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