Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!clyde!cuae2!ltuxa!ttrdc!levy From: levy@ttrdc.UUCP (Daniel R. Levy) Newsgroups: net.lang.c Subject: Re: Conversion, rounding, and truncation Message-ID: <1289@ttrdc.UUCP> Date: Sun, 2-Nov-86 05:07:11 EST Article-I.D.: ttrdc.1289 Posted: Sun Nov 2 05:07:11 1986 Date-Received: Tue, 4-Nov-86 06:09:23 EST References: <797@oakhill.UUCP> Organization: AT&T, Computer Systems Division, Skokie, IL Lines: 42 In article <797@oakhill.UUCP>, tomc@oakhill.UUCP (Tom Cunningham) writes: > double d; > long l; > float f; > double pow(); > d = pow(2.0,3.0); > l = d; > printf("d = %f, l = %ld\n", d, l); > f = d; > l = f; > printf("f = %f, l = %ld\n", d, l); > >The output from code generated by the Sun 3 compiler (4.2BSD), Microsoft C, >and VAX VMS C is: > d = 8.000000, l = 8 > d = 8.000000, l = 8 >Output from a Unix SYSV compiler is: > d = 8.000000, l = 7 > d = 8.000000, l = 8 >it looks like the pow() in the SYSV >implementation returns something not quite 8.0. You have guessed right. pow(x,y) is basically implemented as exp(y*log(x)), with domain and range checking too of course. Depending on the exact implemen- tation of exp() and log(), this can give results which are slightly off even when an exact solution would be representable (as in your example). As a guru explained to me, this is done, even if it would be possible to special- case exactly-integral exponents to pow(), in order to preserve the local continuity of the function. For an example of why this would be wanted, consider that a program which iterates to find a desired value of some complicated, but supposedly continuous, function containing pow() could conceivably get hung up in an infinite loop if pow(2.0,3.0+delta) < pow(2.0,3.0) (delta being a small increment greater than zero) due to the latter being calculated in a different way. -- ------------------------------- Disclaimer: The views contained herein are | dan levy | yvel nad | my own and are not at all those of my em- | an engihacker @ | ployer or the administrator of any computer | at&t computer systems division | upon which I may hack. | skokie, illinois | -------------------------------- Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa, go for it! allegra,ulysses,vax135}!ttrdc!levy