Path: utzoo!attcan!uunet!husc6!bloom-beacon!apple!rutgers!bellcore!faline!thumper!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c Subject: Re: Array indexing vs. pointers... Message-ID: <8273@alice.UUCP> Date: 8 Oct 88 13:51:47 GMT References: <836@proxftl.UUCP> <3105@hubcap.UUCP> <1700@dataio.Data-IO.COM> <1706@dataio.Data-IO.COM> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 31 In article <1706@dataio.Data-IO.COM>, bright@Data-IO.COM (Walter Bright) writes: > For example, suppose you wished to convert Celsius to Farenheit > in a tight loop. Multiplying by the scale factor is much faster > than dividing by the reciprocal, and the answer will be correct > to the desired accuracy. This obviously depends on what you desire. For instance, whether I evaluate f = 32 + c * (9.0/5.0); or f = 32 + c / (5.0/9.0); I will not get as accurate an answer as if I evaluate f = 32 + (c * 9.0) / 5.0; Now, I know that a sensible compiler will do the division inside the parentheses for me at compile time in each of the first two examples above, where the third one does a multiplication *and* a division. The third is clearly the slowest. But on a machine with sensible floating-point, it guarantees me the most accurate result possible whenever c is an integer. In particular, when I convert 100 degrees Celsius to Fahrenheit, I get 212, not 211.9999999999999 or 212.0000000000001. The other two ways of doing it do not guarantee that. -- --Andrew Koenig ark@europa.att.com