No Spam wrote:
> #define POSITIVE_INTEGRATOR_SATURATION 0x03000000L //
> #define NEGATIVE_INTEGRATOR_SATURATION 0xFD000000L //
>
> long integrator;
>
> integrator=0;
>
> if (integrator>POSITIVE_INTEGRATOR_SATURATION)
> integrator=POSITIVE_INTEGRATOR_SATURATION;
> if (integrator<NEGATIVE_INTEGRATOR_SATURATION)
> integrator=NEGATIVE_INTEGRATOR_SATURATION;
>
> 1. Why does the executable always assign
> NEGATIVE_INTEGRATOR_SATURATION to integrator?
Because NEG...ION is a large positive number, hence
greater than zero.
> -I assume that long declaration means the value is signed (the most
> significant bit indicates sign)
Yes, a `long' is signed. And yes, the most significant
bit of a signed integer is the sign bit. But you've missed
something: The type of 0xFD000000L is not `long' (on your
machine, where `long' occupies 32 bits), but `unsigned long'.
Section 6.4.4.1 paragraph 5:
The type of an integer constant is the first of the
corresponding list in which its value can be
represented.
[... and for a hexadecimal constant with an L suffix
the list begins `long int', `unsigned long int', ...]
Since the value 0xFD000000L (424463564

is greater than
your system's LONG_MAX, it cannot be represented as a `long'.
But it can be represented as an `unsigned long', so that is
the constant's type.
Now: Almost all C operators that use two operands require
the operands to have the same type. If they're not already
of the same type, C promotes one or both until the promoted
types match, and then applies the operator to the promoted
values. When you write
integrator < NEG...ION
you are trying to compare a `long' and an `unsigned long',
so C actually evaluates
(unsigned long)integrator < NEG...ION
.... and for the values given, this comparison is true.
Suggested fix:
#define NEG...ION -0x30000000L
Inferior (because of dubious portability) fix:
#define NEG...ION (long)0xFD000000
General principle: Stop thinking about the way your numbers
are represented, and start thinking about their values. You
will save yourself much frustration by doing so.
--
Eric Sosman
lid