Exponentiation in C (was: How not to write a loop)
Gregory Conway
gregory at ritcsh.UUCP
Fri Jul 1 08:29:41 AEST 1988
In article <4778 at haddock.ISC.COM>, karl at haddock.ISC.COM (Karl Heuer) writes:
] In article <808 at l.cc.purdue.edu> cik at l.cc.purdue.edu (Herman Rubin) writes:
] >In article <12784 at apple.Apple.COM>, bgibbons at Apple.COM (Bill Gibbons) writes:
] >>... raising a floating-point value to an integer (typed as integer) power is
] >>always done with repeated multiplies (in about log2(exponent) time),
]
] That's a strange comment to be posting to a C newsgroup.
]
] >Apparently Bill missed the discussion a few months ago about exponentiation
] >in C. There was considerable disagreement about what should be done about
] >the problem, and a rather large number of correspondents seemed unable to
] >consider computing powers except by the pow(x,y) function, which does not
] >use the reasonably quick and exact method.
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??
--
==> Gregory Conway @ Computer Science House, RIT, Rochester, New York <==
"I never did write that love song, the words just never seemed to flow.
Now sad in reflection, did I gaze through perfection,
and examine the shadows on the other side of morning." - Marillion
More information about the Comp.lang.c
mailing list