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
Posted: Tue Sep 07, 2010 2:25 pm
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
Re: automatic typecasting is broken
Posted: Tue Sep 07, 2010 2:52 pm
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
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