View previous topic :: View next topic |
Author |
Message |
prayami
Joined: 22 Dec 2004 Posts: 78
|
Constant out of the valid range |
Posted: Wed Jan 12, 2005 2:37 pm |
|
|
Hi...Anybody figure it out?
I am using 18F4525 and CCS PCH Compiler.
I want to make one line of code instead of following two lines.
Where
int16 NewRPM;
int8 CylNum;
int32 TimerCycle;
Quote: |
TimerCycle=TimerCycle/4;
NewRPM = (int16)(600000000/(TimerCycle*CylNum) );
|
If I make it like following
Quote: |
NewRPM = (int16)(2400000000/(TimerCycle*CylNum) );
|
then it is giving error
Quote: |
Constant out of the valid range -1894967296 is not 0..4294967295
|
This means 2400000000 is not in the range of 0..4294967295
How come this ?
Is there any other way to combine above two line of code. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 12, 2005 3:37 pm |
|
|
It appears that CCS constants are signed 32-bit integers, not unsigned. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jan 12, 2005 5:06 pm |
|
|
It seems like an internal compiler bug to me where the compiler is working with signed 32-bit integers instead of unsigned (in an ANSII compiler this would be the correct behaviour, but CCS has all variables unsigned by default)..
The specified constant is an unsigned integer and fits in the range specified in the error message, so I consider this a compiler error.
A dirty workaround: Code: | NewRPM = (int16)(((signed int32)2400000000)/(TimerCycle*CylNum) ); |
Please email this in a bug report to [email protected] |
|
|
prayami
Joined: 22 Dec 2004 Posts: 78
|
|
Posted: Wed Jan 12, 2005 9:08 pm |
|
|
Hi..
Thanks for reply. But as my number 2400000000 is positive then it must
fit with unsigned int32 bit. And if you check the number carefully it is
bigger than signed int32 bit. So if I cast it to signed int32 then,
it disturbed my RPM calculation.
Error:
Constant out of the valid range -1894967296 is not 0..4294967295
If you focus on error. Then it seems that compiler is not treating it like
unsigned int32. So casting it into signed int32 can solve the compilation
error but turns to give wrong RPM calculation as my number is bigger than
signed int32.
Is it really bug? Can you please try in your program? |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Jan 12, 2005 9:52 pm |
|
|
More efficient to do this:
NewRPM = (1171875/(TimerCycle*CylNum) );
NewRPM *= 2048; |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Jan 13, 2005 2:31 am |
|
|
Quote: | Thanks for reply. But as my number 2400000000 is positive then it must
fit with unsigned int32 bit. And if you check the number carefully it is
bigger than signed int32 bit. So if I cast it to signed int32 then,
it disturbed my RPM calculation. |
Your calculation is not disturbed using my workaround. I called it a 'dirty workaround' because it uses a feature of the cast operator. The cast operator changes a variable from a certain type to any other type you specify without doing any conversion of the internal value. Basically the cast operator is a very dangerous operator because you take a way control from the compiler. Here it works because both signed and unsigned int32 variables have the same internal data size.
I made a small test program and checked the resulting assembly code. Works great!
Still, I consider the error message a compiler bug and should be reported to CCS. If you are unsure, just mail it to them and have them decide what to do with it. |
|
|
|