Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648

Thomas David Rivers <rivers@dignus.com>
31 Jan 2006 21:21:08 -0500

          From comp.compilers

Related articles
1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 devriese@cs.tcd.ie (Edsko de Vries) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 torbenm@app-0.diku.dk (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 rsc@swtch.com (Russ Cox) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 jm@bourguet.org (Jean-Marc Bourguet) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 gneuner2@comcast.net (George Neuner) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 rivers@dignus.com (Thomas David Rivers) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 david@tribble.com (David R Tribble) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 henry@spsystems.net (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 gah@ugcs.caltech.edu (glen herrmannsfeldt) (2006-02-02)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 DrDiettrich@compuserve.de (Hans-Peter Diettrich) (2006-02-02)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 david@tribble.com (David R Tribble) (2006-02-03)
| List of all articles for this month |
From: Thomas David Rivers <rivers@dignus.com>
Newsgroups: comp.compilers
Date: 31 Jan 2006 21:21:08 -0500
Organization: Dignus, LLC
References: 06-01-131 06-01-135
Keywords: C, arithmetic
Posted-Date: 31 Jan 2006 21:21:08 EST

Russ Cox wrote:
>>This works fine, with the sole exception of the number "-2147483648".
>>The problem is, of course, overflow: -2147483648 is a valid negative
>>number (assuming 32-bit numbers), but the integer 2147483648 is _not_ a
>>valid positive number. Thus, the above method of dealing with "-" as a
>>unary operator breaks down.
>
>
> I know a few vendor-supplied C compilers that get this wrong,
> printing warnings about "positive constant overflows" or something
> like that, which is all the more confusing an error when you look at
> it and say "but that's *not* a positive constant!".
>


Your C compiler is correct. In standard C there are no "negative"
integer constants.


See section 6.4.4.1 of the C99 standard.


What is happening here, on a 32-bit 2's compliment machine is that
the value is too large to fit into a signed 'int', so
it becomes an 'unsigned int', following the type hierarchy
in section 6.4.4.1. You've then applied a unary negation
to an 'unsigned int' and the result is also an 'unsigned int' (see
section 6.5.3.3).... it's not clear what the arithmetic
negation of an 'unsigned int' would be.


For this example, our compiled generates:


      Warning #2296: unary negation applied to an unsigned type


Some other compilers also generate warnings like "constant is so large
that it is unsigned", I think that's one I've seen from GCC before.




So - to go back to what the original post was about, the C language
has the same problem.


Our solution is to keep constants as strings. When it is time for
the value of the constant to be evaluated, we have to examine it
to see which type can "hold" it, and place it in appropriate
"containers" for that type.


This can be particularly cumbersome in a cross-platform environment,
such as a 32-bit host compiling for a 64-bit machine... where
there would be a requirement for manipulating large values in
a non-native fashion (e.g. some "bignum" implemenation.)




- Dave Rivers -


--
rivers@dignus.com Work: (919) 676-0847
Get your mainframe programming tools at http://www.dignus.com


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.