Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: MSC C arithmetic Message-ID: <7976@mimsy.UUCP> Date: Fri, 14-Aug-87 01:49:53 EDT Article-I.D.: mimsy.7976 Posted: Fri Aug 14 01:49:53 1987 Date-Received: Sat, 15-Aug-87 14:29:40 EDT References: <8792@brl-adm.ARPA> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 39 In article <8792@brl-adm.ARPA> jdickson@zook.Jpl.Nasa.GOV (Jeff Dickson) writes: > unsigned int repeat_cnt; > unsigned int units; > unsigned long n; > for (n = repeat_cnt * units; n > 0; n--) [and] > double total_bits; > unsigned int repeat_cnt; > unsigned int units; > unsigned int blksiz; > total_bits = repeat_cnt * units * blksiz * 16.0 [produce unexpected results]. 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. 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: typeof(repeat_cnt * (expr0)) is typemax(typeof(repeat_cnt), typeof(expr0)); typeof(expr0) is typemax(typeof(units), typeof(expr1)); typeof(expr1) is typemax(typeof(blksize), typeof(16.0)) which is typemax(type_u_int, type_double) which is just type_double; this propagates all the way back to the top. The first example works on a Sun because there sizeof(u_int) == sizeof(u_long), so unsigned int arithmetic is the same as unsigned long arithmetic. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: seismo!mimsy!chris