Re: Another way to do x^n
- From: "Julian V. Noble" <jvn@xxxxxxxxxxxx>
- Date: Fri, 19 May 2006 10:22:45 -0400
Peter Dickerson wrote:
"Julian V. Noble" <jvn@xxxxxxxxxxxx> wrote in message
news:e4jclh$8qn$1@xxxxxxxxxxxxxxxxxxxxxxxxxxx
John Doty wrote:
[ deleted ]
power(x,n) = power(x*x,n/2) * power(x, n mod 2 )
It's just MADE for recursion!
I think this depends on whether you are a mathematician or software
engineer. Recursion is often clear and obvious to mathematicians to not so
to the engineer. Mind you, if I were an engineer and not a Mathematician I'd
not use either method due to rounding errors (which are the same since the
same arithmatic is done in the same order but with different contol
structures).
Peter
I don't know what you are talking about re: roundoff error. This
seems to be a MYTH about successive multiplications. In these
tests, power is the doubly recursive (cute) version, f^n is the
iterative loop (about 2x faster) and the third method is exponen-
tiation, x^n = exp(n*ln(x)):
fpi 50 power fs. 7.2026719447158E24 ok
fpi 50 f^n fs. 7.2026719447158E24 ok
fpi fln 50e0 f* fexp fs. 7.2026719447158E24 ok
Please note the results are IDENTICAL to 16 places. One would
not employ successive multiplication for n>50 or so in any event
because it is then slower than exponentiation.
Maybe you will get roundoff problems with exponents of 10^6,
but who in their right minds would use the method for such
large exponents?
--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
.
- Follow-Ups:
- Re: Another way to do x^n
- From: Peter Dickerson
- Re: Another way to do x^n
- References:
- Another way to do x^n
- From: Julian V. Noble
- Re: Another way to do x^n
- From: John Doty
- Re: Another way to do x^n
- From: Julian V. Noble
- Re: Another way to do x^n
- From: Peter Dickerson
- Another way to do x^n
- Prev by Date: Re: Another way to do x^n
- Next by Date: Re: Froth running under Windows XP
- Previous by thread: Re: Another way to do x^n
- Next by thread: Re: Another way to do x^n
- Index(es):