From: | Jerry Leichter <jerrold.leichter@smarts.com> |
Newsgroups: | comp.compilers |
Date: | 17 Nov 2000 23:44:40 -0500 |
Organization: | System Management ARTS |
References: | 00-11-046 00-11-082 00-11-120 |
Keywords: | modula, Pascal, types |
Posted-Date: | 17 Nov 2000 23:44:40 EST |
| The real "serious" descendent of Pascal, Ada, brings signed and
| unsigned types for all these lengths...
On the other hand, Modula-3 does not have an unsigned type - and I think
with good reason.
There are two reasons for wanting an unsigned type:
- To extend the range of small integers. This was very
important in the days of 16-bit ints, and is almost
certainly why unsigned ints were originally added
to C. With 32 and now 64 bit ints the standard
these days, this is rarely of any significance.
- To implement "modulo arithmetic". Unsigned ints typically
(and, by standard in C/C++) the mathematical properties
of arithmetic mod 2^k. There are times when this is
useful.
These two reasons for wanting an unsigned type are pretty much
independent of each other. Once you decide that the first reason is
no longer compelling, but the second is of interest, you have to ask
the question: Do we really need a *type* for this - or do we just want
the operations? The latter is the approach Modula-3 took: There are
two basic integral types, INTEGER and CARDINAL; the latter is a
subrange of INTEGER. There is also a standard Word package that
provides such operations as modulo-2^k arithmetic (on INTEGER's).
Needless to say, though the package provides what look like functions,
compilers generate the code in-line just as they do for the usual
operator-form arithmetic. The resulting programs may be somewhat more
verbose - a general property of Pascal descendents - it can express
exactly the same semantics as C.
The *big* advantage to this approach is that it eliminates all kinds
of issues about how signed and unsigned integers should work when they
"meet across an operator". This was an area of ambiguity - and
differences among implementations - in C prior to the first C
standard. The rules in the C standard, while definite, have complex
and non- obvious implications, and this whole area remains a trap for
unwary programmers.
-- Jerry
Return to the
comp.compilers page.
Search the
comp.compilers archives again.