474,046 Members | 6,109 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Constant Define--Compiler Issue with ANSI or my compiler or me?



----snip

#define POSITIVE_INTEGR ATOR_SATURATION 0x03000000L //
#define NEGATIVE_INTEGR ATOR_SATURATION 0xFD000000L //

long integrator;

integrator=0;

if (integrator>POS ITIVE_INTEGRATO R_SATURATION)
integrator=POSI TIVE_INTEGRATOR _SATURATION;
if (integrator<NEG ATIVE_INTEGRATO R_SATURATION)
integrator=NEGA TIVE_INTEGRATOR _SATURATION;
1. Why does the executable always assign
NEGATIVE_INTEGR ATOR_SATURATION to integrator?

-I assume that long declaration means the value is signed (the most
significant bit indicates sign)

------------
#define INTEGRATOR_SATU RATION 0x03000000L // 3

long integrator;

integrator=0;

if (integrator>INT EGRATOR_SATURAT ION)
integrator=POSI TIVE_INTEGRATOR _SATURATION;
if (integrator<(0-INTEGRATOR_SATU RATION))
integrator=NEGA TIVE_INTEGRATOR _SATURATION;

This code leaves integrator at 0, as intended.

-------------

2. Why does the second snippet work, while the first does not.

Additional Info- My machine uses four bytes for long.

I would be happy to learn how to make this portable as soon as I stop
the limit cycles in my control system.


Nov 14 '05 #1
7 1796
No Spam wrote:
#define POSITIVE_INTEGR ATOR_SATURATION 0x03000000L //
#define NEGATIVE_INTEGR ATOR_SATURATION 0xFD000000L //

long integrator;

integrator=0;

if (integrator>POS ITIVE_INTEGRATO R_SATURATION)
integrator=POSI TIVE_INTEGRATOR _SATURATION;
if (integrator<NEG ATIVE_INTEGRATO R_SATURATION)
integrator=NEGA TIVE_INTEGRATOR _SATURATION;

1. Why does the executable always assign
NEGATIVE_INTEGR ATOR_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 (4244635648) 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)0xFD00000 0

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
es*****@acm-dot-org.invalid
Nov 14 '05 #2
On Wed, 29 Dec 2004 08:50:46 -0500, Eric Sosman
<es*****@acm-dot-org.invalid> wrote:
No Spam wrote:
#define POSITIVE_INTEGR ATOR_SATURATION 0x03000000L //
#define NEGATIVE_INTEGR ATOR_SATURATION 0xFD000000L //

long integrator;

integrator=0;

if (integrator>POS ITIVE_INTEGRATO R_SATURATION)
integrator=POSI TIVE_INTEGRATOR _SATURATION;
if (integrator<NEG ATIVE_INTEGRATO R_SATURATION)
integrator=NEGA TIVE_INTEGRATOR _SATURATION;

1. Why does the executable always assign
NEGATIVE_INTEGR ATOR_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 (4244635648) 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.
You are telling me

#define foo1 0xFFFFFFFFL

#define foo2 -1L

(foo1==foo2) evaluates to false

where the machine allocates 32 bits for type long?

Correct?

So by the paragraph,
#define foo1 0xFFFFFFFFUL

#define foo2 0xFFFFFFFFL

(foo1==foo2) evaluates to true (on my machine)?

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)0xFD00000 0

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.


Yes I freely admit that I translated the code into C from an assembly
program, but kept the assembly language way of thinking. Plus I never
would have thought of expressing a hexadecimal number with a negative
sign in front of it.
Nov 14 '05 #3
No Spam wrote:
You are telling me

#define foo1 0xFFFFFFFFL

#define foo2 -1L

(foo1==foo2) evaluates to false

where the machine allocates 32 bits for type long?


Let me say that he is not telling you that. For the equality operation,
the (long) operand -1L is converted to the type of the (unsigned long)
operand 0xFFFFFFFFL. Consequently, the expression evaluates to 1 (true).
Nov 14 '05 #4
On Wed, 29 Dec 2004 20:58:42 +0000, No Spam wrote:
On Wed, 29 Dec 2004 08:50:46 -0500, Eric Sosman>
<es*****@acm-dot-org.invalid> wrote:
....
Since the value 0xFD000000L (4244635648) 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.


You are telling me

#define foo1 0xFFFFFFFFL

#define foo2 -1L

(foo1==foo2) evaluates to false

where the machine allocates 32 bits for type long?

Correct?


No, on an implementation with 32 bit longs Eric is saying that 0XFFFFFFFFL
will have type unsigned long. In the expression foo1==foo2, since the left
hand side has type unsigned long, the right hand side will be converted to
that type before the comparison is made. (unsigned long)-1L evaluates to
ULONG_MAX which in 32 bits will be 0xFFFFFFFF so foo1 and foo2 will
compare equal in that case.
So by the paragraph,
#define foo1 0xFFFFFFFFUL

#define foo2 0xFFFFFFFFL

(foo1==foo2) evaluates to true (on my machine)?


Yes, in a 32 bit type foo1 and foo2 both have type unsigned long
with the same value.

