Post by Richard HeathfieldPost by johnHi, regarding Turbo Pascal 7.x (or the standard), is there any power
function provided?
Something like
power(10,4) meaning 10 ^4, or something similar.
Pascal offers EXP and LN. N^P = EXP(P * LN(N)).
Thus, 10^4 = EXP(4 * LN(10)).
The original example was an integer example, and you have given a float
method. I'm almost sure that your code will almost always give a float
of exact integer value (if in the exact range 0..2^53 for Double and
more for Extended). That may not be good enough.
For integer powers, one can use a multiplying loop, though that would
get slow for 1.000001^1000000 (which gives about e); in which case one
can be cunning as in (written in Borland, but perhaps generic) -
function ExpNum(const A : Xtype ; const B : longint) : Xtype ;
begin if B=0 then ExpNum := 1.0 else
if Odd(B) then ExpNum := ExpNum(A, B-1) * A
else ExpNum := Sqr(ExpNum(A, B div 2)) ;
end {ExpNum} ;
which would for that do about log2(1000000) = 20 recursions. It could
be rewritten iterative.
My <URL:http://www.merlyn.demon.co.uk/pas-math.htm> refers.
With an integer number to be raised to a power, a multiplying loop can
never take unreasonably long, unless the number is -1 0 +1 or runtime
checks are off.
But the actual answer to the OP is "NO" or "NOT IN TP".
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.