# Re: The signs of literals

## David L Moore <dlmoore@ix.netcom.com>13 Dec 1997 12:42:23 -0500

From comp.compilers

Related articles
The signs of literals lin@cs.monash.edu.au (Hung-Ta Lin) (1997-12-07)
Re: The signs of literals dlmoore@ix.netcom.com (David L Moore) (1997-12-10)
Re: The signs of literals clark@quarry.zk3.dec.com (Chris Clark USG) (1997-12-10)
Re: The signs of literals hbaker@netcom.com (1997-12-12)
Re: The signs of literals tim@wagner.Princeton.EDU (1997-12-12)
Re: The signs of literals mtimmerm@microstar.no-spam.com (Matt Timmermans) (1997-12-12)
Re: The signs of literals dlmoore@ix.netcom.com (David L Moore) (1997-12-13)
| List of all articles for this month |

 From: David L Moore Newsgroups: comp.compilers Date: 13 Dec 1997 12:42:23 -0500 Organization: Netcom References: 97-12-053 97-12-060 97-12-083 Keywords: lex, syntax

David L Moore <dlmoore@ix.netcom.com> wrote:
>
> > Second, it would be incorrect to treat a negative number as a positive
> > number with a unary minus preceding it because, if your machine is
> > two's compliment, you can now not represent the largest negative
> > integer (eg -32768 in 16 bit) as a constant.

Henry Baker wrote:
> The solution to this problem is well-known and trivial. You _always_
> build up integers using _negative_ arithmetic, as in `n:=10*n-d'.
> You then convert to positive at the last moment _unless_ you see a
> minus sign.

The last point in my message, which was not quoted above, made exactly
this point. Here is what I wrote:

---------------------------------------------------------------------
If you do not use infinite size arithmetic, make sure you convert the
constant to binary by assuming it is negative, and then converting it
to positive at the end if it is not negative, rather than the other way
round. Otherwise you are stuck with an overflow on the maximum negative
value again as you cannot represent it as a positive binary value.
----------------------------------------------------------------------

The point of my second point (as quoted) was that you have to do this
lexically; you cannot do it syntactically. My third point then
described the correct way to do it lexically.

Incidentally, while this may be well known, I have certainly seen it
done the wrong way, both in compilers and in text books.
[I think the two messages passed in the night; I was several days behind
in my moderator's mailbox. -John]

--

Post a followup to this message