Conversion, rounding, and truncation

Daniel R. Levy levy at ttrdc.UUCP
Sun Nov 2 20:07:11 AEST 1986


In article <797 at oakhill.UUCP>, tomc at 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



More information about the Comp.lang.c mailing list