Re: Distributivity and types

anton@mips.complang.tuwien.ac.at (Anton Ertl)
27 May 1999 23:27:54 -0400

          From comp.compilers

Related articles
Distributivity and types sanjayp@ddi.com (Sanjay Pujare) (1999-05-22)
Re: Distributivity and types wclodius@aol.com (1999-05-22)
Re: Distributivity and types adrian@dcs.rhbnc.ac.uk (1999-05-27)
Re: Distributivity and types anton@mips.complang.tuwien.ac.at (1999-05-27)
Re: Distributivity and types adrian@dcs.rhbnc.ac.uk (1999-05-29)
Re: Distributivity and types jonathan_barker@my-deja.com (1999-05-29)
| List of all articles for this month |

From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.compilers
Date: 27 May 1999 23:27:54 -0400
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
References: 99-05-111 99-05-122
Keywords: optimize, types, arithmetic

Sanjay Pujare (sanjayp@ddi.com) wrote:
> : Consider the expression
>
> : a*(b+c)
>
> : Because of distributivity this can be changed to
>
> : a*b+a*c
...
  adrian@dcs.rhbnc.ac.uk (A Johnstone) writes:
> In any case, due to the finite representation problem you should
> always be cautious when applying algebraic laws: machine integers are
> not mathematical integers. Consider the (contrived) case in which b is
> a large number near the high end of the signed representation (eg
> 32,000 in a 16-bit signed 2's complement rep) and c is a negative
> number near the low end (eg -32,000). The bracket (b+c) will then
> evaluate to a number in the middle of the rep. If a is, say, 10 than
> a*(b+c) will not overflow, but (a*b) + (a*c) will overflow so you'll
> get different answers.


Only if overflows trap. If, OTOH, you use modulo (aka wrap-around)
arithmetic, this law holds (as do many others, because the cardinal
numbers modulo 2^N are a field; two's complement numbers are just a
different interpretation, the operations + and * are the same as for
unsigned numbers) and you will get the correct answer.


That's why IMO modulo arithmetic is the best thing we can have if we
are too cheap to afford real integers. All the overflow trapping
causes at least as many headaches as it saves.


- anton
--
M. Anton Ertl Some things have to be seen to be believed
anton@mips.complang.tuwien.ac.at Most things have to be believed to be seen
http://www.complang.tuwien.ac.at/anton/home.html


Post a followup to this message

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