Python from Wise Guy's Viewpoint

Matthias Blume find at my.address.elsewhere
Tue Oct 28 11:52:10 EST 2003


Matthew Danish <mdanish at andrew.cmu.edu> writes:

> On Mon, Oct 27, 2003 at 10:08:15PM +0100, Joachim Durchholz wrote:
> > Matthew Danish wrote:
> > 
> > >On Mon, Oct 27, 2003 at 07:00:01PM +0100, Andreas Rossberg wrote:
> > >
> > >>Pascal Costanza wrote:
> > >>
> > >>>Can you show me an example of a program that does't make sense anymore 
> > >>>when you strip off the static type information?
> > >>
> > >>Here is a very trivial example, in SML:
> > >>
> > >>	20 * 30
> > >>
> > >>Multiplication, as well as literals, are overloaded. Depending on 
> > >>whether you type this expression as Int8.int (8-bit integers) or 
> > >>IntInf.int (infinite precision integer) the result is either 600 or an 
> > >>overflow exception.
> > >
> > >May I point out that the correct answer is 600, not overflow?
> > 
> > No, the correct answer isn't 600 in all cases.
> > If it's infinite-precision arithmetic, the correct answer is indeed 600.
> > If it's 8-bit arithmetic with overflow, there is no correct answer.
> > If it's 8-bit arithmetic with wrap-around, the correct answer is 88.
> > If it's 8-bit arithmetic with saturation, the correct answer is 255.
> > (The result happens to be independent of whether the arithmetic is 
> > signed or unsigned.)
> 
> What is this stuff?  I am talking about integers here!  You know, the
> infinite set with the same cardinality as natural numbers.

I didn't see you say that.  If you write 20 * 30 in SML, then you are
*not* talking about the infinite set of integers but rather about a set
[-2^N..2^N-1] where N is something like 31 or 32.  If you had written

   20 * 30 : IntInf.int

or something like that, then you would have talked about the infinite
set of integeres, and you would have gotten your "correct" result of
600.  [I still have no idea why that is more "correct" than, say, 18.
That's the result in Z_{97}.]

> Lisp gets exact rational arithmetic right, why don't ML or Haskell?
> Clever compilers like CMUCL can even emit efficient code when it can
> figure out the specific types.  Surely a statically typed language would
> find this even easier!

Sure, it is definitely not any harder than it is in Lisp.  The problem
is that for many algorithms people want to be sure that the compiler
represents their values in machine words.  Infinite precision is
needed sometimes, but in the majority of cases it is overkill.  If you
need infinite precision, specify the type (IntInf.int in SML's case).
A clever compiler might optimize that like a Lisp compiler does.  In
most other cases, why take any chances?

Matthias




More information about the Python-list mailing list