Exponentiation in C (was: How not to write a loop)
msf at prandtl.nas.nasa.gov.UUCP
msf at prandtl.nas.nasa.gov.UUCP
Fri Jul 1 21:31:46 AEST 1988
In article <3167 at ritcsh.UUCP> gregory at 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 at ames-nas.nas.nasa.gov
...!seismo!decuac!csmunix!icase!msf
These are my opinions and not necessarily official views of any
organization.
More information about the Comp.lang.c
mailing list