Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rutgers!umnd-cs!umn-cs!randy From: randy@umn-cs.UUCP (Randy Orrison) Newsgroups: comp.lang.c Subject: Re: MSC C arithmetic Message-ID: <2042@umn-cs.UUCP> Date: Wed, 19-Aug-87 17:38:39 EDT Article-I.D.: umn-cs.2042 Posted: Wed Aug 19 17:38:39 1987 Date-Received: Sat, 22-Aug-87 05:20:47 EDT References: <8792@brl-adm.ARPA> <305@wrs.UUCP> Reply-To: randy@umn-cs.UUCP (Randy Orrison) Organization: UofM Math Department Lines: 44 In article <305@wrs.UUCP> dg@wrs.UUCP (David Goodenough) writes: >The second example fails because '*' groups left to right (see page 49 of the >gospel according to Kernighan and Ritchie :-) and it does the two integer >multiplications first, and then the float (i.e. double). This should work >correctly if you wrap the last multiplication in parentheses: > >> total_bits = repeat_cnt * (units * (blksiz * 16.0)); > >as this then forces all multiplications to have one float operand, hence >getting the desired result. Interesting. Page 49 of my K&R states that "expressions involving one of the associative and commutative operators (*,...) can be rearranged even when parenthesized." Which means that your parentheses should do nothing, and that the original expression is equivalent to: 16.0 * repeat_cnt * units * blksiz Which would mean that the whole expression should be evaluated in double. Since the compiler is free to evaluate the expression in whatever order is wants, does that mean that it is not determined what types it will use? It seems to me that both of the following are legal: repeat_cnt * units * blksiz * 16.0 --int-- --double-- --double-- and 16.0 * blksiz * units * repeat_cnt --double-- --double-- --double-- Which we know can give different results. Is this right? (under K&R and ANSI) (Side note to ANSI people: I like to use parentheses to make my meaning clean to me. If the compiler can think of a better order to do things, LET IT!!! I LOVE unary plus, though temporary variables are just fine.) -- Randy Orrison, University of Minnesota School of Mathematics UUCP: {ihnp4, seismo!rutgers!umnd-cs, sun}!umn-cs!randy ARPA: randy@ux.acss.umn.edu (Yes, these are three BITNET: randy@umnacvx different machines)