Lawrence
Nov 14 '05 #5
Lawrence Kirby wrote:
#define foo1 0xFFFFFFFFUL

#define foo2 0xFFFFFFFFL

(foo1==foo2 ) evaluates to true (on my machine)?

Yes, in a 32 bit type foo1 and foo2 both have type unsigned long
with the same value.


Actually, on an implementation with 32-bit long, 0xFFFFFFFFL
should be implementation defined, no? (i.e., it won't necessarily
get the value -1, and thus won't necessarily convert to
0xFFFFFFFFUL on comparison...)
Nov 14 '05 #6
Micah Cowan wrote:
Lawrence Kirby wrote:
#define foo1 0xFFFFFFFFUL

#define foo2 0xFFFFFFFFL

(foo1==foo 2) evaluates to true (on my machine)?

Yes, in a 32 bit type foo1 and foo2 both have type unsigned long
with the same value.

Actually, on an implementation with 32-bit long, 0xFFFFFFFFL
should be implementation defined, no? (i.e., it won't necessarily
get the value -1, and thus won't necessarily convert to
0xFFFFFFFFUL on comparison...)


Aside from the 32-bitness of `long' there's nothing
implementation-defined about it. The constant's value is
too large for `long', so its type will be `unsigned long'
and its value will be 4294967295UL or `(unsigned long)-1'.

--
Er*********@sun .com

Nov 14 '05 #7
Eric Sosman wrote:
Micah Cowan wrote: Aside from the 32-bitness of `long' there's nothing
implementation-defined about it. The constant's value is
too large for `long', so its type will be `unsigned long'
and its value will be 4294967295UL or `(unsigned long)-1'.


Oops. Yup. I shoulda known better than to correct a message from
Lawrence.
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3167
by: rhmd | last post by:
Just found Python and I love it. What an elegant language! I would like to use it for various applications, but the mathematical calculations are way too slow (a million sines 8 seconds in Python vs. 2 seconds in Excel VBA), so was thinking of learning enough C++ to do the number crunching in C++ and integrating the C++ routines with Python. My question: My question: Which compiler works best with Python? I use Windows XP and 98. Have...
0
3407
by: Ryan Schefke | last post by:
------=_NextPart_000_0077_01C34C8B.2B90C960 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit ..I just sent this out to the win32 distribution list but no one has replied.can someone on this list please help? The issue should be trivial for experienced MySQL users, I'm just a novice, thanks!
3
1799
by: RWC | last post by:
Hello, I have an issue that's driving me batty! I have a report, whose record source is SQL based on a normalized set of tables. There are no nested queries and no dlookups in this record source. When I run this record source on it's own, it functions normally with no errors. The report that this SQL is designed over, contains no subreports, no dlookups, and has no code associated with it, yet when I try to run it, I get an error...
15
5906
by: Anton Gavrilov | last post by:
Hi all, I seek your advice on where to start if I want to write a compiler for a toy C-like language I invented (or, rather, am in the process of inventing). Yes, yes, I know I'm crazy and the very idea is brain-damaged and all that. Chances are I will lose interest long before I reach break-even, but it doesn't hurt trying, does it? I'm a C addict reluctant to switch to C++, partly because I can't fully grok (and accept) the OOP...
5
1866
by: aaragon | last post by:
Hello everybody, I appreciate your taking the time to take a look at this example. I need some help to start the design of an application. To that purpose I'm using policy-based design. The idea is to have a Class that stores elements of Class2 in different ways (arrays in stack memory, arrays in heap memory, using the std::vector and so on). I would like the user to customize the creation of a class with Class2 and StoragePolicy like...
10
2049
by: brett | last post by:
I have a file named Snoopy.class.php on my hosted web server in a directory called d:\home\ABC.COM\blog\wp-content\plugins\ A file named dahnielson_mimetex.php references it. However, when dahnielson_mimetex.php tries to reference the Snoopy file, I get these types of errors: Warning: main() : Failed opening './Snoopy.class.php' for inclusion
5
1831
by: dannynnad | last post by:
Hi, SuperAdministrator when trying to edit the frontend content the following error is comingup: "You are not authorized to view this resource." I checked from the backend whether the section, category n content is public. All of them are public and published. No clue why super admin can't edit the frontend content.
2
1661
by: jonlarosa | last post by:
Hi all, (Apologies for any cross-postings) I am having an issue with a navigation bar in firefox. At seemingly random times, the nav bar will not appear in 1 line (as it should), but will span 2 or more lines. For example: How the nav bar should look:
3
2284
by: zoeb | last post by:
Hi, I am declaring an array which carries x coordinates, however these will vary depending on the geometry the user enters. I have set my code up as follows, but get the "initializer element not constant" issue. I had the code compiling earlier, but made the mistake of moving thing aorund without taking a backup and now nothing works! Any help would be greatly appreciated. /*=======================================================*/...
3
2536
by: Lakshmank85 | last post by:
Hi, i have a c++ class, class XYZ { public: void Function1 ( MyStruct * myStruct ); // member variables, etc }
0
10341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11604
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
11145
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8701
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
5421
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4945
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.