Path: utzoo!utgpu!water!watmath!clyde!bellcore!faline!thumper!ulysses!andante!princeton!udel!gatech!ncar!ames!amelia!prandtl.nas.nasa.gov!msf From: msf@prandtl.nas.nasa.gov.UUCP Newsgroups: comp.lang.c Subject: Re: Exponentiation in C (was: How not to write a loop) Message-ID: <709@amelia.nas.nasa.gov> Date: 1 Jul 88 11:31:46 GMT References: <16276@brl-adm.ARPA> <329@accelerator.eng.ohio-state.edu> <4778@haddock.ISC.COM> <3167@ritcsh.UUCP> Sender: news@amelia.nas.nasa.gov Reply-To: msf@prandtl (Michael S. Fischbein) Organization: NASA Langley Research Center, Hampton, VA Lines: 38 In article <3167@ritcsh.UUCP> gregory@ritcsh.UUCP (Gregory Conway) writes: >I'm still a little wet behind the ears where C is concerned, so (politely) >correct me if I'm wrong, but what's wrong with this: > > y = 3.0^2 > log (y) = log (3.0^2) > log (y) = 2 * log(3.0) > > So...... > > y = exp ((double)2 * log(3.0)); > >Mathematically, it should work. I'm just not so sure that it will compile. >How about it?? Well, it didn't compile. After adding semicolons and a few other dew dads, just to see what would come out, I got this: main() { union { int a; float b; } y; y.b = 3.0; printf("%f\n", (float) (y.a ^ 2)); } and the answer was 1077936130.000000. I had to throw in the union 'cause otherwise the compiler barfed on doing exclusive-or on a float. :-) mike Michael Fischbein msf@ames-nas.nas.nasa.gov ...!seismo!decuac!csmunix!icase!msf These are my opinions and not necessarily official views of any organization.