Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!lsuc!sq!msb From: msb@sq.uucp (Mark Brader) Newsgroups: comp.lang.c Subject: Re: MSC C arithmetic Message-ID: <1987Aug17.185845.20211@sq.uucp> Date: Mon, 17-Aug-87 18:58:45 EDT Article-I.D.: sq.1987Aug17.185845.20211 Posted: Mon Aug 17 18:58:45 1987 Date-Received: Tue, 18-Aug-87 05:43:49 EDT References: <8792@brl-adm.ARPA> <7976@mimsy.UUCP> Reply-To: msb@sq.UUCP (Mark Brader) Organization: SoftQuad Inc., Toronto Lines: 52 Checksum: 10487 To the original question about: > > unsigned int repeat_cnt; > > unsigned int units; > > unsigned long n; > > for (n = repeat_cnt * units; n > 0; n--) ... > > double total_bits; > > unsigned int repeat_cnt; > > unsigned int units; > > unsigned int blksiz; > > total_bits = repeat_cnt * units * blksiz * 16.0 Chris Torek writes: > Microsoft C is compiling the first example correctly. The extension > rules require only that `repeat_cnt * units' be done in unsigned > arithmetic; at least one of the two multiplicands must be of type > long or unsigned long for the operation to be done in long arithmetic. which is true, but he continues: > The second result is not so defensible; the 16.0 should modify the > entire operation so that it is done in double. This follows from > the type matching rules ... which I think is wrong. I quote K&R appendix A section 7.3: "The multiplicative operators *, /, and % group left-to-right." In the ANSI draft, the same statement is concealed behind the grammar in 3.3.5 that defines a multiplicative expression as being, among other things, "multiplicative-expression * cast-expression". It seems clear to me that both sources are saying that the assignment must be considered as: total_bits = ((repeat_cnt * units) * blksiz) * 16.0 whereupon the two left-hand multiplies are done, as the MS C compiler compiled, in unsigned ints. I must admit that it is NOT clear to me whether the regrouping rules allow the compiler to alternatively compile this as: total_bits = repeat_cnt * (units * (blksiz * 16.0)) whereupon the arithmetic would indeed be done in double. I think they do. Perhaps Doug or someone who has studied this closely could comment on it. That is, if the removal (to which I probably object) of the regrouping rules has not already happened. Mark Brader "The language should match the users, utzoo!sq!msb not vice versa" -- Brian W. Kernighan